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.
Completed
Push — master ( 81f84c...8857b5 )
by Maximilian
14:14
created

ApiKeyAuthenticationHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.4285
cc 1
eloc 13
nc 1
nop 6
crap 1
1
<?php
2
3
namespace Ma27\ApiKeyAuthenticationBundle\Model\Login\ApiToken;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use Ma27\ApiKeyAuthenticationBundle\Event\OnAuthenticationEvent;
7
use Ma27\ApiKeyAuthenticationBundle\Event\OnInvalidCredentialsEvent;
8
use Ma27\ApiKeyAuthenticationBundle\Event\OnLogoutEvent;
9
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException;
10
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents;
11
use Ma27\ApiKeyAuthenticationBundle\Model\Key\KeyFactoryInterface;
12
use Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface;
13
use Ma27\ApiKeyAuthenticationBundle\Model\Password\PasswordHasherInterface;
14
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata;
15
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17
/**
18
 * Concrete handler for api key authorization.
19
 */
20
class ApiKeyAuthenticationHandler implements AuthenticationHandlerInterface
21
{
22
    /**
23
     * @var ObjectManager
24
     */
25
    private $om;
26
27
    /**
28
     * @var PasswordHasherInterface
29
     */
30
    private $passwordHasher;
31
32
    /**
33
     * @var KeyFactoryInterface
34
     */
35
    private $keyFactory;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * @var string
44
     */
45
    private $modelName;
46
47
    /**
48
     * @var ClassMetadata
49
     */
50
    private $classMetadata;
51
52
    /**
53
     * Constructor.
54
     *
55
     * @param ObjectManager            $om
56
     * @param PasswordHasherInterface  $passwordHasher
57
     * @param KeyFactoryInterface      $keyFactory
58
     * @param EventDispatcherInterface $dispatcher
59
     * @param string                   $modelName
60
     * @param ClassMetadata            $metadata
61
     */
62 13
    public function __construct(
63
        ObjectManager $om,
64
        PasswordHasherInterface $passwordHasher,
65
        KeyFactoryInterface $keyFactory,
66
        EventDispatcherInterface $dispatcher,
67
        $modelName,
68
        ClassMetadata $metadata
69
    ) {
70 13
        $this->om = $om;
71 13
        $this->passwordHasher = $passwordHasher;
72 13
        $this->keyFactory = $keyFactory;
73 13
        $this->eventDispatcher = $dispatcher;
74 13
        $this->modelName = (string) $modelName;
75 13
        $this->classMetadata = $metadata;
76 13
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 10
    public function authenticate(array $credentials)
82
    {
83 10
        $loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY);
84 10
        $passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY);
85
86 10
        if (!array_key_exists($passwordProperty, $credentials)) {
87 1
            throw new \InvalidArgumentException(
88 1
                sprintf('Unable to find password property "%s" in credential set!', $passwordProperty)
89 1
            );
90
        }
91
92 9
        if (!array_key_exists($loginProperty, $credentials)) {
93 1
            throw new \InvalidArgumentException(
94 1
                sprintf('Unable to find login property "%s" in credential set!', $loginProperty)
95 1
            );
96
        }
97
98 8
        $objectRepository = $this->om->getRepository($this->modelName);
99 8
        $object = $objectRepository->findOneBy(array($loginProperty => $credentials[$loginProperty]));
100
101 8
        if (null === $object || !$this->passwordHasher->compareWith($object->getPassword(), $credentials[$passwordProperty])) {
102 3
            $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, new OnInvalidCredentialsEvent($object));
103
104 3
            throw new CredentialException();
105
        }
106
107 5
        $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object));
108
109 5
        $key = $this->classMetadata->getPropertyValue($object, ClassMetadata::API_KEY_PROPERTY);
110 5
        if (empty($key)) {
111 4
            $this->classMetadata->modifyProperty($object, $this->keyFactory->getKey(), ClassMetadata::API_KEY_PROPERTY);
112 4
        }
113
114 5
        $this->om->persist($object);
115
116 5
        $this->om->flush();
117
118 5
        return $object;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124 3
    public function removeSession($user, $purgeJob = false)
125
    {
126 3
        $this->classMetadata->modifyProperty($user, null, ClassMetadata::API_KEY_PROPERTY);
127
128 3
        $event = new OnLogoutEvent($user);
129 3
        if ($purgeJob) {
130 1
            $event->markAsPurgeJob();
131 1
        }
132
133 3
        $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::LOGOUT, $event);
134
135 3
        $this->om->persist($user);
136
137
        // on purge jobs one big flush will be commited to the db after the whole action
138 3
        if (!$purgeJob) {
139 2
            $this->om->flush();
140 2
        }
141 3
    }
142
143
    /**
144
     * Getter for the object manager.
145
     *
146
     * @return ObjectManager
147
     */
148
    protected function getOm()
149
    {
150
        return $this->om;
151
    }
152
153
    /**
154
     * Getter for the password hasher.
155
     *
156
     * @return PasswordHasherInterface
157
     */
158
    protected function getPasswordHasher()
159
    {
160
        return $this->passwordHasher;
161
    }
162
163
    /**
164
     * Getter for the key factory.
165
     *
166
     * @return KeyFactoryInterface
167
     */
168
    protected function getKeyFactory()
169
    {
170
        return $this->keyFactory;
171
    }
172
173
    /**
174
     * Getter for the dispatcher.
175
     *
176
     * @return EventDispatcherInterface
177
     */
178
    protected function getEventDispatcher()
179
    {
180
        return $this->eventDispatcher;
181
    }
182
183
    /**
184
     * Getter for the model name.
185
     *
186
     * @return string
187
     */
188
    protected function getModelName()
189
    {
190
        return $this->modelName;
191
    }
192
193
    /**
194
     * @return ClassMetadata
195
     */
196
    public function getClassMetadata()
197
    {
198
        return $this->classMetadata;
199
    }
200
}
201