Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

Repository/AnalyticsConfigRepository.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\DashboardBundle\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
7
8
/**
9
 * AnalyticsConfigRepository
10
 *
11
 * This class was generated by the Doctrine ORM. Add your own custom
12
 * repository methods below.
13
 */
14
class AnalyticsConfigRepository extends EntityRepository
15
{
16
    /**
17
     * Get the first config from the database, creates a new entry if the config doesn't exist yet
18
     *
19
     * @return AnalyticsConfig $config
20
     */
21
    public function findFirst($createNew = true)
22
    {
23
        // Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle
24
        $em = $this->getEntityManager();
25
        $query = $em->createQuery('SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c');
26
        $result = $query->getResult();
27
        // if no configs exist, create a new one
28
        if (!$result && $createNew) {
29
            return $this->createConfig();
30
        } elseif ($result) {
31
            return $result[0];
32
        } else {
33
            return false;
34
        }
35
    }
36
37
    /**
38
     * Get the default overviews for a config
39
     *
40
     * @return AnalyticsConfig $config
41
     */
42
    public function findDefaultOverviews($config)
43
    {
44
        $em = $this->getEntityManager();
45
46
        return $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')
47
            ->findBy(array(
48
                    'config' => $config,
49
                    'segment' => null,
50
                ));
51
    }
52
53
    /**
54
     * Create a new config
55
     *
56
     * @return AnalyticsConfig
57
     */
58
    public function createConfig()
59
    {
60
        $em = $this->getEntityManager();
61
62
        $config = new AnalyticsConfig();
63
        $em->persist($config);
64
        $em->flush();
65
66
        $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->addOverviews($config);
67
68
        return $config;
69
    }
70
71
    /**
72
     * Flush a config
73
     *
74
     * @param int $id the config id
75
     */
76
    public function flushConfig($id = false)
77
    {
78
        $em = $this->getEntityManager();
79
80
        // Backward compatibilty to flush overviews without a config set
81
        if (!$id) {
82
            $overviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
83
            foreach ($overviewRepository->findAll() as $overview) {
84
                $em->remove($overview);
85
            }
86
            $em->flush();
87
88
            return;
89
        }
90
91
        $config = $id ? $this->find($id) : $this->findFirst();
92
        foreach ($config->getOverviews() as $overview) {
93
            $em->remove($overview);
94
        }
95
        foreach ($config->getSegments() as $segment) {
96
            $em->remove($segment);
97
        }
98
        $em->flush();
99
    }
100
101
    /**
102
     * Update the timestamp when data is collected
103
     *
104
     * @param int id
105
     */
106
    public function setUpdated($id = false)
107
    {
108
        $em = $this->getEntityManager();
109
        $config = $id ? $this->find($id) : $this->findFirst();
110
        $config->setLastUpdate(new \DateTime());
111
        $em->persist($config);
112
        $em->flush();
113
    }
114
115
    /**
116
     * saves the token
117
     *
118
     * @param string $token
119
     */
120
    public function saveToken($token, $id = false)
121
    {
122
        $em = $this->getEntityManager();
123
        $config = $id ? $this->find($id) : $this->findFirst();
124
        $config->setToken($token);
125
        $em->persist($config);
126
        $em->flush();
127
    }
128
129
    /**
130
     * saves the property id
131
     *
132
     * @param string $propertyId
133
     */
134
    public function savePropertyId($propertyId, $id = false)
135
    {
136
        $em = $this->getEntityManager();
137
        $config = $id ? $this->find($id) : $this->findFirst();
138
        $config->setPropertyId($propertyId);
139
        $em->persist($config);
140
        $em->flush();
141
    }
142
143
    /**
144
     * saves the account id
145
     *
146
     * @param string $accountId
147
     */
148
    public function saveAccountId($accountId, $id = false)
149
    {
150
        $em = $this->getEntityManager();
151
        $config = $id ? $this->find($id) : $this->findFirst();
152
        $config->setAccountId($accountId);
153
        $em->persist($config);
154
        $em->flush();
155
    }
156
157
    /**
158
     * saves the profile id
159
     *
160
     * @param string $profileId
161
     */
162 View Code Duplication
    public function saveProfileId($profileId, $id = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
163
    {
164
        $em = $this->getEntityManager();
165
        $config = $id ? $this->find($id) : $this->findFirst();
166
        $config->setProfileId($profileId);
167
        $em->persist($config);
168
        $em->flush();
169
    }
170
171
    /**
172
     * saves the config name
173
     *
174
     * @param string $profileId
175
     */
176 View Code Duplication
    public function saveConfigName($name, $id = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $em = $this->getEntityManager();
179
        $config = $id ? $this->find($id) : $this->findFirst();
180
        $config->setName($name);
181
        $em->persist($config);
182
        $em->flush();
183
    }
184
185
    /**
186
     * Resets the profile id
187
     *
188
     * @param int id
189
     */
190 View Code Duplication
    public function resetProfileId($id = false)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
191
    {
192
        $em = $this->getEntityManager();
193
        $config = $id ? $this->find($id) : $this->findFirst();
194
        $config->setProfileId('');
195
        $em->persist($config);
196
        $em->flush();
197
    }
198
199
    /**
200
     * Resets the  account id, property id and profile id
201
     *
202
     * @param int id
203
     */
204
    public function resetPropertyId($id = false)
205
    {
206
        $em = $this->getEntityManager();
207
        $config = $id ? $this->find($id) : $this->findFirst();
208
        $config->setAccountId('');
209
        $config->setProfileId('');
210
        $config->setPropertyId('');
211
        $em->persist($config);
212
        $em->flush();
213
    }
214
}
215