Completed
Pull Request — master (#30)
by nonanerz
04:38
created

CalendarController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A userEventsAction() 0 19 4
A singleEventAction() 0 15 2
1
<?php
2
3
namespace AppBundle\Controller\Api;
4
5
use AppBundle\Entity\DTO\DtoEvent;
6
use AppBundle\Entity\Event;
7
use AppBundle\Exception\JsonHttpException;
8
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
9
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
use Symfony\Component\Routing\Annotation\Route;
12
13
class CalendarController extends Controller
14
{
15
    /**
16
     * @Route("/events")
17
     * @Method({"GET"})
18
     */
19
    public function userEventsAction()
20
    {
21
        $user = $this->getUser();
22
        $events = $user->getEvents();
23
        $calendar = $this->get('app.google_calendar');
24
        $googleEvents = [];
25
        foreach ($events as $event) {
26
            $googleEvents[] = $calendar
27
                ->getEventById($event->getGoogleId());
28
        }
29
        $events = [];
30
        foreach ($googleEvents as $event) {
31
            if ($event) {
32
                $events[] = new DtoEvent($event);
33
            }
34
        }
35
36
        return new JsonResponse(['events' => $events]);
37
    }
38
39
    /**
40
     * @Route("/events/{id}")
41
     * @Method("GET")
42
     */
43
    public function singleEventAction($id)
44
    {
45
        /** @var Event $event */
46
        $event = $this->getDoctrine()->getRepository('AppBundle:Event')
47
            ->findByGoogleId($id);
48
        $user = $event->getUsers()->first();
0 ignored issues
show
Bug introduced by
The method first cannot be called on $event->getUsers() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
        if (!$user) {
50
            throw new JsonHttpException(404, 'User not found.');
51
        }
52
        $googleEvent = $this->get('app.google_calendar')
53
            ->getEventById($id);
54
        $event = new DtoEvent($googleEvent);
55
56
        return new JsonResponse(['event' => $event]);
57
    }
58
}
59