Failed Conditions
Push — issue#767 ( 20b3b1 )
by Guilherme
10:48
created

ClientManager   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getClientById() 0 23 3
B sanitizeClient() 0 17 5
B register() 0 36 5
A __construct() 0 10 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\OpenIDBundle\Manager;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\PersonRepository;
15
use LoginCidadao\CoreBundle\Event\GetClientEvent;
16
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use LoginCidadao\OAuthBundle\Model\ClientInterface;
19
use LoginCidadao\OpenIDBundle\Entity\ClientMetadata;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
22
class ClientManager
23
{
24
    /** @var EventDispatcherInterface */
25
    private $dispatcher;
26
27
    /** @var EntityManagerInterface */
28
    private $em;
29
30
    /** @var PersonRepository */
31
    private $personRepository;
32
33
    /** @var string */
34
    private $publicScopes;
35
36
    /**
37
     * ClientManager constructor.
38
     * @param EntityManagerInterface $em
39
     * @param EventDispatcherInterface $dispatcher
40
     * @param PersonRepository $personRepository
41
     * @param $publicScopes
42
     */
43 6
    public function __construct(
44
        EntityManagerInterface $em,
45
        EventDispatcherInterface $dispatcher,
46
        PersonRepository $personRepository,
47
        $publicScopes
48
    ) {
49 6
        $this->em = $em;
50 6
        $this->dispatcher = $dispatcher;
51 6
        $this->personRepository = $personRepository;
52 6
        $this->publicScopes = $publicScopes;
53 6
    }
54
55 2
    public function getClientById($id)
56
    {
57 2
        $randomId = null;
58 2
        if (strstr($id, '_') !== false) {
59 1
            $parts = explode('_', $id);
60 1
            $id = $parts[0];
61 1
            $randomId = $parts[1];
62
        }
63
64 2
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client');
65
66 2
        if ($randomId) {
67 1
            $client = $repo->findOneBy([
68 1
                'id' => $id,
69 1
                'randomId' => $randomId,
70
            ]);
71
        } else {
72 1
            $client = $repo->find($id);
73
        }
74 2
        $event = new GetClientEvent($client);
75 2
        $this->dispatcher->dispatch(LoginCidadaoCoreEvents::GET_CLIENT, $event);
76
77 2
        return $event->getClient();
78
    }
79
80 4
    public function register(ClientMetadata $data)
81
    {
82 4
        if ($data->getClient() === null) {
83 3
            $client = $data->toClient();
84
        } else {
85 1
            $client = $data->getClient();
86
        }
87
88 4
        $client = $this->sanitizeClient($client);
89
90 4
        if (count($data->getContacts()) > 0) {
91
            /** @var PersonInterface[] $owners */
92 1
            $owners = $this->personRepository->findBy([
93 1
                'email' => $data->getContacts(),
94
            ]);
95
96 1
            foreach ($owners as $person) {
97 1
                if (!$person->getEmailConfirmedAt() instanceof \DateTime) {
98
                    // Email is not verified. Skipping...
99 1
                    continue;
100
                }
101 1
                $client->getOwners()->add($person);
102
            }
103
        }
104
105 4
        $publicScopes = explode(' ', $this->publicScopes);
106 4
        $client->setAllowedScopes($publicScopes);
107
108 4
        $this->em->persist($client);
109
110 4
        $data->setClient($client);
111 4
        $this->em->persist($data);
112
113 4
        $this->em->flush();
114
115 4
        return $client;
116
    }
117
118 4
    private function sanitizeClient(ClientInterface $client)
119
    {
120 4
        if ($client->getName() === null) {
121 4
            $firstUrl = parse_url($client->getRedirectUris()[0], PHP_URL_HOST);
122 4
            $client->setName($firstUrl);
123
        }
124 4
        if ($client->getDescription() === null) {
125 4
            $client->setDescription('');
126
        }
127 4
        if ($client->getTermsOfUseUrl() === null) {
128 4
            $client->setTermsOfUseUrl('');
129
        }
130 4
        if ($client->getSiteUrl() === null) {
131 4
            $client->setSiteUrl('');
132
        }
133
134 4
        return $client;
135
    }
136
}
137