Failed Conditions
Push — issue#702 ( 91bd46...0b5bf0 )
by Guilherme
19:37 queued 12:15
created

OrganizationService::getOrganization()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 4
nop 1
dl 0
loc 26
ccs 0
cts 22
cp 0
crap 42
rs 8.439
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\OAuthBundle\Service;
12
13
use LoginCidadao\OAuthBundle\Entity\Organization;
14
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
15
use LoginCidadao\OpenIDBundle\Validator\SectorIdentifierUriChecker;
16
use Symfony\Component\HttpKernel\Exception\HttpException;
17
18
class OrganizationService
19
{
20
    /** @var SectorIdentifierUriChecker */
21
    private $sectorIdentifierChecker;
22
23
    /**
24
     * OrganizationService constructor.
25
     * @param SectorIdentifierUriChecker $sectorIdentifierChecker
26
     */
27
    public function __construct(SectorIdentifierUriChecker $sectorIdentifierChecker)
28
    {
29
        $this->sectorIdentifierChecker = $sectorIdentifierChecker;
30
    }
31
32
    public function getOrganization(ClientMetadata $metadata = null)
33
    {
34
        if ($metadata === null) {
35
            return null;
36
        }
37
38
        if ($metadata->getOrganization() === null && $metadata->getSectorIdentifierUri()) {
39
            $sectorIdentifierUri = $metadata->getSectorIdentifierUri();
40
            try {
41
                $verified = $this->sectorIdentifierChecker->check($metadata, $sectorIdentifierUri);
42
            } catch (HttpException $e) {
43
                $verified = false;
44
            }
45
            $uri = parse_url($sectorIdentifierUri);
46
            $domain = $uri['host'];
47
48
            $organization = new Organization();
49
            $organization->setDomain($domain)
50
                ->setName($domain)
51
                ->setTrusted(false)
52
                ->setVerifiedAt($verified ? new \DateTime() : null);
53
54
            return $organization;
55
        }
56
57
        return $metadata->getOrganization();
58
    }
59
}
60