Completed
Push — master ( 9684f5...5d31f9 )
by Sander
36:56 queued 12:48
created

AnalyticsConfigRepository   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 201
Duplicated Lines 10.45 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 5
dl 21
loc 201
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A findFirst() 0 15 4
A findDefaultOverviews() 0 10 1
A createConfig() 0 12 1
B flushConfig() 0 24 6
A setUpdated() 0 8 2
A saveToken() 0 8 2
A savePropertyId() 0 8 2
A saveAccountId() 0 8 2
A saveProfileId() 7 8 2
A saveConfigName() 7 8 2
A resetProfileId() 7 8 2
A resetPropertyId() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
0 ignored issues
show
Documentation introduced by
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...
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
0 ignored issues
show
Documentation introduced by
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...
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) {
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...
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
Duplication introduced by
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
0 ignored issues
show
Bug introduced by
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...
175
     */
176 View Code Duplication
    public function saveConfigName($name, $id = false)
0 ignored issues
show
Duplication introduced by
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
Duplication introduced by
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