Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

Helper/Google/Analytics/ConfigHelper.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\Helper\Google\Analytics;
4
use Doctrine\ORM\EntityManager;
5
use Kunstmaan\DashboardBundle\Helper\Google\Analytics\ServiceHelper;
6
7
class ConfigHelper
8
{
9
10
    /** @var ServiceHelper */
11
    private $serviceHelper;
12
13
    /** @var string $token */
14
    private $token = false;
15
16
    /** @var string $accountId */
17
    private $propertyId = false;
18
19
    /** @var string $accountId */
20
    private $accountId = false;
21
22
    /** @var string $profileId */
23
    private $profileId = false;
24
25
    /** @var EntityManager $em */
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) {
0 ignored issues
show
You have injected the EntityManager via parameter $em. This is generally not recommended as it might get closed and become unusable. Instead, it is recommended to inject the ManagerRegistry and retrieve the EntityManager via getManager() each time you need it.

The EntityManager might become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:

function someFunction(ManagerRegistry $registry) {
    $em = $registry->getManager();
    $em->getConnection()->beginTransaction();
    try {
        // Do something.
        $em->getConnection()->commit();
    } catch (\Exception $ex) {
        $em->getConnection()->rollback();
        $em->close();

        throw $ex;
    }
}

If that code throws an exception and the EntityManager is closed. Any other code which depends on the same instance of the EntityManager during this request will fail.

On the other hand, if you instead inject the ManagerRegistry, the getManager() method guarantees that you will always get a usable manager instance.

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