1: <?php
2:
3: namespace PHPixie\Security;
4:
5: class Tokens
6: {
7: protected $storageTypes = array(
8: 'database'
9: );
10:
11: protected $builder;
12: protected $database;
13:
14: public function __construct($builder, $database)
15: {
16: $this->builder = $builder;
17: $this->database = $database;
18: }
19:
20: public function token($series, $userId, $challenge, $expires, $string = null)
21: {
22: return new Tokens\Token($series, $userId, $challenge, $expires, $string);
23: }
24:
25: public function handler($configData)
26: {
27: return new Tokens\Handler(
28: $this,
29: $this->builder->random(),
30: $configData
31: );
32: }
33:
34: public function sqlStorage($connection, $configData)
35: {
36: return new Tokens\Storage\Database\SQL(
37: $this,
38: $connection,
39: $configData
40: );
41: }
42:
43: public function mongoStorage($connection, $configData)
44: {
45: return new Tokens\Storage\Database\Mongo(
46: $this,
47: $connection,
48: $configData
49: );
50: }
51:
52: public function buildStorage($configData)
53: {
54: $type = $configData->get('type', 'database');
55: if(!in_array($type, $this->storageTypes)) {
56: throw new \PHPixie\Security\Exception("Token storage type '$type' does not exist");
57: }
58:
59: $method = $type.'Storage';
60: return $this->$method($configData);
61: }
62:
63: public function databaseStorage($configData)
64: {
65: $connectionName = $configData->get('connection', 'default');
66: $connection = $this->database->get($connectionName);
67:
68: if($connection instanceof \PHPixie\Database\Type\SQL\Connection) {
69: return $this->sqlStorage($connection, $configData);
70: }
71:
72: if($connection instanceof \PHPixie\Database\Driver\Mongo\Connection) {
73: return $this->mongoStorage($connection, $configData);
74: }
75:
76: $class = get_class($connection);
77: throw new \PHPixie\Security\Exception("No storage for the '$class' connection");
78: }
79: }