GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

KeyFactory::getKeyLength()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Service\Key;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata;
7
8
/**
9
 * Factory which generates the api keys.
10
 */
11
class KeyFactory implements KeyFactoryInterface
12
{
13
    const MAX_API_KEY_GENERATION_ATTEMPTS = 200;
14
15
    /**
16
     * @var ObjectManager
17
     */
18
    private $om;
19
20
    /**
21
     * @var string
22
     */
23
    private $modelName;
24
25
    /**
26
     * @var ClassMetadata
27
     */
28
    private $metadata;
29
30
    /**
31
     * @var int
32
     */
33
    private $keyLength;
34
35
    /**
36
     * Constructor.
37
     *
38
     * @param ObjectManager $om
39
     * @param string        $modelName
40
     * @param ClassMetadata $metadata
41
     * @param int           $keyLength
42
     */
43 14
    public function __construct(ObjectManager $om, $modelName, ClassMetadata $metadata, $keyLength = 200)
44
    {
45 14
        $this->om = $om;
46 14
        $this->modelName = (string) $modelName;
47 14
        $this->metadata = $metadata;
48 14
        $this->keyLength = (int) floor($keyLength / 2);
49 14
    }
50
51
    /**
52
     * {@inheritdoc}
53
     *
54
     * @throws \RuntimeException If no unique key can be created.
55
     */
56 8
    public function getKey()
57
    {
58 8
        $repository = $this->om->getRepository($this->modelName);
59 8
        $count = 0;
60
61
        do {
62 8
            ++$count;
63
64 8
            if ($count > static::MAX_API_KEY_GENERATION_ATTEMPTS) {
65 1
                throw new \RuntimeException(sprintf(
66 1
                    'Unable to generate a new api key, stopping after %d tries!',
67 1
                    static::MAX_API_KEY_GENERATION_ATTEMPTS
68
                ));
69
            }
70
71 8
            $key = $this->doGenerate();
72 8
        } while (null !== $repository->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => $key)));
73
74 7
        return $key;
75
    }
76
77
    /**
78
     * Getter for the object manager.
79
     *
80
     * @return ObjectManager
81
     */
82
    protected function getOm()
83
    {
84
        return $this->om;
85
    }
86
87
    /**
88
     * Getter for the model name.
89
     *
90
     * @return string
91
     */
92
    protected function getModelName()
93
    {
94
        return $this->modelName;
95
    }
96
97
    /**
98
     * Getter for the api key property.
99
     *
100
     * @return ClassMetadata
101
     */
102
    protected function getMetadata()
103
    {
104
        return $this->metadata;
105
    }
106
107
    /**
108
     * Getter for the key length.
109
     *
110
     * @return int
111
     */
112
    protected function getKeyLength()
113
    {
114
        return $this->keyLength;
115
    }
116
117
    /**
118
     * Generates the bare key.
119
     *
120
     * @return string
121
     */
122 8
    protected function doGenerate()
123
    {
124 8
        return bin2hex(random_bytes($this->keyLength));
125
    }
126
}
127