Completed
Push — 5.0 ( a48099...63af02 )
by
unknown
11:33
created

Repository/AnalyticsConfigRepository.php (1 issue)

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
        // Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle
23
        $em = $this->getEntityManager();
24
        $query = $em->createQuery( 'SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c' );
25
        $result = $query->getResult();
26
        // if no configs exist, create a new one
27
        if (!$result && $createNew) {
28
            return $this->createConfig();
29
        } else if ($result) {
30
           return $result[0];
31
        } else {
32
            return false;
33
        }
34
    }
35
36
    /**
37
     * Get the default overviews for a config
38
     *
39
     * @return AnalyticsConfig $config
0 ignored issues
show
Should the return type not be array? Also, consider making the array more specific, something like array<String>, or String[].

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.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
40
     */
41
    public function findDefaultOverviews($config) {
42
        $em = $this->getEntityManager();
43
        return $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')
44
            ->findBy(array(
45
                    'config' => $config,
46
                    'segment' => null
47
                ));
48
    }
49
50
    /**
51
     * Create a new config
52
     *
53
     * @return AnalyticsConfig
54
     */
55
    public function createConfig() {
56
        $em = $this->getEntityManager();
57
58
        $config = new AnalyticsConfig();
59
        $em->persist($config);
60
        $em->flush();
61
62
        $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->addOverviews($config);
63
        return $config;
64
    }
65
66
    /**
67
     * Flush a config
68
     *
69
     * @param int $id the config id
70
     */
71
    public function flushConfig($id=false) {
72
        $em = $this->getEntityManager();
73
74
        // Backward compatibilty to flush overviews without a config set
75
        if (!$id) {
76
            $overviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
77
            foreach ($overviewRepository->findAll() as $overview) {
78
                $em->remove($overview);
79
            }
80
            $em->flush();
81
            return;
82
        }
83
84
        $config = $id ? $this->find($id) : $this->findFirst();
85
        foreach ($config->getOverviews() as $overview) {
86
            $em->remove($overview);
87
        }
88
        foreach ($config->getSegments() as $segment) {
89
            $em->remove($segment);
90
        }
91
        $em->flush();
92
    }
93
94
    /**
95
     * Update the timestamp when data is collected
96
     *
97
     * @param int id
98
     */
99
    public function setUpdated($id=false) {
100
        $em = $this->getEntityManager();
101
        $config = $id ? $this->find($id) : $this->findFirst();
102
        $config->setLastUpdate(new \DateTime());
103
        $em->persist($config);
104
        $em->flush();
105
    }
106
107
    /**
108
     * saves the token
109
     *
110
     * @param string $token
111
     */
112
    public function saveToken($token, $id=false) {
113
        $em    = $this->getEntityManager();
114
        $config = $id ? $this->find($id) : $this->findFirst();
115
        $config->setToken($token);
116
        $em->persist($config);
117
        $em->flush();
118
    }
119
120
    /**
121
     * saves the property id
122
     *
123
     * @param string $propertyId
124
     */
125
    public function savePropertyId($propertyId, $id=false) {
126
        $em    = $this->getEntityManager();
127
        $config = $id ? $this->find($id) : $this->findFirst();
128
        $config->setPropertyId($propertyId);
129
        $em->persist($config);
130
        $em->flush();
131
    }
132
133
    /**
134
     * saves the account id
135
     *
136
     * @param string $accountId
137
     */
138
    public function saveAccountId($accountId, $id=false) {
139
        $em    = $this->getEntityManager();
140
        $config = $id ? $this->find($id) : $this->findFirst();
141
        $config->setAccountId($accountId);
142
        $em->persist($config);
143
        $em->flush();
144
    }
145
146
    /**
147
     * saves the profile id
148
     *
149
     * @param string $profileId
150
     */
151 View Code Duplication
    public function saveProfileId($profileId, $id=false) {
152
        $em    = $this->getEntityManager();
153
        $config = $id ? $this->find($id) : $this->findFirst();
154
        $config->setProfileId($profileId);
155
        $em->persist($config);
156
        $em->flush();
157
    }
158
159
    /**
160
     * saves the config name
161
     *
162
     * @param string $profileId
163
     */
164 View Code Duplication
    public function saveConfigName($name, $id=false) {
165
        $em    = $this->getEntityManager();
166
        $config = $id ? $this->find($id) : $this->findFirst();
167
        $config->setName($name);
168
        $em->persist($config);
169
        $em->flush();
170
    }
171
172
    /**
173
     * Resets the profile id
174
     *
175
     * @param int id
176
     */
177 View Code Duplication
    public function resetProfileId($id=false) {
178
        $em    = $this->getEntityManager();
179
        $config = $id ? $this->find($id) : $this->findFirst();
180
        $config->setProfileId('');
181
        $em->persist($config);
182
        $em->flush();
183
    }
184
185
    /**
186
     * Resets the  account id, property id and profile id
187
     *
188
     * @param int id
189
     */
190
    public function resetPropertyId($id=false) {
191
        $em    = $this->getEntityManager();
192
        $config = $id ? $this->find($id) : $this->findFirst();
193
        $config->setAccountId('');
194
        $config->setProfileId('');
195
        $config->setPropertyId('');
196
        $em->persist($config);
197
        $em->flush();
198
    }
199
}
200