Completed
Pull Request — 5.6 (#2830)
by Jeroen
14:14
created

Helper/Google/Analytics/ConfigHelper.php (4 issues)

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
    public function __construct(ServiceHelper $serviceHelper, EntityManager $em)
32
    {
33
        $this->serviceHelper = $serviceHelper;
34
        $this->em = $em;
35
        $this->init();
36
    }
37
38
    /**
39
     * Tries to initialise the Client object
40
     *
41
     * @param int $configId
42
     */
43
    public function init($configId = false)
44
    {
45
        // if token is already saved in the database
46
        if ($this->getToken($configId) && '' !== $this->getToken($configId)) {
47
            $this
48
                ->serviceHelper
49
                ->getClientHelper()
50
                ->getClient()
51
                ->setAccessToken($this->getToken($configId));
52
        }
53
54
        if ($configId) {
55
            $this->getAccountId($configId);
56
            $this->getPropertyId($configId);
57
            $this->getProfileId($configId);
58
        }
59
    }
60
61
    /* =============================== TOKEN =============================== */
62
63
    /**
64
     * Get the token from the database
65
     *
66
     * @return string $token
67
     */
68 View Code Duplication
    private function getToken($configId = false)
0 ignored issues
show
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...
69
    {
70
        if (!$this->token || $configId) {
71
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
72
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
73
            if ($configId) {
74
                $this->token = $analyticsConfigRepository->find($configId)->getToken();
75
            } else {
76
                $this->token = $analyticsConfigRepository->findFirst()->getToken();
77
            }
78
        }
79
80
        return $this->token;
81
    }
82
83
    /**
84
     * Save the token to the database
85
     */
86
    public function saveToken($token, $configId = false)
87
    {
88
        $this->token = $token;
89
        $this->em->getRepository(AnalyticsConfig::class)->saveToken($token, $configId);
90
    }
91
92
    /**
93
     * Check if token is set
94
     *
95
     * @return bool $result
96
     */
97
    public function tokenIsSet()
98
    {
99
        return $this->getToken() && '' !== $this->getToken();
100
    }
101
102
    /* =============================== ACCOUNT =============================== */
103
104
    /**
105
     * Get a list of all available accounts
106
     *
107
     * @return array $data A list of all properties
108
     */
109
    public function getAccounts()
110
    {
111
        $accounts = $this->serviceHelper->getService()->management_accounts->listManagementAccounts()->getItems();
112
        $data = [];
113
114
        foreach ($accounts as $account) {
115
            $data[$account->getName()] = [
116
                    'accountId' => $account->getId(),
117
                    'accountName' => $account->getName(),
118
                ];
119
        }
120
        ksort($data);
121
122
        return $data;
123
    }
124
125
    /**
126
     * Get the accountId from the database
127
     *
128
     * @return string $accountId
129
     */
130 View Code Duplication
    public function getAccountId($configId = false)
0 ignored issues
show
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...
131
    {
132
        if (!$this->accountId || $configId) {
133
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
134
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
135
            if ($configId) {
136
                $this->accountId = $analyticsConfigRepository->find($configId)->getAccountId();
137
            } else {
138
                $this->accountId = $analyticsConfigRepository->findFirst()->getAccountId();
139
            }
140
        }
141
142
        return $this->accountId;
143
    }
144
145
    /**
146
     * Save the accountId to the database
147
     */
148
    public function saveAccountId($accountId, $configId = false)
149
    {
150
        $this->accountId = $accountId;
151
        $this->em->getRepository(AnalyticsConfig::class)->saveAccountId($accountId, $configId);
152
    }
153
154
    /**
155
     * Check if token is set
156
     *
157
     * @return bool $result
158
     */
159
    public function accountIsSet()
160
    {
161
        return $this->getAccountId() && '' !== $this->getAccountId();
162
    }
163
164
    /* =============================== PROPERTY =============================== */
165
166
    /**
167
     * Get a list of all available properties
168
     *
169
     * @return array $data A list of all properties
170
     */
171
    public function getProperties($accountId = false)
172
    {
173
        if (!$this->getAccountId() && !$accountId) {
174
            return false;
175
        }
176
177
        if ($accountId) {
178
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($accountId);
179
        } else {
180
            $webproperties = $this->serviceHelper->getService()->management_webproperties->listManagementWebproperties($this->getAccountId());
181
        }
182
        $data = [];
183
184
        foreach ($webproperties->getItems() as $property) {
185
            $profiles = $this->getProfiles($accountId, $property->getId());
186
            if (\count($profiles) > 0) {
187
                $data[$property->getName()] = [
188
                        'propertyId' => $property->getId(),
189
                        'propertyName' => $property->getName() . ' (' . $property->getWebsiteUrl() . ')',
190
                    ];
191
            }
192
        }
193
        ksort($data);
194
195
        return $data;
196
    }
197
198
    /**
199
     * Get the propertyId from the database
200
     *
201
     * @return string $propertyId
202
     */
203 View Code Duplication
    public function getPropertyId($configId = false)
0 ignored issues
show
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...
204
    {
205
        if (!$this->propertyId || $configId) {
206
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
207
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
208
            if ($configId) {
209
                $this->propertyId = $analyticsConfigRepository->find($configId)->getPropertyId();
210
            } else {
211
                $this->propertyId = $analyticsConfigRepository->findFirst()->getPropertyId();
212
            }
213
        }
214
215
        return $this->propertyId;
216
    }
217
218
    /**
219
     * Save the propertyId to the database
220
     */
221
    public function savePropertyId($propertyId, $configId = false)
222
    {
223
        $this->propertyId = $propertyId;
224
        $this->em->getRepository(AnalyticsConfig::class)->savePropertyId($propertyId, $configId);
225
    }
226
227
    /**
228
     * Check if propertyId is set
229
     *
230
     * @return bool $result
231
     */
232
    public function propertyIsSet()
233
    {
234
        return null !== $this->getPropertyId() && '' !== $this->getPropertyId();
235
    }
236
237
    /* =============================== PROFILE =============================== */
238
239
    /**
240
     * Get a list of all available profiles
241
     *
242
     * @return array $data A list of all properties
243
     */
244
    public function getProfiles($accountId = false, $propertyId = false)
245
    {
246
        if ((!$this->getAccountId() && !$accountId) || (!$this->getPropertyId() && !$propertyId)) {
247
            return false;
248
        }
249
250
        // get views
251
        if ($accountId && $propertyId) {
252
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
253
                    $accountId,
254
                    $propertyId
255
                );
256
        } else {
257
            $profiles = $this->serviceHelper->getService()->management_profiles->listManagementProfiles(
258
                    $this->getAccountId(),
259
                    $this->getPropertyId()
260
                );
261
        }
262
263
        $data = [];
264
        if (\is_array($profiles->getItems())) {
265
            foreach ($profiles->getItems() as $profile) {
266
                $data[$profile->name] = [
267
                            'profileId' => $profile->id,
268
                            'profileName' => $profile->name,
269
                            'created' => $profile->created,
270
                        ];
271
            }
272
        }
273
        ksort($data);
274
275
        return $data;
276
    }
277
278
    /**
279
     * Get the propertyId from the database
280
     *
281
     * @return string $propertyId
282
     */
283 View Code Duplication
    public function getProfileId($configId = false)
0 ignored issues
show
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...
284
    {
285
        if (!$this->profileId || $configId) {
286
            /** @var AnalyticsConfigRepository $analyticsConfigRepository */
287
            $analyticsConfigRepository = $this->em->getRepository(AnalyticsConfig::class);
288
            if ($configId) {
289
                $this->profileId = $analyticsConfigRepository->find($configId)->getProfileId();
290
            } else {
291
                $this->profileId = $analyticsConfigRepository->findFirst()->getProfileId();
292
            }
293
        }
294
295
        return $this->profileId;
296
    }
297
298
    /**
299
     * Save the profileId to the database
300
     */
301
    public function saveProfileId($profileId, $configId = false)
302
    {
303
        $this->profileId = $profileId;
304
        $this->em->getRepository(AnalyticsConfig::class)->saveProfileId($profileId, $configId);
305
    }
306
307
    /**
308
     * Check if token is set
309
     *
310
     * @return bool $result
311
     */
312
    public function profileIsSet()
313
    {
314
        return null !== $this->getProfileId() && '' !== $this->getProfileId();
315
    }
316
317
    /**
318
     * Get the active profile
319
     *
320
     * @return the profile
321
     */
322
    public function getActiveProfile()
323
    {
324
        $profiles = $this->getProfiles();
325
        $profileId = $this->getProfileId();
326
327
        if (!\is_array($profiles)) {
328
            throw new \Exception('<fg=red>The config is invalid</fg=red>');
329
        }
330
331
        foreach ($profiles as $profile) {
332
            if ($profile['profileId'] == $profileId) {
333
                return $profile;
334
            }
335
        }
336
    }
337
338
    /* =============================== PROFILE SEGMENTS =============================== */
339
340
    /**
341
     * get all segments for the saved profile
342
     *
343
     * @return array
344
     */
345
    public function getProfileSegments()
346
    {
347
        $profileSegments = $this
348
                    ->serviceHelper
349
                    ->getService()
350
                    ->management_segments
351
                    ->listManagementSegments()
352
                    ->items;
353
354
        $builtin = [];
355
        $own = [];
356
        foreach ($profileSegments as $segment) {
357
            if ($segment->type == 'BUILT_IN') {
358
                $builtin[] = [
359
                        'name' => $segment->name,
360
                        'query' => $segment->segmentId,
361
                    ];
362
            } else {
363
                $own[] = [
364
                        'name' => $segment->name,
365
                        'query' => $segment->segmentId,
366
                    ];
367
            }
368
        }
369
370
        return ['builtin' => $builtin, 'own' => $own];
371
    }
372
373
    /* =============================== CONFIG =============================== */
374
375
    /**
376
     * Save the config to the database
377
     */
378
    public function saveConfigName($configName, $configId = false)
379
    {
380
        $this->em->getRepository(AnalyticsConfig::class)->saveConfigName($configName, $configId);
381
    }
382
383
    /* =============================== AUTH URL =============================== */
384
385
    /**
386
     * get the authUrl
387
     *
388
     * @return string $authUrl
389
     */
390
    public function getAuthUrl()
391
    {
392
        return $this
393
                ->serviceHelper
394
                ->getClientHelper()
395
                ->getClient()
396
                ->createAuthUrl();
397
    }
398
}
399