Completed
Push — master ( d47add...fca143 )
by Valentyn
02:52
created

BaseController::getGuest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 0
crap 1
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 3
    protected function getGuest(): ?GuestSession
42
    {
43
        /** @var $request Request */
44 3
        $request = $this->get('request_stack')->getCurrentRequest();
45 3
        $guestSessionToken = (string)$request->get('guest_api_token', '');
46
47
        /** @var $guestRepository GuestRepository */
48 3
        $guestRepository = $this->getDoctrine()->getRepository(GuestSession::class);
49
50 3
        return $guestRepository->findOneBy([
51 3
            'token' => $guestSessionToken
52
        ]);
53
    }
54
55 5
    private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection)
56
    {
57
        return [
58 5
            'data' => $paginatedCollection->getItems(),
59
            'paging' => [
60 5
                'total' => $paginatedCollection->getTotal(),
61 5
                'offset' => $paginatedCollection->getOffset(),
62 5
                'limit' => $paginatedCollection->getLimit(),
63
            ]
64
        ];
65
    }
66
67 18
    private function appendRolesToContextGroups(?array $context): array
68
    {
69 18
        if ($this->getUser() === null) return $context;
70
71 5
        if ($context === null) {
72
            return [
73
                'groups' => $this->getUser()->getRoles(),
74
            ];
75
        }
76
77 5
        if (isset($context['groups'])) {
78 5
            $context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles());
79
        } else {
80
            $context['groups'] = $this->getUser()->getRoles();
81
        }
82
83 5
        return $context;
84
    }
85
}