Failed Conditions
Push — issue#765 ( 54a2c2...25bda7 )
by Guilherme
09:33
created

RemoteClaimManager::getRemoteClaimsWithTokens()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 2
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 3
rs 9.4285
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\RemoteClaimsBundle\Model;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Authorization;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\OAuthBundle\Model\ClientInterface;
17
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorization;
18
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorizationRepository;
19
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimRepository;
20
21
class RemoteClaimManager implements RemoteClaimManagerInterface
22
{
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26
    /** @var RemoteClaimAuthorizationRepository */
27
    private $remoteClaimAuthorizationRepository;
28
29
    /** @var RemoteClaimRepository */
30
    private $remoteClaimRepository;
31
32
    /**
33
     * RemoteClaimManager constructor.
34
     * @param EntityManagerInterface $em
35
     * @param RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository
36
     * @param RemoteClaimRepository $remoteClaimRepository
37
     */
38 19
    public function __construct(
39
        EntityManagerInterface $em,
40
        RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository,
41
        RemoteClaimRepository $remoteClaimRepository
42
    ) {
43 19
        $this->em = $em;
44 19
        $this->remoteClaimAuthorizationRepository = $remoteClaimAuthorizationRepository;
45 19
        $this->remoteClaimRepository = $remoteClaimRepository;
46 19
    }
47
48
    /**
49
     * {@inheritdoc}
50
     * @throws \Doctrine\ORM\NonUniqueResultException
51
     */
52 2
    public function enforceAuthorization(RemoteClaimAuthorizationInterface $authorization)
53
    {
54 2
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
55 2
        if ($existingAuthorization instanceof RemoteClaimAuthorizationInterface) {
56 1
            return $existingAuthorization;
57
        }
58
59 1
        $this->em->persist($authorization);
60
61 1
        return $authorization;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     * @throws \Doctrine\ORM\NonUniqueResultException
67
     */
68 3
    public function isAuthorized($claimName, PersonInterface $person, ClientInterface $client)
69
    {
70 3
        if (!$claimName instanceof TagUri) {
71 1
            $claimName = TagUri::createFromString($claimName);
72
        }
73 3
        $authorization = (new RemoteClaimAuthorization())
74 3
            ->setClaimName($claimName)
75 3
            ->setPerson($person)
76 3
            ->setClient($client);
77 3
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
78
79 3
        return $existingAuthorization instanceof RemoteClaimAuthorizationInterface;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 1
    public function revokeAllAuthorizations(Authorization $authorization)
86
    {
87 1
        $remoteClaimAuthorizations = $this->remoteClaimAuthorizationRepository
88 1
            ->findAllByClientAndPerson($authorization->getClient(), $authorization->getPerson());
89
90 1
        foreach ($remoteClaimAuthorizations as $remoteClaimAuthorization) {
91 1
            $this->em->remove($remoteClaimAuthorization);
92
        }
93 1
        $this->em->flush();
94
95 1
        return true;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 5
    public function filterRemoteClaims($scopes)
102
    {
103 5
        $returnString = is_string($scopes);
104 5
        if ($returnString) {
105 3
            $scopes = explode(' ', $scopes);
106
        }
107
108 5
        $response = [];
109 5
        foreach ($scopes as $scope) {
110
            try {
111 4
                TagUri::createFromString($scope);
112 4
            } catch (\InvalidArgumentException $e) {
113 4
                $response[] = $scope;
114
            }
115
        }
116
117 5
        if ($returnString) {
118 3
            return implode(' ', $response);
119
        }
120
121 2
        return $response;
122
    }
123
124
    /**
125
     * @inheritDoc
126
     */
127 3
    public function getExistingRemoteClaim(TagUri $claimName)
128
    {
129 3
        return $this->remoteClaimRepository->findOneBy([
130 3
            'name' => $claimName,
131
        ]);
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137 1
    public function getRemoteClaimsFromAuthorization(Authorization $authorization)
138
    {
139 1
        return $this->remoteClaimRepository
140 1
            ->findByClientAndPerson($authorization->getClient(), $authorization->getPerson());
141
    }
142
143
    /**
144
     * @inheritDoc
145
     */
146 1
    public function getRemoteClaimsAuthorizationsFromAuthorization(Authorization $authorization)
147
    {
148 1
        return $this->remoteClaimAuthorizationRepository->findAllByClientAndPerson(
149 1
            $authorization->getClient(), $authorization->getPerson()
150
        );
151
    }
152
153
    /**
154
     * @inheritDoc
155
     */
156 1
    public function getRemoteClaimsWithTokens(ClientInterface $client, PersonInterface $person)
157
    {
158
        /** @var RemoteClaimAuthorizationInterface[] $remoteClaimAuthorizations */
159 1
        $remoteClaimAuthorizations = $this->remoteClaimAuthorizationRepository
160 1
            ->findAllByClientAndPerson($client, $person);
161
162
        /** @var RemoteClaimInterface[] $remoteClaims */
163 1
        $remoteClaims = $this->remoteClaimRepository->findByClientAndPerson($client, $person);
164
165 1
        $response = [];
166 1
        foreach ($remoteClaimAuthorizations as $authorization) {
167 1
            $tag = $this->getTagString($authorization->getClaimName());
168 1
            $response[$tag]['authorization'] = $authorization;
169
        }
170 1
        foreach ($remoteClaims as $remoteClaim) {
171 1
            $tag = $this->getTagString($remoteClaim->getName());
172 1
            $response[$tag]['remoteClaim'] = $remoteClaim;
173
        }
174
175 1
        return $response;
176
    }
177
178
    /**
179
     * @inheritDoc
180
     */
181 1
    public function getRemoteClaimAuthorizationByAccessToken(ClaimProviderInterface $claimProvider, $accessToken)
182
    {
183 1
        return $this->remoteClaimAuthorizationRepository->findOneBy([
184 1
            'claimProvider' => $claimProvider,
185 1
            'accessToken' => $accessToken,
186
        ]);
187
    }
188
189
    /**
190
     * @inheritDoc
191
     */
192 2
    public function updateRemoteClaimUri(TagUri $claimName, $uri)
193
    {
194 2
        $remoteClaim = $this->getExistingRemoteClaim($claimName);
195
196 2
        if (!$remoteClaim instanceof RemoteClaimInterface) {
197
            // TODO: log
198
            // TODO: throw exception?
199 1
            return null;
200
        }
201
202 1
        $remoteClaim->setUri($uri);
203 1
        $this->em->flush();
204
205 1
        return $remoteClaim;
206
    }
207
208
    /**
209
     * @param string|TagUri $tag
210
     * @return string
211
     */
212 1
    private function getTagString($tag)
213
    {
214 1
        return is_string($tag) ? $tag : $tag->__toString();
215
    }
216
}
217