Completed
Pull Request — 6.0 (#1872)
by Sander
102:24 queued 60:56
created

Repository/AnalyticsConfigRepository.php (8 issues)

Labels
Severity

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
 * Class AnalyticsConfigRepository
10
 */
11
class AnalyticsConfigRepository extends EntityRepository
12
{
13
    /**
14
     * Get the first config from the database, creates a new entry if the config doesn't exist yet
15
     *
16
     * @param bool $createNew
17
     *
18
     * @return bool|AnalyticsConfig
19
     * @throws \Doctrine\ORM\OptimisticLockException
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 array
43
     */
44
    public function findDefaultOverviews($config)
45
    {
46
        $em = $this->getEntityManager();
47
48
        return $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')
49
            ->findBy(
50
                [
51
                    'config' => $config,
52
                    'segment' => null,
53
                ]
54
            );
55
    }
56
57
    /**
58
     * @return AnalyticsConfig
59
     *
60
     * @throws \Doctrine\ORM\OptimisticLockException
61
     */
62
    public function createConfig()
63
    {
64
        $em = $this->getEntityManager();
65
66
        $config = new AnalyticsConfig();
67
        $em->persist($config);
68
        $em->flush();
69
70
        $this->getEntityManager()->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->addOverviews($config);
71
72
        return $config;
73
    }
74
75
    /**
76
     * @param bool $id
77
     *
78
     * @throws \Doctrine\ORM\OptimisticLockException
79
     */
80
    public function flushConfig($id = false)
81
    {
82
        $em = $this->getEntityManager();
83
84
        // Backward compatibilty to flush overviews without a config set
85
        if (!$id) {
86
            $overviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
87
            foreach ($overviewRepository->findAll() as $overview) {
88
                $em->remove($overview);
89
            }
90
            $em->flush();
91
92
            return;
93
        }
94
95
        $config = $id ? $this->find($id) : $this->findFirst();
96
        foreach ($config->getOverviews() as $overview) {
97
            $em->remove($overview);
98
        }
99
        foreach ($config->getSegments() as $segment) {
100
            $em->remove($segment);
101
        }
102
        $em->flush();
103
    }
104
105
    /**
106
     * @param bool $id
107
     *
108
     * @throws \Doctrine\ORM\OptimisticLockException
109
     */
110
    public function setUpdated($id = false)
111
    {
112
        $em = $this->getEntityManager();
113
        $config = $id ? $this->find($id) : $this->findFirst();
114
        $config->setLastUpdate(new \DateTime());
115
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 113 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
116
        $em->flush();
117
    }
118
119
    /**
120
     * @param string $token
121
     * @param bool   $id
122
     *
123
     * @throws \Doctrine\ORM\OptimisticLockException
124
     */
125
    public function saveToken($token, $id = false)
126
    {
127
        $em = $this->getEntityManager();
128
        $config = $id ? $this->find($id) : $this->findFirst();
129
        $config->setToken($token);
130
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 128 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
131
        $em->flush();
132
    }
133
134
    /**
135
     * @param string $propertyId
136
     * @param bool   $id
137
     *
138
     * @throws \Doctrine\ORM\OptimisticLockException
139
     */
140
    public function savePropertyId($propertyId, $id = false)
141
    {
142
        $em = $this->getEntityManager();
143
        $config = $id ? $this->find($id) : $this->findFirst();
144
        $config->setPropertyId($propertyId);
145
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 143 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
146
        $em->flush();
147
    }
148
149
    /**
150
     * @param string $accountId
151
     * @param bool   $id
152
     *
153
     * @throws \Doctrine\ORM\OptimisticLockException
154
     */
155
    public function saveAccountId($accountId, $id = false)
156
    {
157
        $em = $this->getEntityManager();
158
        $config = $id ? $this->find($id) : $this->findFirst();
159
        $config->setAccountId($accountId);
160
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 158 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
161
        $em->flush();
162
    }
163
164
    /**
165
     * @param string $profileId
166
     * @param bool   $id
167
     *
168
     * @throws \Doctrine\ORM\OptimisticLockException
169
     */
170 View Code Duplication
    public function saveProfileId($profileId, $id = false)
171
    {
172
        $em = $this->getEntityManager();
173
        $config = $id ? $this->find($id) : $this->findFirst();
174
        $config->setProfileId($profileId);
175
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 173 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
176
        $em->flush();
177
    }
178
179
    /**
180
     * @param string $name
181
     * @param bool   $id
182
     *
183
     * @throws \Doctrine\ORM\OptimisticLockException
184
     */
185 View Code Duplication
    public function saveConfigName($name, $id = false)
186
    {
187
        $em = $this->getEntityManager();
188
        $config = $id ? $this->find($id) : $this->findFirst();
189
        $config->setName($name);
190
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 188 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
191
        $em->flush();
192
    }
193
194
    /**
195
     * @param bool $id
196
     *
197
     * @throws \Doctrine\ORM\OptimisticLockException
198
     */
199 View Code Duplication
    public function resetProfileId($id = false)
200
    {
201
        $em = $this->getEntityManager();
202
        $config = $id ? $this->find($id) : $this->findFirst();
203
        $config->setProfileId('');
204
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 202 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
205
        $em->flush();
206
    }
207
208
    /**
209
     * @param bool $id
210
     *
211
     * @throws \Doctrine\ORM\OptimisticLockException
212
     */
213
    public function resetPropertyId($id = false)
214
    {
215
        $em = $this->getEntityManager();
216
        $config = $id ? $this->find($id) : $this->findFirst();
217
        $config->setAccountId('');
218
        $config->setProfileId('');
219
        $config->setPropertyId('');
220
        $em->persist($config);
0 ignored issues
show
It seems like $config defined by $id ? $this->find($id) : $this->findFirst() on line 216 can also be of type boolean; however, Doctrine\ORM\EntityManager::persist() does only seem to accept object, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
221
        $em->flush();
222
    }
223
}
224