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

OrganizationService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 0
cts 26
cp 0
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getOrganization() 0 26 6
A __construct() 0 3 1
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