Failed Conditions
Push — issue#768 ( 76d4a9 )
by Guilherme
11:58
created

ClientRepository::getOwnedByPersonQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace LoginCidadao\OAuthBundle\Entity;
4
5
use Doctrine\ORM\EntityRepository;
6
use LoginCidadao\CoreBundle\Model\PersonInterface;
7
use LoginCidadao\OAuthBundle\Model\ClientInterface;
8
9
class ClientRepository extends EntityRepository
10
{
11
12
    /**
13
     * @param PersonInterface $person
14
     * @param $id
15
     * @return ClientInterface|Client|null
16
     * @throws \Doctrine\ORM\NonUniqueResultException
17
     */
18
    public function findOneOwned(PersonInterface $person, $id)
19
    {
20
        return $this->createQueryBuilder('c')
21
            ->where(':person MEMBER OF c.owners')
22
            ->andWhere('c.id = :id ')
23
            ->setParameters(compact('person', 'id'))
24
            ->getQuery()
25
            ->getOneOrNullResult();
26
    }
27
28
    public function getCountPerson(
29
        PersonInterface $person = null,
30
        $clientId = null
31
    ) {
32
        $qb = $this->getEntityManager()->createQueryBuilder()
33
            ->select('count(a.id) AS qty, c.id AS client')
34
            ->from('LoginCidadaoCoreBundle:Authorization', 'a')
35
            ->innerJoin(
36
                'LoginCidadaoOAuthBundle:Client',
37
                'c',
38
                'WITH',
39
                'a.client = c'
40
            )
41
            ->where('c.published = true')
42
            ->groupBy('a.client, c.id')
43
            ->orderBy('qty', 'DESC');
44
45
        if ($person !== null) {
46
            $clients = $this->getEntityManager()->createQueryBuilder()
47
                ->select('IDENTITY(a.client)')
48
                ->from('LoginCidadaoCoreBundle:Authorization', 'a')
49
                ->where('a.person = :person')
50
                ->setParameter('person', $person)
51
                ->getQuery()->getScalarResult();
52
53
            $qb->orWhere('a.id IN (:clients)')
54
                ->setParameter('clients', $clients);
55
        }
56
57
        if ($clientId !== null) {
58
            $qb->andWhere('c.id = :clientId')
59
                ->setParameter('clientId', $clientId);
60
        }
61
62
        $result = $qb->getQuery()->getResult();
63
64
        return $this->injectObject($result, 'client');
65
    }
66
67
    public function statsUsersByServiceByDay(
68
        $days,
69
        $clientId = null,
70
        PersonInterface $person = null
71
    ) {
72
        $date = new \DateTime("-$days days");
73
74
        $query = $this->createQueryBuilder('c')
75
            ->select('DATE(a.createdAt) AS day, c.id AS client, COUNT(a.id) AS users')
76
            ->join('c.authorizations', 'a')
77
            ->where('a.createdAt >= :date')
78
            ->andWhere('c.published = true')
79
            ->groupBy('day, client')
80
            ->orderBy('day')
81
            ->setParameter('date', $date);
82
83
        if ($clientId !== null) {
84
            $query
85
                ->andWhere('a.client = :clientId')
86
                ->setParameter('clientId', $clientId);
87
        }
88
89
        if ($person !== null) {
90
            $clients = $this->getEntityManager()->createQueryBuilder()
91
                ->select('c.id')
92
                ->from($this->getEntityName(), 'c', 'c.id')
93
                ->join('c.authorizations', 'a')
94
                ->where('a.person = :person')
95
                ->setParameters(compact('person'))
96
                ->getQuery()->getArrayResult();
97
98
            $ids = array_keys($clients);
99
            $query->orWhere(
100
                $query->expr()
101
                    ->andX('a.createdAt >= :date', 'c.id IN (:clients)')
102
            )->setParameter('clients', $ids);
103
        }
104
105
        return $query->getQuery()->getScalarResult();
106
    }
107
108
    private function injectObject(array $items = [], $idKey)
0 ignored issues
show
Coding Style introduced by
Parameters which have default values should be placed at the end.

If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway:

// $a must always be passed; it's default value is never used.
function someFunction($a = 5, $b) { }
Loading history...
109
    {
110
        $ids = [];
111
        foreach ($items as $item) {
112
            $ids[] = $item[$idKey];
113
        }
114
115
        $clients = $this->findBy(['id' => $ids]);
116
        $indexedClients = [];
117
        foreach ($clients as $client) {
118
            $indexedClients[$client->getId()] = $client;
119
        }
120
121
        return array_map(
122
            function ($item) use ($idKey, $indexedClients) {
123
                $id = $item[$idKey];
124
                $item[$idKey] = $indexedClients[$id];
125
126
                return $item;
127
            },
128
            $items
129
        );
130
    }
131
132
    /**
133
     * @return mixed
134
     * @throws \Doctrine\ORM\NoResultException
135
     * @throws \Doctrine\ORM\NonUniqueResultException
136
     */
137
    public function countClients()
138
    {
139
        return $this->createQueryBuilder('c')
140
            ->select('COUNT(c)')
141
            ->getQuery()->getSingleScalarResult();
142
    }
143
144
    public function getAccessTokenAccounting(\DateTime $start, \DateTime $end)
145
    {
146
        $query = $this->createQueryBuilder('c')
147
            ->select('c.id, COUNT(a) AS access_tokens')
148
            ->leftJoin('LoginCidadaoOAuthBundle:AccessToken', 'a', 'WITH', 'a.client = c')
149
            ->where('a.createdAt BETWEEN :start AND :end')
150
            ->orWhere('a.id IS NULL')
151
            ->groupBy('c.id')
152
            ->setParameters(compact('start', 'end'));
153
154
        return $query->getQuery()->getScalarResult();
155
    }
156
157
    public function getActionLogAccounting(\DateTime $start, \DateTime $end)
158
    {
159
        $query = $this->createQueryBuilder('c')
160
            ->select('c.id, COUNT(a) AS api_usage')
161
            ->leftJoin('LoginCidadaoAPIBundle:ActionLog', 'a', 'WITH', 'a.clientId = c.id')
162
            ->where('a.createdAt BETWEEN :start AND :end')
163
            ->orWhere('a.id IS NULL')
164
            ->groupBy('c.id')
165
            ->setParameters(compact('start', 'end'));
166
167
        return $query->getQuery()->getScalarResult();
168
    }
169
170
    public function getOwnedByPersonQuery(PersonInterface $person)
171
    {
172
        return $this->createQueryBuilder('c')
173
            ->where(':person MEMBER OF c.owners')
174
            ->setParameter('person', $person)
175
            ->addOrderBy('c.id', 'desc');
176
    }
177
}
178