Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
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
namespace Kunstmaan\DashboardBundle\Controller;
3
4
use Kunstmaan\DashboardBundle\Repository\AnalyticsConfigRepository;
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
10
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
11
12
class GoogleAnalyticsController extends Controller
13
{
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()
20
     *
21
     * @param \Symfony\Component\HttpFoundation\Request $request
22
     * @return array
23
     */
24
    public function widgetAction(Request $request)
25
    {
26
        $params['redirect_uri'] = $this->get('router')->generate('KunstmaanDashboardBundle_setToken', array(), UrlGeneratorInterface::ABSOLUTE_URL);
27
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
28
29
        // if token not set
30
        if (!$configHelper->tokenIsSet()) {
31
            if ($this->getParameter('google.api.client_id') != '' && $this->getParameter('google.api.client_secret') != '' && $this->getParameter('google.api.dev_key') != '' ) {
32
                $params['authUrl'] = $configHelper->getAuthUrl($params['redirect_uri']);
33
            }
34
35
            return $this->render('KunstmaanDashboardBundle:GoogleAnalytics:connect.html.twig', $params);
36
        }
37
38
        // if propertyId not set
39
        if (!$configHelper->accountIsSet()) {
40
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
41
        }
42
43
        // if propertyId not set
44
        if (!$configHelper->propertyIsSet()) {
45
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_PropertySelection'));
46
        }
47
48
        // if profileId not set
49
        if (!$configHelper->profileIsSet()) {
50
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
51
        }
52
53
        $em = $this->getDoctrine()->getManager();
54
55
        // get the segment id
56
        $segmentId = $request->query->get('id');
57
        $params['segments'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getSegments();
58
        $params['segmentId'] = $segmentId;
59
60
        // set the overviews param
61
        $params['token'] = true;
62
        if ($segmentId) {
63
            $overviews = $em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->find($segmentId)->getOverviews();
64
        } else {
65
            $overviews = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview')->getDefaultOverviews();
66
        }
67
68
        $params['disableGoals'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getDisableGoals();
69
        $params['overviews'] = $overviews;
70
        /** @var AnalyticsConfigRepository $analyticsConfigRepository */
71
        $analyticsConfigRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig');
72
        $date = $analyticsConfigRepository->findFirst()->getLastUpdate();
73
        if ($date) {
74
            $params['last_update'] = $date->format('d-m-Y H:i');
75
        } else {
76
            $params['last_update'] = "never";
77
        }
78
        return $params;
79
    }
80
81
82
    /**
83
     * @Route("/setToken/", name="KunstmaanDashboardBundle_setToken")
84
     *
85
     * @param Request $request
86
     *
87
     * @throws AccessDeniedException
88
     *
89
     * @return array
0 ignored issues
show
Should the return type not be \Symfony\Component\HttpFoundation\RedirectResponse?

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...
90
     */
91
    public function setTokenAction(Request $request)
92
    {
93
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
94
95
        $code = urldecode($request->query->get('code'));
96
97
        if (isset($code)) {
98
            $clientHelper = $this->container->get('kunstmaan_dashboard.helper.google.client');
99
            $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
100
101
            $clientHelper->getClient()->authenticate($code);
102
            $configHelper->saveToken($clientHelper->getClient()->getAccessToken());
103
104
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
105
        }
106
107
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics'));
108
    }
109
110
111
    /**
112
     * @Route("/config", name="KunstmaanDashboardBundle_Config")
113
     *
114
     * @param Request $request
115
     *
116
     * @throws AccessDeniedException
117
     *
118
     * @return array
0 ignored issues
show
Should the return type not be \Symfony\Component\HttpFoundation\Response?

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...
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
154
        $params['profileSegments'] = $configHelper->getProfileSegments();
155
156
        return $this->render(
157
            'KunstmaanDashboardBundle:GoogleAnalytics:setupcontainer.html.twig',
158
            $params
159
        );
160
    }
161
162
    /**
163
     * @Route("/resetProfile", name="KunstmaanDashboardBundle_analytics_resetProfile")
164
     *
165
     * @throws AccessDeniedException
166
     */
167
    public function resetProfileAction()
168
    {
169
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
170
171
        $em = $this->getDoctrine()->getManager();
172
        $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetProfileId();
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
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
188
    }
189
}
190