Completed
Push — issue#767 ( 0f6294...09d8c8 )
by Guilherme
07:16
created

ClientManager   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 98.31%

Importance

Changes 0
Metric Value
dl 0
loc 132
ccs 58
cts 59
cp 0.9831
rs 10
c 0
b 0
f 0
wmc 17

5 Methods

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