|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LoginCidadao\OAuthBundle\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
|
6
|
|
|
use LoginCidadao\CoreBundle\Model\PersonInterface; |
|
7
|
|
|
|
|
8
|
|
|
class ClientRepository extends EntityRepository |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
public function findOneOwned(PersonInterface $person, $id) |
|
12
|
|
|
{ |
|
13
|
|
|
return $this->createQueryBuilder('c') |
|
14
|
|
|
->where(':person MEMBER OF c.owners') |
|
15
|
|
|
->andWhere('c.id = :id ') |
|
16
|
|
|
->setParameters(compact('person', 'id')) |
|
17
|
|
|
->getQuery() |
|
18
|
|
|
->getOneOrNullResult(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public function getCountPerson( |
|
22
|
|
|
PersonInterface $person = null, |
|
23
|
|
|
$clientId = null |
|
24
|
|
|
) { |
|
25
|
|
|
$qb = $this->getEntityManager()->createQueryBuilder() |
|
26
|
|
|
->select('count(a.id) AS qty, c.id AS client') |
|
27
|
|
|
->from('LoginCidadaoCoreBundle:Authorization', 'a') |
|
28
|
|
|
->innerJoin( |
|
29
|
|
|
'LoginCidadaoOAuthBundle:Client', |
|
30
|
|
|
'c', |
|
31
|
|
|
'WITH', |
|
32
|
|
|
'a.client = c' |
|
33
|
|
|
) |
|
34
|
|
|
->where('c.published = true') |
|
35
|
|
|
->groupBy('a.client, c.id') |
|
36
|
|
|
->orderBy('qty', 'DESC'); |
|
37
|
|
|
|
|
38
|
|
|
if ($person !== null) { |
|
39
|
|
|
$clients = $this->getEntityManager()->createQueryBuilder() |
|
40
|
|
|
->select('IDENTITY(a.client)') |
|
41
|
|
|
->from('LoginCidadaoCoreBundle:Authorization', 'a') |
|
42
|
|
|
->where('a.person = :person') |
|
43
|
|
|
->setParameter('person', $person) |
|
44
|
|
|
->getQuery()->getScalarResult(); |
|
45
|
|
|
|
|
46
|
|
|
$qb->orWhere('a.id IN (:clients)') |
|
47
|
|
|
->setParameter('clients', $clients); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
if ($clientId !== null) { |
|
51
|
|
|
$qb->andWhere('c.id = :clientId') |
|
52
|
|
|
->setParameter('clientId', $clientId); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$result = $qb->getQuery()->getResult(); |
|
56
|
|
|
|
|
57
|
|
|
return $this->injectObject($result, 'client'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function statsUsersByServiceByDay( |
|
61
|
|
|
$days, |
|
62
|
|
|
$clientId = null, |
|
63
|
|
|
PersonInterface $person = null |
|
64
|
|
|
) { |
|
65
|
|
|
$date = new \DateTime("-$days days"); |
|
66
|
|
|
|
|
67
|
|
|
$query = $this->createQueryBuilder('c') |
|
68
|
|
|
->select('DATE(a.createdAt) AS day, c.id AS client, COUNT(a.id) AS users') |
|
69
|
|
|
->join('c.authorizations', 'a') |
|
70
|
|
|
->where('a.createdAt >= :date') |
|
71
|
|
|
->andWhere('c.published = true') |
|
72
|
|
|
->groupBy('day, client') |
|
73
|
|
|
->orderBy('day') |
|
74
|
|
|
->setParameter('date', $date); |
|
75
|
|
|
|
|
76
|
|
|
if ($clientId !== null) { |
|
77
|
|
|
$query |
|
78
|
|
|
->andWhere('a.client = :clientId') |
|
79
|
|
|
->setParameter('clientId', $clientId); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($person !== null) { |
|
83
|
|
|
$clients = $this->getEntityManager()->createQueryBuilder() |
|
84
|
|
|
->select('c.id') |
|
85
|
|
|
->from($this->getEntityName(), 'c', 'c.id') |
|
86
|
|
|
->join('c.authorizations', 'a') |
|
87
|
|
|
->where('a.person = :person') |
|
88
|
|
|
->setParameters(compact('person')) |
|
89
|
|
|
->getQuery()->getArrayResult(); |
|
90
|
|
|
|
|
91
|
|
|
$ids = array_keys($clients); |
|
92
|
|
|
$query->orWhere( |
|
93
|
|
|
$query->expr() |
|
94
|
|
|
->andX('a.createdAt >= :date', 'c.id IN (:clients)') |
|
95
|
|
|
)->setParameter('clients', $ids); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $query->getQuery()->getScalarResult(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private function injectObject(array $items = [], $idKey) |
|
|
|
|
|
|
102
|
|
|
{ |
|
103
|
|
|
$ids = []; |
|
104
|
|
|
foreach ($items as $item) { |
|
105
|
|
|
$ids[] = $item[$idKey]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$clients = $this->findBy(['id' => $ids]); |
|
109
|
|
|
$indexedClients = []; |
|
110
|
|
|
foreach ($clients as $client) { |
|
111
|
|
|
$indexedClients[$client->getId()] = $client; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return array_map( |
|
115
|
|
|
function ($item) use ($idKey, $indexedClients) { |
|
116
|
|
|
$id = $item[$idKey]; |
|
117
|
|
|
$item[$idKey] = $indexedClients[$id]; |
|
118
|
|
|
|
|
119
|
|
|
return $item; |
|
120
|
|
|
}, |
|
121
|
|
|
$items |
|
122
|
|
|
); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
public function countClients() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->createQueryBuilder('c') |
|
131
|
|
|
->select('COUNT(c)') |
|
132
|
|
|
->getQuery()->getSingleScalarResult(); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
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: