Completed
Pull Request — 6.0 (#1872)
by Sander
102:24 queued 60:56
created

GoogleAnalyticsController::resetProfileAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\DashboardBundle\Controller;
4
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment;
6
use Kunstmaan\DashboardBundle\Repository\AnalyticsConfigRepository;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
13
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
14
15
/**
16
 * Class GoogleAnalyticsController
17
 */
18
class GoogleAnalyticsController extends Controller
19
{
20
    /**
21
     * The index action will render the main screen the users see when they log in in to the admin
22
     *
23
     * @Route("/", name="KunstmaanDashboardBundle_widget_googleanalytics")
24
     * @Template()
25
     *
26
     * @param \Symfony\Component\HttpFoundation\Request $request
27
     *
28
     * @return Response|array
29
     * @throws \Doctrine\ORM\OptimisticLockException
30
     */
31
    public function widgetAction(Request $request)
32
    {
33
        $params['redirect_uri'] = $this->get('router')->generate('KunstmaanDashboardBundle_setToken', [], UrlGeneratorInterface::ABSOLUTE_URL);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$params was never initialized. Although not strictly required by PHP, it is generally a good practice to add $params = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
34
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
35
36
        // if token not set
37
        if (!$configHelper->tokenIsSet()) {
38
            if ($this->getParameter('google.api.client_id') !== '' && $this->getParameter('google.api.client_secret') !== '' && $this->getParameter(
39
                    'google.api.dev_key'
40
                ) !== '') {
41
                $params['authUrl'] = $configHelper->getAuthUrl();
42
            }
43
44
            return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params);
45
        }
46
47
        if (!$configHelper->accountIsSet() || !$configHelper->propertyIsSet() || !$configHelper->profileIsSet()) {
48
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
49
        }
50
51
        $em = $this->getDoctrine()->getManager();
52
53
        // get the segment id
54
        $segmentId = $request->query->get('id');
55
        $params['segments'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getSegments();
56
        $params['segmentId'] = $segmentId;
57
58
        // set the overviews param
59
        $params['token'] = true;
60
        if ($segmentId) {
61
            $overviews = $em->getRepository(AnalyticsSegment::class)->find($segmentId)->getOverviews();
62
        } else {
63
            $overviews = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->getDefaultOverviews();
64
        }
65
66
        $params['disableGoals'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getDisableGoals();
67
        $params['overviews'] = $overviews;
68
        /** @var AnalyticsConfigRepository $analyticsConfigRepository */
69
        $analyticsConfigRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
70
        $date = $analyticsConfigRepository->findFirst()->getLastUpdate();
71
        if ($date) {
72
            $params['last_update'] = $date->format('d-m-Y H:i');
73
        } else {
74
            $params['last_update'] = 'never';
75
        }
76
77
        return $params;
78
    }
79
80
81
    /**
82
     * @Route("/setToken/", name="KunstmaanDashboardBundle_setToken")
83
     *
84
     * @param Request $request
85
     *
86
     * @throws AccessDeniedException
87
     *
88
     * @return Response
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use \Symfony\Component\HttpFoundation\RedirectResponse.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
89
     */
90
    public function setTokenAction(Request $request)
91
    {
92
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
93
94
        $code = urldecode($request->query->get('code'));
95
96
        if (null !== $code) {
97
            $clientHelper = $this->container->get('kunstmaan_dashboard.helper.google.client');
98
            $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
99
100
            $clientHelper->getClient()->fetchAccessTokenWithAuthCode($code);
101
            if (null !== ($accessToken = $clientHelper->getClient()->getAccessToken())) {
102
                $configHelper->saveToken(json_encode($accessToken));
103
            }
104
105
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
106
        }
107
108
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics'));
109
    }
110
111
112
    /**
113
     * @Route("/config", name="KunstmaanDashboardBundle_Config")
114
     *
115
     * @param Request $request
116
     *
117
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
118
     * @throws \Doctrine\ORM\OptimisticLockException
119
     */
120
    public function configAction(Request $request)
121
    {
122
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
123
124
        $params = [];
125
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
126
127
        if (null !== $request->request->get('accounts')) {
128
            return $this->redirect($this->generateUrl('kunstmaan_dashboard'));
129
        }
130
131
        $em = $this->getDoctrine()->getManager();
132
        $config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst();
133
134
        $params['accountId'] = $config->getAccountId();
135
        $params['propertyId'] = 0;
136
        $params['profileId'] = 0;
137
        $params['properties'] = [];
138
        $params['profiles'] = [];
139
140
        if ($params['accountId']) {
141
            $params['propertyId'] = $config->getPropertyId();
142
            $params['properties'] = $configHelper->getProperties();
143
144
            $params['profileId'] = $config->getProfileId();
145
            $params['profiles'] = $configHelper->getProfiles();
146
        }
147
148
        $params['accounts'] = $configHelper->getAccounts();
149
        $params['segments'] = $config->getSegments();
150
        $params['disableGoals'] = $config->getDisableGoals();
151
        $params['configId'] = $config->getId();
152
153
154
        $params['profileSegments'] = $configHelper->getProfileSegments();
155
156
        return $this->render(
157
            'KunstmaanDashboardBundle:GoogleAnalytics:setupcontainer.html.twig',
158
            $params
159
        );
160
    }
161
}
162