Completed
Push — master ( aba493...5356ed )
by Ruud
315:38 queued 305:00
created

Repository/AnalyticsConfigRepository.php (7 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
        }
31
32
        if ($result) {
33
            return $result[0];
34
        }
35
36
        return false;
37
    }
38
39
    /**
40
     * Get the default overviews for a config
41
     *
42
     * @return AnalyticsConfig $config
0 ignored issues
show
Should the return type not be object[]?

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.

Loading history...
43
     */
44
    public function findDefaultOverviews($config)
45
    {
46
        $em = $this->getEntityManager();
47
48
        return $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')
49
            ->findBy(array(
50
                    'config' => $config,
51
                    'segment' => null,
52
                ));
53
    }
54
55
    /**
56
     * Create a new config
57
     *
58
     * @return AnalyticsConfig
59
     */
60
    public function createConfig()
61
    {
62
        $em = $this->getEntityManager();
63
64
        $config = new AnalyticsConfig();
65
        $em->persist($config);
66
        $em->flush();
67
68
        $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->addOverviews($config);
69
70
        return $config;
71
    }
72
73
    /**
74
     * Flush a config
75
     *
76
     * @param int $id the config id
0 ignored issues
show
Should the type for parameter $id not be false|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
77
     */
78
    public function flushConfig($id = false)
79
    {
80
        $em = $this->getEntityManager();
81
82
        // Backward compatibilty to flush overviews without a config set
83
        if (!$id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type false|integer is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
84
            $overviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
85
            foreach ($overviewRepository->findAll() as $overview) {
86
                $em->remove($overview);
87
            }
88
            $em->flush();
89
90
            return;
91
        }
92
93
        $config = $id ? $this->find($id) : $this->findFirst();
94
        foreach ($config->getOverviews() as $overview) {
95
            $em->remove($overview);
96
        }
97
        foreach ($config->getSegments() as $segment) {
98
            $em->remove($segment);
99
        }
100
        $em->flush();
101
    }
102
103
    /**
104
     * Update the timestamp when data is collected
105
     *
106
     * @param int id
107
     */
108
    public function setUpdated($id = false)
109
    {
110
        $em = $this->getEntityManager();
111
        $config = $id ? $this->find($id) : $this->findFirst();
112
        $config->setLastUpdate(new \DateTime());
113
        $em->persist($config);
114
        $em->flush();
115
    }
116
117
    /**
118
     * saves the token
119
     *
120
     * @param string $token
121
     */
122
    public function saveToken($token, $id = false)
123
    {
124
        $em = $this->getEntityManager();
125
        $config = $id ? $this->find($id) : $this->findFirst();
126
        $config->setToken($token);
127
        $em->persist($config);
128
        $em->flush();
129
    }
130
131
    /**
132
     * saves the property id
133
     *
134
     * @param string $propertyId
135
     */
136
    public function savePropertyId($propertyId, $id = false)
137
    {
138
        $em = $this->getEntityManager();
139
        $config = $id ? $this->find($id) : $this->findFirst();
140
        $config->setPropertyId($propertyId);
141
        $em->persist($config);
142
        $em->flush();
143
    }
144
145
    /**
146
     * saves the account id
147
     *
148
     * @param string $accountId
149
     */
150
    public function saveAccountId($accountId, $id = false)
151
    {
152
        $em = $this->getEntityManager();
153
        $config = $id ? $this->find($id) : $this->findFirst();
154
        $config->setAccountId($accountId);
155
        $em->persist($config);
156
        $em->flush();
157
    }
158
159
    /**
160
     * saves the profile id
161
     *
162
     * @param string $profileId
163
     */
164 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...
165
    {
166
        $em = $this->getEntityManager();
167
        $config = $id ? $this->find($id) : $this->findFirst();
168
        $config->setProfileId($profileId);
169
        $em->persist($config);
170
        $em->flush();
171
    }
172
173
    /**
174
     * saves the config name
175
     *
176
     * @param string $profileId
0 ignored issues
show
There is no parameter named $profileId. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
177
     */
178 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...
179
    {
180
        $em = $this->getEntityManager();
181
        $config = $id ? $this->find($id) : $this->findFirst();
182
        $config->setName($name);
183
        $em->persist($config);
184
        $em->flush();
185
    }
186
187
    /**
188
     * Resets the profile id
189
     *
190
     * @param int id
191
     */
192 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...
193
    {
194
        $em = $this->getEntityManager();
195
        $config = $id ? $this->find($id) : $this->findFirst();
196
        $config->setProfileId('');
197
        $em->persist($config);
198
        $em->flush();
199
    }
200
201
    /**
202
     * Resets the  account id, property id and profile id
203
     *
204
     * @param int id
205
     */
206
    public function resetPropertyId($id = false)
207
    {
208
        $em = $this->getEntityManager();
209
        $config = $id ? $this->find($id) : $this->findFirst();
210
        $config->setAccountId('');
211
        $config->setProfileId('');
212
        $config->setPropertyId('');
213
        $em->persist($config);
214
        $em->flush();
215
    }
216
}
217