Completed
Push — master ( 3e3269...414f6c )
by Valentyn
05:10
created

BaseController   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 85.19%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 73
ccs 23
cts 27
cp 0.8519
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A response() 0 11 2
A getLocales() 0 4 1
A getGuest() 0 13 1
A addMetaPaginationInfo() 0 11 1
A appendRolesToContextGroups() 0 18 4
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Controller;
5
6
use App\Guests\Entity\GuestSession;
7
use App\Guests\Repository\GuestRepository;
8
use App\Pagination\PaginatedCollectionInterface;
9
use App\Translation\TranslatedResponseTrait;
10
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
use Symfony\Component\HttpFoundation\Request;
13
14
/**
15
 * Class BaseController
16
 * @package App\Controller
17
 */
18
abstract class BaseController extends Controller implements ControllerInterface
19
{
20
    use TranslatedResponseTrait;
21
22
    /**
23
     * @param $data
24
     * @param int $status
25
     * @param array $headers
26
     * @param array $context
27
     * @return JsonResponse
28
     */
29 18
    protected function response($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
30
    {
31 18
        if ($data instanceof PaginatedCollectionInterface) {
32 5
            $data = $this->addMetaPaginationInfo($data);
33
        }
34
35 18
        $contextWithRoles = $this->appendRolesToContextGroups($context);
36 18
        $translatedContent = $this->translateResponse($data, $contextWithRoles);
37
38 18
        return $this->json($translatedContent, $status, $headers, $context);
39
    }
40
41
    protected function getLocales()
42
    {
43
        return $this->getParameter('locales');
44
    }
45
46 3
    protected function getGuest(): ?GuestSession
47
    {
48
        /** @var $request Request */
49 3
        $request = $this->get('request_stack')->getCurrentRequest();
50 3
        $guestSessionToken = (string)$request->get('guest_api_token', '');
51
52
        /** @var $guestRepository GuestRepository */
53 3
        $guestRepository = $this->getDoctrine()->getRepository(GuestSession::class);
54
55 3
        return $guestRepository->findOneBy([
56 3
            'token' => $guestSessionToken
57
        ]);
58
    }
59
60 5
    private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection)
61
    {
62
        return [
63 5
            'data' => $paginatedCollection->getItems(),
64
            'paging' => [
65 5
                'total' => $paginatedCollection->getTotal(),
66 5
                'offset' => $paginatedCollection->getOffset(),
67 5
                'limit' => $paginatedCollection->getLimit(),
68
            ]
69
        ];
70
    }
71
72 18
    private function appendRolesToContextGroups(?array $context): array
73
    {
74 18
        if ($this->getUser() === null) return $context;
75
76 5
        if ($context === null) {
77
            return [
78
                'groups' => $this->getUser()->getRoles(),
79
            ];
80
        }
81
82 5
        if (isset($context['groups'])) {
83 5
            $context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles());
84
        } else {
85
            $context['groups'] = $this->getUser()->getRoles();
86
        }
87
88 5
        return $context;
89
    }
90
}