Completed
Push — master ( b4ede8...73596f )
by Guilherme
05:13
created

AuthorizationManager::getAuthorization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\CoreBundle\Service;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Authorization;
15
use LoginCidadao\CoreBundle\Entity\AuthorizationRepository;
16
use LoginCidadao\CoreBundle\Model\PersonInterface;
17
use LoginCidadao\OAuthBundle\Model\ClientInterface;
18
19
class AuthorizationManager
20
{
21
    /** @var string merge new scope with old one */
22
    public const SCOPE_MERGE = 'merge';
23
24
    /** @var string replace old scope by new one */
25
    public const SCOPE_REPLACE = 'replace';
26
27
    /** @var EntityManagerInterface */
28
    private $em;
29
30
    /** @var AuthorizationRepository */
31
    private $repository;
32
33
    /**
34
     * AuthorizationManager constructor.
35
     * @param EntityManagerInterface $em
36
     * @param AuthorizationRepository $repository
37
     */
38 4
    public function __construct(EntityManagerInterface $em, AuthorizationRepository $repository)
39
    {
40 4
        $this->em = $em;
41 4
        $this->repository = $repository;
42 4
    }
43
44
    /**
45
     * @param PersonInterface $person
46
     * @param ClientInterface $client
47
     * @return Authorization|null
48
     */
49 4
    public function getAuthorization(PersonInterface $person, ClientInterface $client): ?Authorization
50
    {
51
        /** @var Authorization $authorization */
52 4
        $authorization = $this->repository->findOneBy([
53 4
            'person' => $person,
54 4
            'client' => $client,
55
        ]);
56
57 4
        return $authorization;
58
    }
59
60
    /**
61
     * @param PersonInterface $person
62
     * @param ClientInterface $client
63
     * @param array $scope
64
     * @param string $scopeStrategy
65
     * @param bool $flush
66
     * @return Authorization
67
     */
68 3
    public function enforceAuthorization(
69
        PersonInterface $person,
70
        ClientInterface $client,
71
        array $scope,
72
        string $scopeStrategy,
73
        bool $flush = true
74
    ): Authorization {
75 3
        $authorization = $this->getAuthorization($person, $client);
76 3
        if (null === $authorization) {
77 2
            $authorization = (new Authorization())
78 2
                ->setPerson($person)
79 2
                ->setClient($client);
80 2
            $this->em->persist($authorization);
81
        }
82
83 3
        $authorization->setScope(
84 3
            $this->mergeScope($authorization->getScope(), $scope, $scopeStrategy)
85
        );
86
87 2
        if ($flush) {
88 2
            $this->em->flush();
89
        }
90
91 2
        return $authorization;
92
    }
93
94 3
    private function mergeScope(array $oldScope, array $newScope, string $strategy)
95
    {
96 3
        if (self::SCOPE_REPLACE === $strategy) {
97 1
            return $newScope;
98 2
        } elseif (self::SCOPE_MERGE === $strategy) {
99 1
            return array_unique(array_merge($oldScope, $newScope));
100
        } else {
101 1
            throw new \InvalidArgumentException("Invalid scope merging strategy: '{$strategy}'");
102
        }
103
    }
104
}
105