Failed Conditions
Push — issue#764 ( 39afc8 )
by Guilherme
17:45 queued 08:27
created

ClientManager::getClientById()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 4
nop 1
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 3
rs 9.0856
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\Manager;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Event\GetClientEvent;
15
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
17
18
class ClientManager
19
{
20
    /** @var EventDispatcherInterface */
21
    protected $dispatcher;
22
23
    /** @var EntityManagerInterface */
24
    private $em;
25
26 2
    public function __construct(
27
        EntityManagerInterface $em,
28
        EventDispatcherInterface $dispatcher
29
    ) {
30 2
        $this->em = $em;
31 2
        $this->dispatcher = $dispatcher;
32 2
    }
33
34 2
    public function getClientById($id)
35
    {
36 2
        $randomId = null;
37 2
        if (strstr($id, '_') !== false) {
38 1
            $parts = explode('_', $id);
39 1
            $id = $parts[0];
40 1
            $randomId = $parts[1];
41
        }
42
43 2
        $repo = $this->em->getRepository('LoginCidadaoOAuthBundle:Client');
44
45 2
        if ($randomId) {
46 1
            $client = $repo->findOneBy([
47 1
                'id' => $id,
48 1
                'randomId' => $randomId,
49
            ]);
50
        } else {
51 1
            $client = $repo->find($id);
52
        }
53 2
        $event = new GetClientEvent($client);
54 2
        $this->dispatcher->dispatch(LoginCidadaoCoreEvents::GET_CLIENT, $event);
55
56 2
        return $event->getClient();
57
    }
58
}
59