Failed Conditions
Push — issue#767 ( b130a3...4d8d6b )
by Guilherme
11:45 queued 06:37
created

SectorIdentifierUriValidator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
67
    {
68
        $this->context->buildViolation($message)
69
            ->atPath('sector_identifier_uri')
70
            ->addViolation();
71
    }
72
}
73