HydratorService   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 18
eloc 60
c 6
b 0
f 0
dl 0
loc 136
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getMembers() 0 12 5
A error() 0 3 1
A getCount() 0 3 1
A __construct() 0 9 2
A item() 0 4 1
A serialize() 0 4 1
A data() 0 4 1
A getSearch() 0 29 2
A result() 0 9 1
A collectionData() 0 9 1
A collection() 0 9 1
A getBasicResponse() 0 13 1
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Securi...e\TokenStorageInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
 AS Security;
7
use Symfony\Component\Serializer\SerializerInterface;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Serializer\SerializerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\RequestStack;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpFoundation\RequestStack was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
10
11
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Exception;
13
14
class HydratorService
15
{
16
17
    private $request;
18
    private $uri;
19
20
    public function __construct(
21
        private  EntityManagerInterface $manager,
22
        private   SerializerInterface $serializer,
23
        RequestStack $requestStack
24
    ) {
25
26
        $this->serializer = $serializer;
27
        $this->request = $requestStack->getCurrentRequest();
28
        $this->uri = $this->request ? $this->request->getPathInfo() : '';
29
    }
30
    public function error($e)
31
    {
32
        return $e;
33
    }
34
35
    public function collectionData($data, $class, $groups,  mixed $arguments = [])
36
    {
37
        $response = $this->getBasicResponse($class);
38
39
        $response['member']      =   $this->data($data, $groups);
40
        $response['search']       =   $this->getSearch($class);
41
        $response['totalItems']   =   $this->getCount($class, $arguments);
42
43
        return $response;
44
    }
45
    public function collection($class, $groups,  mixed $arguments = [], int $limit = 0, int $page = 1, array $orderby = [])
46
    {
47
        $response = $this->getBasicResponse($class);
48
49
        $response['member']      =   $this->getMembers($class, $groups, $arguments, $limit, $page, $orderby);
50
        $response['search']       =   $this->getSearch($class);
51
        $response['totalItems']   =   $this->getCount($class, $arguments);
52
53
        return $response;
54
    }
55
56
    public function result($result)
57
    {
58
        //$response = $this->getBasicResponse($class);
59
        //$response['search']       =   $this->getSearch($class);
60
61
        $response['member']      =  $result;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
62
        $response['totalItems']   =   count($response['member']);
63
64
        return $response;
65
    }
66
67
    public function data($data, $groups)
68
    {
69
        $analisesSerialized = $this->serializer->serialize($data, 'jsonld', ['groups' => $groups]);
70
        return json_decode($analisesSerialized);
71
    }
72
73
74
    public function item($class, $id, $groups)
75
    {
76
        $data = $this->manager->getRepository($class)->find(preg_replace("/[^0-9]/", "", $id));
77
        return $this->data($data, $groups);
78
    }
79
80
    private function getBasicResponse($class)
81
    {
82
        $className = substr($class, strrpos($class, '\\') + 1);
83
84
        $response['@id']        = '/' . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $className)) . 's';
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
85
        $response['@context']   =   "/contexts/" . $className;
86
        $response['@type']      =   "Collection";
87
88
        $response['view'] = [
89
            '@id' =>   $this->uri,
90
            '@type' => 'PartialCollectionView'
91
        ];
92
        return $response;
93
    }
94
95
    private function getMembers($class, $groups, mixed $arguments = [], int $limit = 0, int $page = 1, array $orderby = [])
96
    {
97
98
        if ($limit < 1)
99
            $limit = $this->request->get('itemsPerPage') ?: 50;
100
101
        if ($page == 1)
102
            $offset = (($page = $this->request->get('page') ?: 1) - 1) * $limit;
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
103
104
        $data =     $this->manager->getRepository($class)->findBy($arguments, $orderby, $limit, $offset);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $offset does not seem to be defined for all execution paths leading up to this point.
Loading history...
105
106
        return $this->serialize($data, ['groups' => $groups]);
107
    }
108
109
    private function serialize($data, array $groups = [])
110
    {
111
        $analisesSerialized = $this->serializer->serialize($data, 'jsonld', $groups);
112
        return json_decode($analisesSerialized);
113
    }
114
115
116
    private function getCount($class, $arguments)
117
    {
118
        return $this->manager->getRepository($class)->count($arguments);
119
    }
120
121
    private function getSearch($class)
122
    {
123
124
        $metadata = $this->manager->getClassMetadata($class);
125
        $arguments = $metadata->getFieldNames();
126
        $search = [
127
            '@type' => 'IriTemplate',
128
            'template' =>   $this->uri . '{?' . implode(',', array_values($arguments)) . '}',
129
            'variableRepresentation' => 'BasicRepresentation',
130
            'mapping' => []
131
        ];
132
133
        foreach ($metadata->getFieldNames() as $field) {
134
135
            $search['mapping'][] = [
136
                '@type' => 'IriTemplateMapping',
137
                'variable' => $field,
138
                'property' => $field,
139
                'required' => false
140
            ];
141
            $search['mapping'][] = [
142
                '@type' => 'IriTemplateMapping',
143
                'variable' => $field . '[]',
144
                'property' => $field,
145
                'required' => false
146
            ];
147
        }
148
149
        return $search;
150
    }
151
}
152