Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
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
    {
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
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);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method addOverviews() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...yticsOverviewRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
69
70
        return $config;
71
    }
72
73
    /**
74
     * Flush a config
75
     *
76
     * @param int $id the config id
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) {
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)
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
177
     */
178 View Code Duplication
    public function saveConfigName($name, $id = false)
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)
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