Completed
Push — 2.x ( aeb5ce...f5bd0f )
by Jindřich
03:11
created

DoctrineUserConnector::getUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace SkautisBundle\Security\Authentication\Connector;
4
5
use Doctrine\ORM\EntityManager;
6
use SkautisBundle\Entity\DoctrineUserConnection;
7
use SkautisBundle\Security\Authentication\Connector\SkautisUserConnectorInterface;
8
9
/**
10
 * Trida pro propojeni symfony uzivatele se Skautis uzivatelem, ukladajici data pomoci Doctrine ORM
11
 */
12
class DoctrineUserConnector implements SkautisUserConnectorInterface
13
{
14
15
    /**
16
     * @var EntityManager
17
     */
18
    protected $em;
19
20
    public function __construct(EntityManager $em) {
21
        $this->em = $em;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function getUsername($personId) {
28
        /**
29
         * @var DoctrineUserConnection
30
         */
31
        $connection = $this->em->find('SkautisBundle\Entity\DoctrineUserConnection', $personId);
32
33
        if (!$connection) {
34
            return "";
35
        }
36
37
        return $connection->getUsername();
38
    }
39
40
    /**
41
     * @inheritdoc
42
     */
43
    public function getPersonId($username) {
44
        $connection = $this->findConnectionByUsername($username);
45
46
        if (!$connection) {
47
            return "";
48
        }
49
50
        return $connection->getUsername();
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function connect($personId, $username) {
57
58
        $queryBuilder = $this->em->createQueryBuilder()
59
            ->select('C')
60
            ->from('SkautisBundle\Entity\DoctrineUserConnection', 'C')
61
            ->where('C.username = :username')
62
            ->orWhere('C.personId = :person_id')
63
            ->setParameter('username', $username)
64
            ->setParameter('person_id', $personId);
65
66
        $result = $queryBuilder->getQuery()->getResult();
67
68
        if (sizeof($result) > 0) {
69
            return; //@TODO exception?
70
        }
71
72
73
        $connection = new DoctrineUserConnection();
74
        $connection->setPersonId($personId);
75
        $connection->setUsername($username);
76
        $this->em->persist($connection);
77
        $this->em->flush();
78
    }
79
80
    /**
81
     * @inheritdoc
82
     */
83
    public function disconnect($username) {
84
        $connection = $this->findConnectionByUsername($username);
85
86
        if (!$connection) {
87
            return; //@TODO exception?
88
        }
89
90
        $this->em->remove($connection);
91
        $this->em->flush();
92
    }
93
94
    /**
95
     * @param string $username
96
     * @return DoctrineUserConnection|null
97
     */
98
    protected function findConnectionByUsername($username) {
99
        $queryBuilder = $this->em->createQueryBuilder()
100
            ->select('C')
101
            ->from('SkautisBundle\Entity\DoctrineUserConnection', 'C')
102
            ->where('C.username = :username')
103
            ->setParameter('username', $username);
104
105
        $result = $queryBuilder->getQuery()->getResult();
106
107
        if (sizeof($result) < 1) {
108
            return null;
109
        }
110
111
        return $result[0];
112
    }
113
}
114