Completed
Push — 5.0 ( a48099...63af02 )
by
unknown
11:33
created

Controller/GoogleAnalyticsAJAXController.php (1 issue)

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\AdminBundle\FlashMessages\FlashTypes;
5
use Kunstmaan\DashboardBundle\Command\GoogleAnalyticsDataCollectCommand;
6
use Kunstmaan\DashboardBundle\Entity\AnalyticsGoal;
7
use Kunstmaan\DashboardBundle\Entity\AnalyticsSegment;
8
use Kunstmaan\DashboardBundle\Repository\AnalyticsOverviewRepository;
9
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\Console\Input\ArrayInput;
12
use Symfony\Component\Console\Output\NullOutput;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
class GoogleAnalyticsAJAXController extends Controller
17
{
18
19
    /**
20
     * @Route("/updateData", name="KunstmaanDashboardBundle_analytics_update")
21
     */
22
    public function runUpdateAction(Request $request)
23
    {
24
        $configId  = $request->query->get('configId');
25
        $segmentId = $request->query->get('segmentId');
26
27
        $command = new GoogleAnalyticsDataCollectCommand();
28
        $command->setContainer($this->container);
29
        $input  = new ArrayInput(array('--config' => $configId, '--segment' => $segmentId));
30
        $output = new NullOutput();
31
        $command->run($input, $output);
32
33
        return new JsonResponse(array(), 200, array('Content-Type' => 'application/json'));
34
    }
35
36
37
    /**
38
     * Return an ajax response with all data for an overview
39
     *
40
     * @Route("/getOverview/{id}", requirements={"id" = "\d+"}, name="KunstmaanDashboardBundle_analytics_overview_ajax")
41
     *
42
     */
43
    public function getOverviewAction($id)
44
    {
45
        $em = $this->getDoctrine()->getManager();
46
        /** @var AnalyticsOverviewRepository $analyticsOverviewRepository */
47
        $analyticsOverviewRepository = $em->getRepository('KunstmaanDashboardBundle:AnalyticsOverview');
48
        $overview                    = $analyticsOverviewRepository->find($id);
49
50
        // goals data
51
        $goals = array();
52
        foreach ($overview->getActiveGoals() as $key => $goal) {
53
            /** @var AnalyticsGoal $goal */
54
            $goals[$key]['name']      = $goal->getName();
55
            $goals[$key]['visits']    = $goal->getVisits();
56
            $goals[$key]['id']        = $goal->getId();
57
            $goals[$key]['chartData'] = json_decode($goal->getChartData());
58
        }
59
60
        // overview data
61
        $overviewData = array(
62
            'id'                       => $overview->getId(),
63
            'chartData'                => json_decode($overview->getChartData(), true),
64
            'chartDataMaxValue'        => $overview->getChartDataMaxValue(),
65
            'title'                    => $overview->getTitle(),
66
            'timespan'                 => $overview->getTimespan(),
67
            'startOffset'              => $overview->getStartOffset(),
68
            'sessions'                 => number_format($overview->getSessions()),
69
            'users'                    => number_format($overview->getUsers()),
70
            'pagesPerSession'          => round($overview->getPagesPerSession(), 2),
71
            'avgSessionDuration'       => $overview->getAvgSessionDuration(),
72
            'returningUsers'           => number_format($overview->getReturningUsers()),
73
            'newUsers'                 => round($overview->getNewUsers(), 2),
74
            'pageViews'                => number_format($overview->getPageViews()),
75
            'returningUsersPercentage' => $overview->getReturningUsersPercentage(),
76
            'newUsersPercentage'       => $overview->getNewUsersPercentage(),
77
        );
78
79
        // put all data in the return array
80
        $return = array(
81
            'responseCode' => 200,
82
            'overview'     => $overviewData,
83
            'goals'        => $goals,
84
        );
85
86
        // return json response
87
        return new JsonResponse($return, 200, array('Content-Type' => 'application/json'));
88
    }
89
90
    /* =============================== ACCOUNT =============================== */
91
92
    /**
93
     * @Route("/accounts/", name="kunstmaan_dashboard_ajax_accounts")
94
     */
95
    public function getAccountsAction(Request $request)
96
    {
97
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
98
        $configId     = $request->query->get('configId');
99
        if ($configId) {
100
            $configHelper->init($configId);
101
        }
102
103
        $accounts = $configHelper->getAccounts();
104
105
        return new JsonResponse($accounts, 200, array('Content-Type' => 'application/json'));
106
    }
107
108
    /**
109
     * @Route("/account/save", name="kunstmaan_dashboard_ajax_account_save")
110
     */
111 View Code Duplication
    public function saveAccountAction(Request $request)
112
    {
113
        $accountId    = $request->query->get('id');
114
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
115
        $configHelper->saveAccountId($accountId);
116
117
        return new JsonResponse();
118
    }
119
120
    /* =============================== PROPERTY =============================== */
121
122
    /**
123
     * @Route("/properties/", name="kunstmaan_dashboard_ajax_properties")
124
     */
125
    public function getPropertiesAction(Request $request)
126
    {
127
        $accountId    = $request->query->get('accountId');
128
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
129
        $configId     = $request->query->get('configId');
130
        if ($configId) {
131
            $configHelper->init($configId);
132
        }
133
134
        $properties = $configHelper->getProperties($accountId);
135
136
        return new JsonResponse($properties, 200, array('Content-Type' => 'application/json'));
137
    }
138
139
    /**
140
     * @Route("/property/save", name="kunstmaan_dashboard_ajax_property_save")
141
     */
142 View Code Duplication
    public function savePropertyAction(Request $request)
143
    {
144
        $propertyId   = $request->query->get('id');
145
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
146
        $configHelper->savePropertyId($propertyId);
147
148
        return new JsonResponse();
149
    }
150
151
    /* =============================== PROFILE =============================== */
152
153
    /**
154
     * @Route("/profiles/", name="kunstmaan_dashboard_ajax_profiles")
155
     */
156
    public function getProfilesAction(Request $request)
157
    {
158
        $accountId    = $request->query->get('accountId');
159
        $propertyId   = $request->query->get('propertyId');
160
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
161
        $configId     = $request->query->get('configId');
162
        if ($configId) {
163
            $configHelper->init($configId);
164
        }
165
166
        $profiles = $configHelper->getProfiles($accountId, $propertyId);
167
168
        return new JsonResponse($profiles, 200, array('Content-Type' => 'application/json'));
169
    }
170
171
    /**
172
     * @Route("/profile/save", name="kunstmaan_dashboard_ajax_profile_save")
173
     */
174 View Code Duplication
    public function saveProfileAction(Request $request)
175
    {
176
        $propertyId   = $request->query->get('id');
177
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
178
        $configHelper->saveProfileId($propertyId);
179
180
        return new JsonResponse();
181
    }
182
183
    /* =============================== CONFIG =============================== */
184
185
    /**
186
     * @Route("/config/save", name="kunstmaan_dashboard_ajax_config_save")
187
     */
188
    public function saveConfigAction(Request $request)
189
    {
190
        // get params
191
        $configId     = $request->query->get('configId');
192
        $accountId    = $request->query->get('accountId');
193
        $propertyId   = $request->query->get('propertyId');
194
        $profileId    = $request->query->get('profileId');
195
        $disableGoals = $request->query->get('disableGoals');
196
197
        // edit the config
198
        $em     = $this->getDoctrine()->getManager();
199
        $config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId);
200
        if ($accountId && $propertyId && $profileId) {
201
            $config->setAccountId($accountId);
202
            $config->setPropertyId($propertyId);
203
            $config->setProfileId($profileId);
204
        }
205
206
        $em->persist($config);
207
        $em->flush();
208
209
        // set the config name
210
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
211
        $configHelper->init($configId);
212
        $profile = $configHelper->getActiveProfile();
213
        $config->setName($profile['profileName']);
214
        $config->setDisableGoals($disableGoals);
215
216
        $em->persist($config);
217
        $em->flush();
218
219
        $this->addFlash(
220
            FlashTypes::SUCCESS,
221
            $this->get('translator')->trans('kuma_admin.ga_ajax_controller.flash.success')
222
        );
223
224
        return new JsonResponse();
225
    }
226
227
    /**
228
     * @Route("/config/remove", name="kunstmaan_dashboard_ajax_config_remove")
229
     */
230
    public function removeConfigAction(Request $request)
231
    {
232
        // get params
233
        $configId = $request->query->get('configId');
234
235
        // edit the config
236
        $em     = $this->getDoctrine()->getManager();
237
        $config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId);
238
        $em->remove($config);
239
        $em->flush();
240
241
        return new JsonResponse();
242
    }
243
244
    /**
245
     * @Route("/config/get", name="kunstmaan_dashboard_ajax_config_get")
246
     */
247
    public function getConfigAction(Request $request)
248
    {
249
        $em        = $this->getDoctrine()->getManager();
250
        $config    = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->findFirst();
251
        $accountId = $config->getAccountId();
252
253
        if (!$accountId) {
254
            return new JsonResponse();
255
        }
256
257
        $configHelper = $this->container->get('kunstmaan_dashboard.helper.google.analytics.config');
258
        $configHelper->getAccounts();
259
        $configHelper->getProperties();
260
        $configHelper->getProfiles();
261
    }
262
263
    /* =============================== SEGMENT =============================== */
264
265
    /**
266
     * @Route("/segment/add/", name="kunstmaan_dashboard_ajax_segment_add")
267
     */
268
    public function addSegmentAction(Request $request)
269
    {
270
        $configId = $request->query->get('configId');
271
        $em       = $this->getDoctrine()->getManager();
272
273
        // create a new segment
274
        $segment = new AnalyticsSegment();
275
        $query   = $request->query->get('query');
276
        $name    = $request->query->get('name');
277
        $segment->setQuery($query);
278
        $segment->setName($name);
279
280
        // add the segment to the config
281
        $config = $em->getRepository('KunstmaanDashboardBundle:AnalyticsConfig')->find($configId);
282
        $segment->setConfig($config);
0 ignored issues
show
$config is of type object, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
283
        $segments = $config->getSegments();
284
        $segments[] = $segment;
285
        $config->setSegments($segments);
286
287
        $em->persist($config);
288
        $em->flush();
289
290
        return new JsonResponse();
291
    }
292
293
    /**
294
     * @Route("/segment/delete", name="kunstmaan_dashboard_ajax_segment_delete")
295
     */
296
    public function deleteSegmentAction(Request $request)
297
    {
298
        $em = $this->getDoctrine()->getManager();
299
300
        $id = $request->query->get('id');
301
        $em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->deleteSegment($id);
302
303
        return new JsonResponse();
304
    }
305
306
    /**
307
     * @Route("/segment/edit", name="kunstmaan_dashboard_ajax_segment_edit")
308
     */
309
    public function editSegmentAction(Request $request)
310
    {
311
        $em = $this->getDoctrine()->getManager();
312
313
        $id      = $request->query->get('id');
314
        $query   = $request->query->get('query');
315
        $name    = $request->query->get('name');
316
        $segment = $em->getRepository('KunstmaanDashboardBundle:AnalyticsSegment')->find($id);
317
        $segment->setName($name);
318
        $segment->setQuery($query);
319
        $em->persist($segment);
320
        $em->flush();
321
322
        return new JsonResponse();
323
    }
324
}
325