1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface |
|
|
|
|
6
|
|
|
AS Security; |
7
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
|
|
|
8
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
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; |
|
|
|
|
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'; |
|
|
|
|
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; |
|
|
|
|
103
|
|
|
|
104
|
|
|
$data = $this->manager->getRepository($class)->findBy($arguments, $orderby, $limit, $offset); |
|
|
|
|
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
|
|
|
|
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths