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
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
8
9
/**
10
 * AnalyticsConfigRepository
11
 *
12
 * This class was generated by the Doctrine ORM. Add your own custom
13
 * repository methods below.
14
 */
15
class AnalyticsConfigRepository extends EntityRepository
16
{
17
    /**
18
     * Get the first config from the database, creates a new entry if the config doesn't exist yet
19
     *
20
     * @return AnalyticsConfig $config
21
     */
22
    public function findFirst($createNew = true)
23
    {
24
        // Backwards compatibility: select the first config, still used in the dashboard, specified config ids are set in the dashboard collection bundle
25
        $em = $this->getEntityManager();
26
        $query = $em->createQuery('SELECT c FROM KunstmaanDashboardBundle:AnalyticsConfig c');
27
        $result = $query->getResult();
28
        // if no configs exist, create a new one
29
        if (!$result && $createNew) {
30
            return $this->createConfig();
31
        }
32
33
        if ($result) {
34
            return $result[0];
35
        }
36
37
        return false;
38
    }
39
40
    /**
41
     * Get the default overviews for a config
42
     *
43
     * @return AnalyticsConfig $config
44
     */
45
    public function findDefaultOverviews($config)
46
    {
47
        $em = $this->getEntityManager();
48
49
        return $em->getRepository(AnalyticsOverview::class)
50
            ->findBy(array(
51
                    'config' => $config,
52
                    'segment' => null,
53
                ));
54
    }
55
56
    /**
57
     * Create a new config
58
     *
59
     * @return AnalyticsConfig
60
     */
61
    public function createConfig()
62
    {
63
        $em = $this->getEntityManager();
64
65
        $config = new AnalyticsConfig();
66
        $em->persist($config);
67
        $em->flush();
68
69
        $this->getEntityManager()->getRepository(AnalyticsOverview::class)->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...
70
71
        return $config;
72
    }
73
74
    /**
75
     * Flush a config
76
     *
77
     * @param int $id the config id
78
     */
79
    public function flushConfig($id = false)
80
    {
81
        $em = $this->getEntityManager();
82
83
        // Backward compatibilty to flush overviews without a config set
84
        if (!$id) {
85
            $overviewRepository = $em->getRepository(AnalyticsOverview::class);
86
            foreach ($overviewRepository->findAll() as $overview) {
87
                $em->remove($overview);
88
            }
89
            $em->flush();
90
91
            return;
92
        }
93
94
        $config = $id ? $this->find($id) : $this->findFirst();
95
        foreach ($config->getOverviews() as $overview) {
96
            $em->remove($overview);
97
        }
98
        foreach ($config->getSegments() as $segment) {
99
            $em->remove($segment);
100
        }
101
        $em->flush();
102
    }
103
104
    /**
105
     * Update the timestamp when data is collected
106
     *
107
     * @param int id
108
     */
109
    public function setUpdated($id = false)
110
    {
111
        $em = $this->getEntityManager();
112
        $config = $id ? $this->find($id) : $this->findFirst();
113
        $config->setLastUpdate(new \DateTime());
114
        $em->persist($config);
115
        $em->flush();
116
    }
117
118
    /**
119
     * saves the token
120
     *
121
     * @param string $token
122
     */
123
    public function saveToken($token, $id = false)
124
    {
125
        $em = $this->getEntityManager();
126
        $config = $id ? $this->find($id) : $this->findFirst();
127
        $config->setToken($token);
128
        $em->persist($config);
129
        $em->flush();
130
    }
131
132
    /**
133
     * saves the property id
134
     *
135
     * @param string $propertyId
136
     */
137
    public function savePropertyId($propertyId, $id = false)
138
    {
139
        $em = $this->getEntityManager();
140
        $config = $id ? $this->find($id) : $this->findFirst();
141
        $config->setPropertyId($propertyId);
142
        $em->persist($config);
143
        $em->flush();
144
    }
145
146
    /**
147
     * saves the account id
148
     *
149
     * @param string $accountId
150
     */
151
    public function saveAccountId($accountId, $id = false)
152
    {
153
        $em = $this->getEntityManager();
154
        $config = $id ? $this->find($id) : $this->findFirst();
155
        $config->setAccountId($accountId);
156
        $em->persist($config);
157
        $em->flush();
158
    }
159
160
    /**
161
     * saves the profile id
162
     *
163
     * @param string $profileId
164
     */
165 View Code Duplication
    public function saveProfileId($profileId, $id = false)
166
    {
167
        $em = $this->getEntityManager();
168
        $config = $id ? $this->find($id) : $this->findFirst();
169
        $config->setProfileId($profileId);
170
        $em->persist($config);
171
        $em->flush();
172
    }
173
174
    /**
175
     * saves the config name
176
     *
177
     * @param string $profileId
178
     */
179 View Code Duplication
    public function saveConfigName($name, $id = false)
180
    {
181
        $em = $this->getEntityManager();
182
        $config = $id ? $this->find($id) : $this->findFirst();
183
        $config->setName($name);
184
        $em->persist($config);
185
        $em->flush();
186
    }
187
188
    /**
189
     * Resets the profile id
190
     *
191
     * @param int id
192
     */
193 View Code Duplication
    public function resetProfileId($id = false)
194
    {
195
        $em = $this->getEntityManager();
196
        $config = $id ? $this->find($id) : $this->findFirst();
197
        $config->setProfileId('');
198
        $em->persist($config);
199
        $em->flush();
200
    }
201
202
    /**
203
     * Resets the  account id, property id and profile id
204
     *
205
     * @param int id
206
     */
207
    public function resetPropertyId($id = false)
208
    {
209
        $em = $this->getEntityManager();
210
        $config = $id ? $this->find($id) : $this->findFirst();
211
        $config->setAccountId('');
212
        $config->setProfileId('');
213
        $config->setPropertyId('');
214
        $em->persist($config);
215
        $em->flush();
216
    }
217
}
218