Completed
Push — issue#666 ( 3afa36...5bb037 )
by Guilherme
03:30
created

AuthorizationSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
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\EventSubscriber;
12
13
use LoginCidadao\LogBundle\Traits\LoggerAwareTrait;
14
use LoginCidadao\OpenIDBundle\Event\AuthorizationEvent;
15
use LoginCidadao\OpenIDBundle\LoginCidadaoOpenIDEvents;
16
use LoginCidadao\RemoteClaimsBundle\Model\HttpUri;
17
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimFetcherInterface;
18
use LoginCidadao\RemoteClaimsBundle\Model\RemoteClaimInterface;
19
use LoginCidadao\RemoteClaimsBundle\Model\TagUri;
20
use Psr\Log\LoggerAwareInterface;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
23
class AuthorizationSubscriber implements EventSubscriberInterface, LoggerAwareInterface
24
{
25
    use LoggerAwareTrait;
26
27
    /** @var RemoteClaimFetcherInterface */
28
    private $claimFetcher;
29
30
    /** @var RemoteClaimInterface[] */
31
    private $remoteClaims;
32
33 1
    /**
34
     * AuthorizationSubscriber constructor.
35 1
     * @param RemoteClaimFetcherInterface $claimFetcher
36 1
     */
37
    public function __construct(RemoteClaimFetcherInterface $claimFetcher)
38 15
    {
39
        $this->claimFetcher = $claimFetcher;
40
    }
41 15
42
    public static function getSubscribedEvents()
43
    {
44
        return [
45 1
            LoginCidadaoOpenIDEvents::NEW_AUTHORIZATION_REQUEST => 'onNewAuthorizationRequest',
46
        ];
47 1
    }
48 1
49 1
    public function onNewAuthorizationRequest(AuthorizationEvent $event)
50 1
    {
51 1
        foreach ($event->getScope() as $scope) {
52
            if ($this->checkHttpUri($scope) || $this->checkTagUri($scope)) {
53
                try {
54 1
                    $remoteClaim = $this->claimFetcher->getRemoteClaim($scope);
55
                    $this->remoteClaims[] = $remoteClaim;
56 1
                    $event->addRemoteClaim($remoteClaim);
57
                } catch (\Exception $e) {
58
                    $this->error(
59 1
                        "Error retrieving remote claim {$scope}: {$e->getMessage()}",
60
                        ['exception' => $e]
61 1
                    );
62 1
                }
63 1
            }
64
        }
65
    }
66
67 1
    private function checkHttpUri($uri)
0 ignored issues
show
Coding Style introduced by
function checkHttpUri() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
68
    {
69
        try {
70 1
            $http = HttpUri::createFromString($uri);
71
72 1
            return $http->getScheme() && $http->getHost();
73 1
        } catch (\Exception $e) {
74 1
            return false;
75
        }
76
    }
77
78
    private function checkTagUri($uri)
0 ignored issues
show
Coding Style introduced by
function checkTagUri() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
79
    {
80
        try {
81
            TagUri::createFromString($uri);
82
83
            return true;
84
        } catch (\Exception $e) {
85
            return false;
86
        }
87
    }
88
}
89