Completed
Push — master ( d44c39...321b0a )
by Valentyn
04:27
created

BaseController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 92.68%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 7
dl 0
loc 100
ccs 38
cts 41
cp 0.9268
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A items() 0 4 1
A response() 0 13 3
A getLocales() 0 4 1
A getGuest() 0 13 1
A addMetaPaginationInfo() 0 11 1
A prepareResponseData() 0 15 3
A appendRolesToContextGroups() 0 20 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Controller;
6
7
use App\Guests\Entity\GuestSession;
8
use App\Guests\Repository\GuestRepository;
9
use App\Pagination\PaginatedCollectionInterface;
10
use App\Transformer\Transformer;
11
use App\Translation\TranslatedResponseTrait;
12
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Class BaseController.
18
 */
19
abstract class BaseController extends Controller implements ControllerInterface
20
{
21
    use TranslatedResponseTrait;
22
23 19
    public function items($data, Transformer $transformer): JsonResponse
24
    {
25 19
        return $this->response($data, 200, [], [], $transformer);
26
    }
27
28
    /**
29
     * @param $data
30
     * @param int   $status
31
     * @param array $headers
32
     * @param array $context
33
     * @param Transformer $transformer
34
     *
35
     * @return JsonResponse
36
     */
37 44
    public function response($data, int $status = 200, array $headers = [], array $context = [], ?Transformer $transformer = null): JsonResponse
38
    {
39 44
        if ($data instanceof PaginatedCollectionInterface) {
40 25
            $data = $this->addMetaPaginationInfo($data);
41
        }
42
43 44
        $contextWithRoles = $this->appendRolesToContextGroups($context);
44 44
        $response = $this->translateResponse($data, $contextWithRoles);
45 44
        if ($transformer !== null) {
46 19
            $response = $this->prepareResponseData($response, $transformer);
47
        }
48 44
        return $this->json($response, $status, $headers, $context);
49
    }
50
51
    public function getLocales()
52
    {
53
        return $this->getParameter('locales');
54
    }
55
56 17
    public function getGuest(): ?GuestSession
57
    {
58
        /** @var $request Request */
59 17
        $request = $this->get('request_stack')->getCurrentRequest();
60 17
        $guestSessionToken = (string) $request->get('guest_api_token', '');
61
62
        /** @var $guestRepository GuestRepository */
63 17
        $guestRepository = $this->getDoctrine()->getRepository(GuestSession::class);
64
65 17
        return $guestRepository->findOneBy([
66 17
            'token' => $guestSessionToken,
67
        ]);
68
    }
69
70 25
    private function addMetaPaginationInfo(PaginatedCollectionInterface $paginatedCollection)
71
    {
72
        return [
73 25
            'data' => $paginatedCollection->getItems(),
74
            'paging' => [
75 25
                'total' => $paginatedCollection->getTotal(),
76 25
                'offset' => $paginatedCollection->getOffset(),
77 25
                'limit' => $paginatedCollection->getLimit(),
78
            ],
79
        ];
80
    }
81
82 44
    private function appendRolesToContextGroups(?array $context): array
83
    {
84 44
        if ($this->getUser() === null) {
85 31
            return $context;
86
        }
87
88 20
        if ($context === null) {
89
            return [
90
                'groups' => $this->getUser()->getRoles(),
91
            ];
92
        }
93
94 20
        if (isset($context['groups'])) {
95 17
            $context['groups'] = array_merge($context['groups'], $this->getUser()->getRoles());
96
        } else {
97 3
            $context['groups'] = $this->getUser()->getRoles();
98
        }
99
100 20
        return $context;
101
    }
102
103 19
    private function prepareResponseData(array $data, Transformer $transformer): array
104
    {
105 19
        $response = [];
106 19
        $data = $transformer->process($data);
107 19
        foreach ($data as $key => $value) {
108 19
            if (is_array($value)) {
109
                //$value = $transformer->process($value);
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
110 19
                $response[$key] = $this->prepareResponseData($value, $transformer);
111 19
                continue;
112
            }
113 19
            $response[$key] = $value;
114
        }
115
116 19
        return $response;
117
    }
118
}
119