ApiController::lastUserSubscribersByIdAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2
nc 2
nop 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