Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

Controller/GoogleAnalyticsController.php (6 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
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
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();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findFirst() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method getDefaultOverviews() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...yticsOverviewRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
67
        }
68
69
        $params['disableGoals'] = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst()->getDisableGoals();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findFirst() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
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
        $codeParameter = $request->query->get('code');
97
98
        if (null !== $codeParameter) {
99
            $code = urldecode($codeParameter);
100
            $clientHelper = $this->container->get('kunstmaan_dashboard.helper.google.client');
101
            $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
102
103
            $clientHelper->getClient()->authenticate($code);
104
            $configHelper->saveToken($clientHelper->getClient()->getAccessToken());
105
106
            return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
107
        }
108
109
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_widget_googleanalytics'));
110
    }
111
112
    /**
113
     * @Route("/config", name="KunstmaanDashboardBundle_Config")
114
     *
115
     * @param Request $request
116
     *
117
     * @throws AccessDeniedException
118
     *
119
     * @return array
120
     */
121
    public function configAction(Request $request)
122
    {
123
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
124
125
        $params = array();
126
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
127
128
        if (null !== $request->request->get('accounts')) {
129
            return $this->redirect($this->generateUrl('kunstmaan_dashboard'));
130
        }
131
132
        $em = $this->getDoctrine()->getManager();
133
        $config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method findFirst() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
134
135
        $params['accountId'] = $config->getAccountId();
136
        $params['propertyId'] = 0;
137
        $params['profileId'] = 0;
138
        $params['properties'] = array();
139
        $params['profiles'] = array();
140
141
        if ($params['accountId']) {
142
            $params['propertyId'] = $config->getPropertyId();
143
            $params['properties'] = $configHelper->getProperties();
144
145
            $params['profileId'] = $config->getProfileId();
146
            $params['profiles'] = $configHelper->getProfiles();
147
        }
148
149
        $params['accounts'] = $configHelper->getAccounts();
150
        $params['segments'] = $config->getSegments();
151
        $params['disableGoals'] = $config->getDisableGoals();
152
        $params['configId'] = $config->getId();
153
154
        $params['profileSegments'] = $configHelper->getProfileSegments();
155
156
        return $this->render(
157
            '@KunstmaanDashboard/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();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method resetProfileId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
173
174
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_ProfileSelection'));
175
    }
176
177
    /**
178
     * @Route("/resetProperty", name="KunstmaanDashboardBundle_analytics_resetProperty")
179
     *
180
     * @throws AccessDeniedException
181
     */
182
    public function resetPropertyAction()
183
    {
184
        $this->denyAccessUnlessGranted('ROLE_SUPER_ADMIN');
185
186
        $em = $this->getDoctrine()->getManager();
187
        $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->resetPropertyId();
0 ignored issues
show
It seems like you code against a concrete implementation and not the interface Doctrine\Persistence\ObjectRepository as the method resetPropertyId() does only exist in the following implementations of said interface: Kunstmaan\DashboardBundl...alyticsConfigRepository.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
188
189
        return $this->redirect($this->generateUrl('KunstmaanDashboardBundle_Config'));
190
    }
191
}
192