1 | <?php |
||
11 | class KeyFactory implements KeyFactoryInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var ObjectManager |
||
15 | */ |
||
16 | private $om; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $modelName; |
||
22 | |||
23 | /** |
||
24 | * @var ClassMetadata |
||
25 | */ |
||
26 | private $metadata; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | private $keyLength; |
||
32 | |||
33 | /** |
||
34 | * Constructor. |
||
35 | * |
||
36 | * @param ObjectManager $om |
||
37 | * @param string $modelName |
||
38 | * @param ClassMetadata $metadata |
||
39 | * @param int $keyLength |
||
40 | */ |
||
41 | 6 | public function __construct(ObjectManager $om, $modelName, ClassMetadata $metadata, $keyLength = 200) |
|
42 | { |
||
43 | 6 | $this->om = $om; |
|
44 | 6 | $this->modelName = (string) $modelName; |
|
45 | 6 | $this->metadata = $metadata; |
|
46 | 6 | $this->keyLength = (int) $keyLength; |
|
47 | 6 | } |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 4 | public function getKey() |
|
53 | { |
||
54 | 4 | $repository = $this->om->getRepository($this->modelName); |
|
55 | 4 | $max = 200; |
|
56 | 4 | $count = 0; |
|
57 | |||
58 | do { |
||
59 | 4 | ++$count; |
|
60 | |||
61 | 4 | if ($count > $max) { |
|
62 | 1 | throw new \RuntimeException('Unable to generate a new api key, stopping after 200 tries!'); |
|
63 | } |
||
64 | |||
65 | 4 | $key = $this->doGenerate(); |
|
66 | 4 | } while (null !== $repository->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $key))); |
|
67 | |||
68 | 3 | return $key; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Getter for the object manager. |
||
73 | * |
||
74 | * @return ObjectManager |
||
75 | */ |
||
76 | protected function getOm() |
||
80 | |||
81 | /** |
||
82 | * Getter for the model name. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | protected function getModelName() |
||
90 | |||
91 | /** |
||
92 | * Getter for the api key property. |
||
93 | * |
||
94 | * @return ClassMetadata |
||
95 | */ |
||
96 | protected function getMetadata() |
||
100 | |||
101 | /** |
||
102 | * Getter for the key length. |
||
103 | * |
||
104 | * @return int |
||
105 | */ |
||
106 | protected function getKeyLength() |
||
110 | |||
111 | /** |
||
112 | * Generates the bare key. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 4 | private function doGenerate() |
|
120 | } |
||
121 |