1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\DashboardBundle\Repository; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityRepository; |
6
|
|
|
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig; |
7
|
|
|
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* AnalyticsConfigRepository |
11
|
|
|
* |
12
|
|
|
* This class was generated by the Doctrine ORM. Add your own custom |
13
|
|
|
* repository methods below. |
14
|
|
|
*/ |
15
|
|
|
class AnalyticsConfigRepository extends EntityRepository |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Get the first config from the database, creates a new entry if the config doesn't exist yet |
19
|
|
|
* |
20
|
|
|
* @return AnalyticsConfig $config |
21
|
|
|
*/ |
22
|
|
|
public function findFirst($createNew = true) |
23
|
|
|
{ |
24
|
|
|
// Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle |
25
|
|
|
$em = $this->getEntityManager(); |
26
|
|
|
$query = $em->createQuery('SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c'); |
27
|
|
|
$result = $query->getResult(); |
28
|
|
|
// if no configs exist, create a new one |
29
|
|
|
if (!$result && $createNew) { |
30
|
|
|
return $this->createConfig(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if ($result) { |
34
|
|
|
return $result[0]; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the default overviews for a config |
42
|
|
|
* |
43
|
|
|
* @return AnalyticsConfig $config |
|
|
|
|
44
|
|
|
*/ |
45
|
|
|
public function findDefaultOverviews($config) |
46
|
|
|
{ |
47
|
|
|
$em = $this->getEntityManager(); |
48
|
|
|
|
49
|
|
|
return $em->getRepository(AnalyticsOverview::class) |
50
|
|
|
->findBy(array( |
51
|
|
|
'config' => $config, |
52
|
|
|
'segment' => null, |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Create a new config |
58
|
|
|
* |
59
|
|
|
* @return AnalyticsConfig |
60
|
|
|
*/ |
61
|
|
|
public function createConfig() |
62
|
|
|
{ |
63
|
|
|
$em = $this->getEntityManager(); |
64
|
|
|
|
65
|
|
|
$config = new AnalyticsConfig(); |
66
|
|
|
$em->persist($config); |
67
|
|
|
$em->flush(); |
68
|
|
|
|
69
|
|
|
$this->getEntityManager()->getRepository(AnalyticsOverview::class)->addOverviews($config); |
|
|
|
|
70
|
|
|
|
71
|
|
|
return $config; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Flush a config |
76
|
|
|
* |
77
|
|
|
* @param int $id the config id |
|
|
|
|
78
|
|
|
*/ |
79
|
|
|
public function flushConfig($id = false) |
80
|
|
|
{ |
81
|
|
|
$em = $this->getEntityManager(); |
82
|
|
|
|
83
|
|
|
// Backward compatibilty to flush overviews without a config set |
84
|
|
|
if (!$id) { |
|
|
|
|
85
|
|
|
$overviewRepository = $em->getRepository(AnalyticsOverview::class); |
86
|
|
|
foreach ($overviewRepository->findAll() as $overview) { |
87
|
|
|
$em->remove($overview); |
88
|
|
|
} |
89
|
|
|
$em->flush(); |
90
|
|
|
|
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
95
|
|
|
foreach ($config->getOverviews() as $overview) { |
96
|
|
|
$em->remove($overview); |
97
|
|
|
} |
98
|
|
|
foreach ($config->getSegments() as $segment) { |
99
|
|
|
$em->remove($segment); |
100
|
|
|
} |
101
|
|
|
$em->flush(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Update the timestamp when data is collected |
106
|
|
|
* |
107
|
|
|
* @param int id |
108
|
|
|
*/ |
109
|
|
|
public function setUpdated($id = false) |
110
|
|
|
{ |
111
|
|
|
$em = $this->getEntityManager(); |
112
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
113
|
|
|
$config->setLastUpdate(new \DateTime()); |
114
|
|
|
$em->persist($config); |
115
|
|
|
$em->flush(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* saves the token |
120
|
|
|
* |
121
|
|
|
* @param string $token |
122
|
|
|
*/ |
123
|
|
|
public function saveToken($token, $id = false) |
124
|
|
|
{ |
125
|
|
|
$em = $this->getEntityManager(); |
126
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
127
|
|
|
$config->setToken($token); |
128
|
|
|
$em->persist($config); |
129
|
|
|
$em->flush(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* saves the property id |
134
|
|
|
* |
135
|
|
|
* @param string $propertyId |
136
|
|
|
*/ |
137
|
|
|
public function savePropertyId($propertyId, $id = false) |
138
|
|
|
{ |
139
|
|
|
$em = $this->getEntityManager(); |
140
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
141
|
|
|
$config->setPropertyId($propertyId); |
142
|
|
|
$em->persist($config); |
143
|
|
|
$em->flush(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* saves the account id |
148
|
|
|
* |
149
|
|
|
* @param string $accountId |
150
|
|
|
*/ |
151
|
|
|
public function saveAccountId($accountId, $id = false) |
152
|
|
|
{ |
153
|
|
|
$em = $this->getEntityManager(); |
154
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
155
|
|
|
$config->setAccountId($accountId); |
156
|
|
|
$em->persist($config); |
157
|
|
|
$em->flush(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* saves the profile id |
162
|
|
|
* |
163
|
|
|
* @param string $profileId |
164
|
|
|
*/ |
165
|
|
View Code Duplication |
public function saveProfileId($profileId, $id = false) |
|
|
|
|
166
|
|
|
{ |
167
|
|
|
$em = $this->getEntityManager(); |
168
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
169
|
|
|
$config->setProfileId($profileId); |
170
|
|
|
$em->persist($config); |
171
|
|
|
$em->flush(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* saves the config name |
176
|
|
|
* |
177
|
|
|
* @param string $profileId |
|
|
|
|
178
|
|
|
*/ |
179
|
|
View Code Duplication |
public function saveConfigName($name, $id = false) |
|
|
|
|
180
|
|
|
{ |
181
|
|
|
$em = $this->getEntityManager(); |
182
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
183
|
|
|
$config->setName($name); |
184
|
|
|
$em->persist($config); |
185
|
|
|
$em->flush(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Resets the profile id |
190
|
|
|
* |
191
|
|
|
* @param int id |
192
|
|
|
*/ |
193
|
|
View Code Duplication |
public function resetProfileId($id = false) |
|
|
|
|
194
|
|
|
{ |
195
|
|
|
$em = $this->getEntityManager(); |
196
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
197
|
|
|
$config->setProfileId(''); |
198
|
|
|
$em->persist($config); |
199
|
|
|
$em->flush(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Resets the account id, property id and profile id |
204
|
|
|
* |
205
|
|
|
* @param int id |
206
|
|
|
*/ |
207
|
|
|
public function resetPropertyId($id = false) |
208
|
|
|
{ |
209
|
|
|
$em = $this->getEntityManager(); |
210
|
|
|
$config = $id ? $this->find($id) : $this->findFirst(); |
211
|
|
|
$config->setAccountId(''); |
212
|
|
|
$config->setProfileId(''); |
213
|
|
|
$config->setPropertyId(''); |
214
|
|
|
$em->persist($config); |
215
|
|
|
$em->flush(); |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.