ApiController   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 5
dl 0
loc 33
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A lastUserSubscribersByIdAction() 0 25 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller;
4
5
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6
use Skobkin\Bundle\PointToolsBundle\Entity\{SubscriptionEvent, User};
7
use Skobkin\Bundle\PointToolsBundle\Repository\SubscriptionEventRepository;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Response;
10
11
class ApiController
12
{
13
    /**
14
     * Returns last user subscribers log
15
     *
16
     * @ParamConverter("user", class="SkobkinPointToolsBundle:User")
17
     */
18
    public function lastUserSubscribersByIdAction(User $user, SubscriptionEventRepository $subscriptionEventRepository): Response
19
    {
20
        $qb = $subscriptionEventRepository->createQueryBuilder('se');
21
        $qb
22
            ->select(['se', 'sub'])
23
            ->innerJoin('se.subscriber', 'sub')
24
            ->where($qb->expr()->eq('se.author', ':author'))
25
            ->orderBy('se.date', 'desc')
26
            ->setParameter('author', $user)
27
            ->setMaxResults(20)
28
        ;
29
30
        $data = [];
31
32
        /** @var SubscriptionEvent $event */
33
        foreach ($qb->getQuery()->getResult() as $event) {
34
            $data[] = [
35
                'user' => $event->getSubscriber()->getLogin(),
36
                'action' => $event->getAction(),
37
                'datetime' => $event->getDate()->format('d.m.Y H:i:s'),
38
            ];
39
        }
40
41
        return new JsonResponse($data);
42
    }
43
}
44