Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Helper/Google/Analytics/ConfigHelper.php (5 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\Helper\Google\Analytics;
4
5
use Doctrine\ORM\EntityManager;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
7
8
class ConfigHelper
9
{
10
    /** @var ServiceHelper */
11
    private $serviceHelper;
12
13
    /** @var string */
14
    private $token = false;
15
16
    /** @var string */
17
    private $propertyId = false;
18
19
    /** @var string */
20
    private $accountId = false;
21
22
    /** @var string */
23
    private $profileId = false;
24
25
    /** @var EntityManager */
26
    private $em;
27
28
    /**
29
     * constructor
30
     *
31
     * @param ServiceHelper $serviceHelper
32
     * @param EntityManager $em
33
     */
34
    public function __construct(ServiceHelper $serviceHelper, EntityManager $em)
35
    {
36
        $this->serviceHelper = $serviceHelper;
37
        $this->em = $em;
38
        $this->init();
39
    }
40
41
    /**
42
     * Tries to initialise the Client object
43
     *
44
     * @param int $configId
45
     */
46
    public function init($configId = false)
47
    {
48
        // if token is already saved in the database
49
        if ($this->getToken($configId) && '' !== $this->getToken($configId)) {
50
            $this
51
                ->serviceHelper
52
                ->getClientHelper()
53
                ->getClient()
54
                ->setAccessToken($this->getToken($configId));
55
        }
56
57
        if ($configId) {
58
            $this->getAccountId($configId);
59
            $this->getPropertyId($configId);
60
            $this->getProfileId($configId);
61
        }
62
    }
63
64
    /* =============================== TOKEN =============================== */
65
66
    /**
67
     * Get the token from the database
68
     *
69
     * @return string $token
70
     */
71 View Code Duplication
    private function getToken($configId = false)
72
    {
73
        if (!$this->token || $configId) {
74
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
75
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
76
            if ($configId) {
77
                $this->token = $analyticsConfigRepository->find($configId)->getToken();
78
            } else {
79
                $this->token = $analyticsConfigRepository->findFirst()->getToken();
80
            }
81
        }
82
83
        return $this->token;
84
    }
85
86
    /**
87
     * Save the token to the database
88
     */
89
    public function saveToken($token, $configId = false)
90
    {
91
        $this->token = $token;
92
        $this->em->getRepository(AnalyticsConfig::class)->saveToken($token, $configId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method saveToken() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
93
    }
94
95
    /**
96
     * Check if token is set
97
     *
98
     * @return bool $result
99
     */
100
    public function tokenIsSet()
101
    {
102
        return $this->getToken() && '' !== $this->getToken();
103
    }
104
105
    /* =============================== ACCOUNT =============================== */
106
107
    /**
108
     * Get a list of all available accounts
109
     *
110
     * @return array $data A list of all properties
111
     */
112
    public function getAccounts()
113
    {
114
        $accounts = $this->serviceHelper->getService()->management_accounts->listManagementAccounts()->getItems();
115
        $data = array();
116
117
        foreach ($accounts as $account) {
118
            $data[$account->getName()] = array(
119
                    'accountId' => $account->getId(),
120
                    'accountName' => $account->getName(),
121
                );
122
        }
123
        ksort($data);
124
125
        return $data;
126
    }
127
128
    /**
129
     * Get the accountId from the database
130
     *
131
     * @return string $accountId
132
     */
133 View Code Duplication
    public function getAccountId($configId = false)
134
    {
135
        if (!$this->accountId || $configId) {
136
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
137
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
138
            if ($configId) {
139
                $this->accountId = $analyticsConfigRepository->find($configId)->getAccountId();
140
            } else {
141
                $this->accountId = $analyticsConfigRepository->findFirst()->getAccountId();
142
            }
143
        }
144
145
        return $this->accountId;
146
    }
147
148
    /**
149
     * Save the accountId to the database
150
     */
151
    public function saveAccountId($accountId, $configId = false)
152
    {
153
        $this->accountId = $accountId;
154
        $this->em->getRepository(AnalyticsConfig::class)->saveAccountId($accountId, $configId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method saveAccountId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
155
    }
156
157
    /**
158
     * Check if token is set
159
     *
160
     * @return bool $result
161
     */
162
    public function accountIsSet()
163
    {
164
        return $this->getAccountId() && '' !== $this->getAccountId();
165
    }
166
167
    /* =============================== PROPERTY =============================== */
168
169
    /**
170
     * Get a list of all available properties
171
     *
172
     * @return array $data A list of all properties
173
     */
174
    public function getProperties($accountId = false)
175
    {
176
        if (!$this->getAccountId() && !$accountId) {
177
            return false;
178
        }
179
180
        if ($accountId) {
181
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($accountId);
182
        } else {
183
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($this->getAccountId());
184
        }
185
        $data = array();
186
187
        foreach ($webproperties->getItems() as $property) {
188
            $profiles = $this->getProfiles($accountId, $property->getId());
189
            if (\count($profiles) > 0) {
190
                $data[$property->getName()] = array(
191
                        'propertyId' => $property->getId(),
192
                        'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
193
                    );
194
            }
195
        }
196
        ksort($data);
197
198
        return $data;
199
    }
200
201
    /**
202
     * Get the propertyId from the database
203
     *
204
     * @return string $propertyId
205
     */
206 View Code Duplication
    public function getPropertyId($configId = false)
207
    {
208
        if (!$this->propertyId || $configId) {
209
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
210
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
211
            if ($configId) {
212
                $this->propertyId = $analyticsConfigRepository->find($configId)->getPropertyId();
213
            } else {
214
                $this->propertyId = $analyticsConfigRepository->findFirst()->getPropertyId();
215
            }
216
        }
217
218
        return $this->propertyId;
219
    }
220
221
    /**
222
     * Save the propertyId to the database
223
     */
224
    public function savePropertyId($propertyId, $configId = false)
225
    {
226
        $this->propertyId = $propertyId;
227
        $this->em->getRepository(AnalyticsConfig::class)->savePropertyId($propertyId, $configId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method savePropertyId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
228
    }
229
230
    /**
231
     * Check if propertyId is set
232
     *
233
     * @return bool $result
234
     */
235
    public function propertyIsSet()
236
    {
237
        return null !== $this->getPropertyId() && '' !== $this->getPropertyId();
238
    }
239
240
    /* =============================== PROFILE =============================== */
241
242
    /**
243
     * Get a list of all available profiles
244
     *
245
     * @return array $data A list of all properties
246
     */
247
    public function getProfiles($accountId = false, $propertyId = false)
248
    {
249
        if ((!$this->getAccountId() && !$accountId) || (!$this->getPropertyId() && !$propertyId)) {
250
            return false;
251
        }
252
253
        // get views
254
        if ($accountId && $propertyId) {
255
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
256
                    $accountId,
257
                    $propertyId
258
                );
259
        } else {
260
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
261
                    $this->getAccountId(),
262
                    $this->getPropertyId()
263
                );
264
        }
265
266
        $data = array();
267
        if (\is_array($profiles->getItems())) {
268
            foreach ($profiles->getItems() as $profile) {
269
                $data[$profile->name] = array(
270
                            'profileId' => $profile->id,
271
                            'profileName' => $profile->name,
272
                            'created' => $profile->created,
273
                        );
274
            }
275
        }
276
        ksort($data);
277
278
        return $data;
279
    }
280
281
    /**
282
     * Get the propertyId from the database
283
     *
284
     * @return string $propertyId
285
     */
286 View Code Duplication
    public function getProfileId($configId = false)
287
    {
288
        if (!$this->profileId || $configId) {
289
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
290
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
291
            if ($configId) {
292
                $this->profileId = $analyticsConfigRepository->find($configId)->getProfileId();
293
            } else {
294
                $this->profileId = $analyticsConfigRepository->findFirst()->getProfileId();
295
            }
296
        }
297
298
        return $this->profileId;
299
    }
300
301
    /**
302
     * Save the profileId to the database
303
     */
304
    public function saveProfileId($profileId, $configId = false)
305
    {
306
        $this->profileId = $profileId;
307
        $this->em->getRepository(AnalyticsConfig::class)->saveProfileId($profileId, $configId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method saveProfileId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
308
    }
309
310
    /**
311
     * Check if token is set
312
     *
313
     * @return bool $result
314
     */
315
    public function profileIsSet()
316
    {
317
        return null !== $this->getProfileId() && '' !== $this->getProfileId();
318
    }
319
320
    /**
321
     * Get the active profile
322
     *
323
     * @return the profile
324
     */
325
    public function getActiveProfile()
326
    {
327
        $profiles = $this->getProfiles();
328
        $profileId = $this->getProfileId();
329
330
        if (!\is_array($profiles)) {
331
            throw new \Exception('<fg=red>The config is invalid</fg=red>');
332
        }
333
334
        foreach ($profiles as $profile) {
335
            if ($profile['profileId'] == $profileId) {
336
                return $profile;
337
            }
338
        }
339
    }
340
341
    /* =============================== PROFILE SEGMENTS =============================== */
342
343
    /**
344
     * get all segments for the saved profile
345
     *
346
     * @return array
347
     */
348
    public function getProfileSegments()
349
    {
350
        $profileSegments = $this
351
                    ->serviceHelper
352
                    ->getService()
353
                    ->management_segments
354
                    ->listManagementSegments()
355
                    ->items;
356
357
        $builtin = array();
358
        $own = array();
359
        foreach ($profileSegments as $segment) {
360
            if ($segment->type == 'BUILT_IN') {
361
                $builtin[] = array(
362
                        'name' => $segment->name,
363
                        'query' => $segment->segmentId,
364
                    );
365
            } else {
366
                $own[] = array(
367
                        'name' => $segment->name,
368
                        'query' => $segment->segmentId,
369
                    );
370
            }
371
        }
372
373
        return array('builtin' => $builtin, 'own' => $own);
374
    }
375
376
    /* =============================== CONFIG =============================== */
377
378
    /**
379
     * Save the config to the database
380
     */
381
    public function saveConfigName($configName, $configId = false)
382
    {
383
        $this->em->getRepository(AnalyticsConfig::class)->saveConfigName($configName, $configId);
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method saveConfigName() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

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...
384
    }
385
386
    /* =============================== AUTH URL =============================== */
387
388
    /**
389
     * get the authUrl
390
     *
391
     * @return string $authUrl
392
     */
393
    public function getAuthUrl()
394
    {
395
        return $this
396
                ->serviceHelper
397
                ->getClientHelper()
398
                ->getClient()
399
                ->createAuthUrl();
400
    }
401
}
402