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

Controller/GoogleAnalyticsController.php (2 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\Controller;
4
5
use Kunstmaan\DashboardBundle\Entity\AnalyticsConfig;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsOverview;
7
use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment;
8
use Kunstmaan\DashboardBundle\Repository\AnalyticsConfigRepository;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\Request;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
14
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
15
16
class GoogleAnalyticsController extends Controller
0 ignored issues
show
Deprecated Code introduced by
The class Symfony\Bundle\Framework...e\Controller\Controller has been deprecated with message: since Symfony 4.2, use "Symfony\Bundle\FrameworkBundle\Controller\AbstractController" instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
17
{
18
    /**
19
     * The index action will render the main screen the users see when they log in in to the admin
20
     *
21
     * @Route("/", name="KunstmaanDashboardBundle_widget_googleanalytics")
22
     * @Template("@KunstmaanDashboard/GoogleAnalytics/widget.html.twig")
23
     *
24
     * @return array
0 ignored issues
show
Should the return type not be \Symfony\Component\HttpFoundation\Response|array?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
25
     */
26
    public function widgetAction(Request $request)
27
    {
28
        $params['redirect_uri'] = $this->get('router')->generate('KunstmaanDashboardBundle_setToken', [], UrlGeneratorInterface::ABSOLUTE_URL);
29
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
30
31
        // if token not set
32
        if (!$configHelper->tokenIsSet()) {
33
            if ($this->getParameter('kunstmaan_dashboard.google_analytics.api.client_id') != '' && $this->getParameter('kunstmaan_dashboard.google_analytics.api.client_secret') != '' && $this->getParameter('kunstmaan_dashboard.google_analytics.api.dev_key') != '') {
34
                $params['authUrl'] = $configHelper->getAuthUrl();
35
            }
36
37
            return $this->render('@KunstmaanDashboard/GoogleAnalytics/connect.html.twig', $params);
38
        }
39
40
        // if propertyId not set
41
        if (!$configHelper->accountIsSet()) {
42
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
43
        }
44
45
        // if propertyId not set
46
        if (!$configHelper->propertyIsSet()) {
47
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
48
        }
49
50
        // if profileId not set
51
        if (!$configHelper->profileIsSet()) {
52
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
53
        }
54
55
        $em = $this->getDoctrine()->getManager();
56
57
        // get the segment id
58
        $segmentId = $request->query->get('id');
59
        $params['segments'] = $em->getRepository(AnalyticsConfig::class)->findFirst()->getSegments();
60
        $params['segmentId'] = $segmentId;
61
62
        // set the overviews param
63
        $params['token'] = true;
64
        if ($segmentId) {
65
            $overviews = $em->getRepository(AnalyticsSegment::class)->find($segmentId)->getOverviews();
66
        } else {
67
            $overviews = $em->getRepository(AnalyticsOverview::class)->getDefaultOverviews();
68
        }
69
70
        $params['disableGoals'] = $em->getRepository(AnalyticsConfig::class)->findFirst()->getDisableGoals();
71
        $params['overviews'] = $overviews;
72
        /** @var AnalyticsConfigRepository $analyticsConfigRepository */
73
        $analyticsConfigRepository = $em->getRepository(AnalyticsConfig::class);
74
        $date = $analyticsConfigRepository->findFirst()->getLastUpdate();
75
        if ($date) {
76
            $params['last_update'] = $date->format('d-m-Y H:i');
77
        } else {
78
            $params['last_update'] = 'never';
79
        }
80
81
        return $params;
82
    }
83
84
    /**
85
     * @Route("/setToken/", name="KunstmaanDashboardBundle_setToken")
86
     *
87
     * @throws AccessDeniedException
88
     *
89
     * @return array
90
     */
91
    public function setTokenAction(Request $request)
92
    {
93
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
94
95
        $codeParameter = $request->query->get('code');
96
97
        if (null !== $codeParameter) {
98
            $code = urldecode($codeParameter);
99
            $clientHelper = $this->container->get('kunstmaan_dashboard.helper.google.client');
100
            $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
101
102
            $clientHelper->getClient()->authenticate($code);
103
            $configHelper->saveToken($clientHelper->getClient()->getAccessToken());
104
105
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
106
        }
107
108
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics'));
109
    }
110
111
    /**
112
     * @Route("/config", name="KunstmaanDashboardBundle_Config")
113
     *
114
     * @throws AccessDeniedException
115
     *
116
     * @return array
117
     */
118
    public function configAction(Request $request)
119
    {
120
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
121
122
        $params = [];
123
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
124
125
        if (null !== $request->request->get('accounts')) {
126
            return $this->redirect($this->generateUrl('kunstmaan_dashboard'));
127
        }
128
129
        $em = $this->getDoctrine()->getManager();
130
        $config = $em->getRepository(AnalyticsConfig::class)->findFirst();
131
132
        $params['accountId'] = $config->getAccountId();
133
        $params['propertyId'] = 0;
134
        $params['profileId'] = 0;
135
        $params['properties'] = [];
136
        $params['profiles'] = [];
137
138
        if ($params['accountId']) {
139
            $params['propertyId'] = $config->getPropertyId();
140
            $params['properties'] = $configHelper->getProperties();
141
142
            $params['profileId'] = $config->getProfileId();
143
            $params['profiles'] = $configHelper->getProfiles();
144
        }
145
146
        $params['accounts'] = $configHelper->getAccounts();
147
        $params['segments'] = $config->getSegments();
148
        $params['disableGoals'] = $config->getDisableGoals();
149
        $params['configId'] = $config->getId();
150
151
        $params['profileSegments'] = $configHelper->getProfileSegments();
152
153
        return $this->render(
154
            '@KunstmaanDashboard/GoogleAnalytics/setupcontainer.html.twig',
155
            $params
156
        );
157
    }
158
159
    /**
160
     * @Route("/resetProfile", name="KunstmaanDashboardBundle_analytics_resetProfile")
161
     *
162
     * @throws AccessDeniedException
163
     */
164
    public function resetProfileAction()
165
    {
166
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
167
168
        $em = $this->getDoctrine()->getManager();
169
        $em->getRepository(AnalyticsConfig::class)->resetProfileId();
170
171
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
172
    }
173
174
    /**
175
     * @Route("/resetProperty", name="KunstmaanDashboardBundle_analytics_resetProperty")
176
     *
177
     * @throws AccessDeniedException
178
     */
179
    public function resetPropertyAction()
180
    {
181
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
182
183
        $em = $this->getDoctrine()->getManager();
184
        $em->getRepository(AnalyticsConfig::class)->resetPropertyId();
185
186
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
187
    }
188
}
189