Completed
Push — master ( 06c1ce...67d37c )
by Jeroen
06:20
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\Repository\AnalyticsConfigRepository;
6
use Symfony\Component\Routing\Annotation\Route;
7
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
11
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
12
13
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...
14
{
15
    /**
16
     * The index action will render the main screen the users see when they log in in to the admin
17
     *
18
     * @Route("/", name="KunstmaanDashboardBundle_widget_googleanalytics")
19
     * @Template("@KunstmaanDashboard/GoogleAnalytics/widget.html.twig")
20
     *
21
     * @param \Symfony\Component\HttpFoundation\Request $request
22
     *
23
     * @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...
24
     */
25
    public function widgetAction(Request $request)
26
    {
27
        $params['redirect_uri'] = $this->get('router')->generate('KunstmaanDashboardBundle_setToken', array(), UrlGeneratorInterface::ABSOLUTE_URL);
28
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
29
30
        // if token not set
31
        if (!$configHelper->tokenIsSet()) {
32
            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') != '') {
33
                $params['authUrl'] = $configHelper->getAuthUrl();
34
            }
35
36
            return $this->render('@KunstmaanDashboard/GoogleAnalytics/connect.html.twig', $params);
37
        }
38
39
        // if propertyId not set
40
        if (!$configHelper->accountIsSet()) {
41
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
42
        }
43
44
        // if propertyId not set
45
        if (!$configHelper->propertyIsSet()) {
46
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
47
        }
48
49
        // if profileId not set
50
        if (!$configHelper->profileIsSet()) {
51
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
52
        }
53
54
        $em = $this->getDoctrine()->getManager();
55
56
        // get the segment id
57
        $segmentId = $request->query->get('id');
58
        $params['segments'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getSegments();
59
        $params['segmentId'] = $segmentId;
60
61
        // set the overviews param
62
        $params['token'] = true;
63
        if ($segmentId) {
64
            $overviews = $em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->find($segmentId)->getOverviews();
65
        } else {
66
            $overviews = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->getDefaultOverviews();
67
        }
68
69
        $params['disableGoals'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getDisableGoals();
70
        $params['overviews'] = $overviews;
71
        /** @var AnalyticsConfigRepository $analyticsConfigRepository */
72
        $analyticsConfigRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
73
        $date = $analyticsConfigRepository->findFirst()->getLastUpdate();
74
        if ($date) {
75
            $params['last_update'] = $date->format('d-m-Y H:i');
76
        } else {
77
            $params['last_update'] = 'never';
78
        }
79
80
        return $params;
81
    }
82
83
    /**
84
     * @Route("/setToken/", name="KunstmaanDashboardBundle_setToken")
85
     *
86
     * @param Request $request
87
     *
88
     * @throws AccessDeniedException
89
     *
90
     * @return array
91
     */
92
    public function setTokenAction(Request $request)
93
    {
94
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
95
96
        $code = urldecode($request->query->get('code'));
97
98
        if (isset($code)) {
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
     * @param Request $request
115
     *
116
     * @throws AccessDeniedException
117
     *
118
     * @return array
119
     */
120
    public function configAction(Request $request)
121
    {
122
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
123
124
        $params = array();
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'] = array();
138
        $params['profiles'] = array();
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
        $params['profileSegments'] = $configHelper->getProfileSegments();
154
155
        return $this->render(
156
            '@KunstmaanDashboard/GoogleAnalytics/setupcontainer.html.twig',
157
            $params
158
        );
159
    }
160
161
    /**
162
     * @Route("/resetProfile", name="KunstmaanDashboardBundle_analytics_resetProfile")
163
     *
164
     * @throws AccessDeniedException
165
     */
166
    public function resetProfileAction()
167
    {
168
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
169
170
        $em = $this->getDoctrine()->getManager();
171
        $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetProfileId();
172
173
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
174
    }
175
176
    /**
177
     * @Route("/resetProperty", name="KunstmaanDashboardBundle_analytics_resetProperty")
178
     *
179
     * @throws AccessDeniedException
180
     */
181
    public function resetPropertyAction()
182
    {
183
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
184
185
        $em = $this->getDoctrine()->getManager();
186
        $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetPropertyId();
187
188
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
189
    }
190
}
191