Completed
Pull Request — master (#358)
by Leny
24:06
created

BrowseEventController::heartbeatAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace Victoire\Bundle\AnalyticsBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\HttpFoundation\JsonResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\HttpFoundation\Response;
10
use Victoire\Bundle\AnalyticsBundle\Entity\BrowseEvent;
11
use Victoire\Bundle\UserBundle\Entity\User;
12
13
/**
14
 * @Route("/browseEvent")
15
 */
16
class BrowseEventController extends Controller
17
{
18
    /**
19
     * @Route("/track/{viewReferenceId}/{referer}", name="victoire_analytics_track")
20
     */
21
    public function trackAction(Request $request, $viewReferenceId, $referer = null)
22
    {
23
        $browseEvent = new BrowseEvent();
24
        $browseEvent->setViewReferenceId($viewReferenceId);
25
        $browseEvent->setIp($request->getClientIp());
26
        $browseEvent->setReferer($referer);
27
        if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
28
            $browseEvent->setAuthor($this->getUser());
0 ignored issues
show
Documentation introduced by
$this->getUser() is of type null|object, but the function expects a object<Victoire\Bundle\UserBundle\Model\User>.

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...
29
        }
30
        $entityManager = $this->get('doctrine.orm.entity_manager');
31
        $entityManager->persist($browseEvent);
32
        $entityManager->flush();
33
34
        return new Response();
35
    }
36
37
    /**
38
     * @Route("/heartbeat/{id}", name="victoire_analytics_heartbeat")
39
     */
40
    public function heartbeatAction($id)
41
    {
42
        $entityManager = $this->get('doctrine.orm.entity_manager');
43
        /** @var User $user */
44
        $user = $entityManager->getRepository($this->getParameter('victoire_core.user_class'))->find($id);
45
        $user->setHeartbeat(new \DateTime());
46
        $entityManager->flush();
47
48
        return new JsonResponse(['Glad you\'re alive :beating-heart:']);
49
    }
50
}
51