Failed Conditions
Push — issue#767 ( d652de...c7fd2d )
by Guilherme
05:20
created

SectorIdentifierUriValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
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\OpenIDBundle\Validator\Constraints;
12
13
use LoginCidadao\OAuthBundle\Entity\Organization;
14
use LoginCidadao\OAuthBundle\Entity\OrganizationRepository;
15
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
16
use LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker;
17
use Symfony\Component\Validator\Constraint;
18
use Symfony\Component\Validator\ConstraintValidator;
19
20
class SectorIdentifierUriValidator extends ConstraintValidator
21
{
22
23
    /** @var OrganizationRepository */
24
    private $orgRepo;
25
26
    /** @var SectorIdentifierUriChecker */
27
    private $uriChecker;
28
29
    /**
30
     * SectorIdentifierUriValidator constructor.
31
     * @param OrganizationRepository $orgRepo
32
     * @param SectorIdentifierUriChecker $uriChecker
33
     */
34 3
    public function __construct(OrganizationRepository $orgRepo, SectorIdentifierUriChecker $uriChecker)
35
    {
36 3
        $this->orgRepo = $orgRepo;
37 3
        $this->uriChecker = $uriChecker;
38 3
    }
39
40
    /**
41
     * @param ClientMetadata $metadata
42
     * @param Constraint $constraint
43
     */
44 3
    public function validate($metadata, Constraint $constraint)
45
    {
46 3
        if (!$metadata->getSectorIdentifierUri()) {
47 1
            return;
48
        }
49
50 2
        $sectorIdentifierUri = $metadata->getSectorIdentifierUri();
51
52
        /** @var Organization $organization */
53 2
        $organization = $this->orgRepo->findOneBy(compact('sectorIdentifierUri'));
54
55 2
        $success = $this->uriChecker->check($metadata, $sectorIdentifierUri);
56 2
        if (!$success) {
57 1
            $metadata->setOrganization(null);
58
        }
59
60 2
        if ($success && $organization instanceof Organization) {
61 1
            $metadata->setOrganization($organization);
62
        }
63 2
    }
64
}
65