Passed
Push — master ( 401cce...21cdfe )
by Derek Stephen
03:23
created

ScopeRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 58.81%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 57
ccs 10
cts 17
cp 0.5881
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 5 1
A finalizeScopes() 0 13 2
A save() 0 4 1
A getScopeEntityByIdentifier() 0 5 1
1
<?php
2
3
namespace OAuth\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Exception;
7
use League\OAuth2\Server\Entities\ClientEntityInterface;
8
use League\OAuth2\Server\Entities\ScopeEntityInterface;
9
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
10
use OAuth\Client;
11
use OAuth\Scope;
12
13
class ScopeRepository extends EntityRepository implements ScopeRepositoryInterface
14
{
15
    /**
16
     * @param string $identifier
17
     * @return null|Scope
18
     */
19 4
    public function getScopeEntityByIdentifier($identifier)
20
    {
21
        /** @var Scope $scope */
22 4
        $scope = $this->findOneBy(['identifier' => $identifier]);
23 4
        return $scope;
24
    }
25
26
    /**
27
     * @param array $scopes
28
     * @param string $grantType
29
     * @param ClientEntityInterface $clientEntity
30
     * @param null|string $userIdentifier
31
     * @return ScopeEntityInterface[]
32
     * @throws Exception
33
     */
34 3
    public function finalizeScopes(array $scopes, $grantType, ClientEntityInterface $clientEntity, $userIdentifier = null)
35
    {
36
        /** @var Client $clientEntity */
37 3
        $clientScopes = $clientEntity->getScopes()->getValues();
38
        $finalScopes = array_uintersect($scopes, $clientScopes, function($a, $b) {
39 3
            return strcmp(spl_object_hash($a), spl_object_hash($b));
40 3
        });
41
42 3
        if (count($finalScopes) < count($scopes)) {
43 1
            throw new Exception('Scopes not authorised.', 403);
44
        }
45
46 2
        return $finalScopes;
47
    }
48
49
    /**
50
     * @param Scope $scope
51
     * @return Scope
52
     * @throws \Doctrine\ORM\OptimisticLockException
53
     */
54
    public function create(Scope $scope)
55
    {
56
        $this->_em->persist($scope);
57
        $this->_em->flush($scope);
58
        return $scope;
59
    }
60
61
    /**
62
     * @param Scope $scope
63
     * @return Scope
64
     * @throws \Doctrine\ORM\OptimisticLockException
65
     */
66
    public function save(Scope $scope)
67
    {
68
        $this->_em->flush($scope);
69
        return $scope;
70
    }
71
}