1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ControleOnline\Service; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Security\Core\Security; |
|
|
|
|
6
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
|
|
|
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
|
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class HydratorService |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Entity Manager |
17
|
|
|
* |
18
|
|
|
* @var EntityManagerInterface |
19
|
|
|
*/ |
20
|
|
|
private $manager = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Security |
24
|
|
|
* |
25
|
|
|
* @var Security |
26
|
|
|
*/ |
27
|
|
|
private $security = null; |
28
|
|
|
private $serializer; |
29
|
|
|
private $request; |
30
|
|
|
private $uri; |
31
|
|
|
|
32
|
|
|
public function __construct( |
33
|
|
|
Security $security, |
34
|
|
|
EntityManagerInterface $entityManager, |
35
|
|
|
SerializerInterface $serializer, |
36
|
|
|
RequestStack $requestStack |
37
|
|
|
) { |
38
|
|
|
$this->security = $security; |
39
|
|
|
$this->manager = $entityManager; |
40
|
|
|
$this->serializer = $serializer; |
41
|
|
|
$this->request = $requestStack->getCurrentRequest(); |
42
|
|
|
$this->uri = $this->request ? $this->request->getPathInfo() : ''; |
43
|
|
|
} |
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['hydra:member'] = $this->getMembers($class, $groups, $arguments, $limit, $page, $orderby); |
50
|
|
|
$response['hydra:search'] = $this->getSearch($class); |
51
|
|
|
$response['hydra:totalItems'] = $this->getCount($class, $arguments); |
52
|
|
|
|
53
|
|
|
return $response; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function result($result) |
57
|
|
|
{ |
58
|
|
|
//$response = $this->getBasicResponse($class); |
59
|
|
|
//$response['hydra:search'] = $this->getSearch($class); |
60
|
|
|
|
61
|
|
|
$response['hydra:member'] = $result; |
|
|
|
|
62
|
|
|
$response['hydra:totalItems'] = count($response['hydra:member']); |
63
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function item($class, $id, $groups) |
68
|
|
|
{ |
69
|
|
|
$data = $this->manager->getRepository($class)->find(preg_replace("/[^0-9]/", "", $id)); |
70
|
|
|
$analisesSerialized = $this->serializer->serialize($data, 'jsonld', ['groups' => $groups]); |
71
|
|
|
return json_decode($analisesSerialized); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function getBasicResponse($class) |
75
|
|
|
{ |
76
|
|
|
$className = substr($class, strrpos($class, '\\') + 1); |
77
|
|
|
|
78
|
|
|
$response['@id'] = '/' . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $className)) . 's'; |
|
|
|
|
79
|
|
|
$response['@context'] = "/contexts/" . $className; |
80
|
|
|
$response['@type'] = "hydra:Collection"; |
81
|
|
|
|
82
|
|
|
$response['hydra:view'] = [ |
83
|
|
|
'@id' => $this->uri, |
84
|
|
|
'@type' => 'hydra:PartialCollectionView' |
85
|
|
|
]; |
86
|
|
|
return $response; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function getMembers($class, $groups, mixed $arguments = [], int $limit = 0, int $page = 1, array $orderby = []) |
90
|
|
|
{ |
91
|
|
|
|
92
|
|
|
if ($limit < 1) |
93
|
|
|
$limit = $this->request->get('itemsPerPage') ?: 50; |
94
|
|
|
|
95
|
|
|
if ($page == 1) |
96
|
|
|
$offset = (($page = $this->request->get('page') ?: 1) - 1) * $limit; |
|
|
|
|
97
|
|
|
|
98
|
|
|
$data = $this->manager->getRepository($class)->findBy($arguments, $orderby, $limit, $offset); |
|
|
|
|
99
|
|
|
|
100
|
|
|
return $this->serialize($data, ['groups' => $groups]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function serialize($data, array $groups = []) |
104
|
|
|
{ |
105
|
|
|
$analisesSerialized = $this->serializer->serialize($data, 'jsonld', $groups); |
106
|
|
|
return json_decode($analisesSerialized); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
private function getCount($class, $arguments) |
111
|
|
|
{ |
112
|
|
|
return $this->manager->getRepository($class)->count($arguments); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function getSearch($class) |
116
|
|
|
{ |
117
|
|
|
|
118
|
|
|
$metadata = $this->manager->getClassMetadata($class); |
119
|
|
|
$arguments = $metadata->getFieldNames(); |
120
|
|
|
$search = [ |
121
|
|
|
'@type' => 'hydra:IriTemplate', |
122
|
|
|
'hydra:template' => $this->uri . '{?' . implode(',', array_values($arguments)) . '}', |
123
|
|
|
'hydra:variableRepresentation' => 'BasicRepresentation', |
124
|
|
|
'hydra:mapping' => [] |
125
|
|
|
]; |
126
|
|
|
|
127
|
|
|
foreach ($metadata->getFieldNames() as $field) { |
128
|
|
|
|
129
|
|
|
$search['hydra:mapping'][] = [ |
130
|
|
|
'@type' => 'IriTemplateMapping', |
131
|
|
|
'variable' => $field, |
132
|
|
|
'property' => $field, |
133
|
|
|
'required' => false |
134
|
|
|
]; |
135
|
|
|
$search['hydra:mapping'][] = [ |
136
|
|
|
'@type' => 'IriTemplateMapping', |
137
|
|
|
'variable' => $field . '[]', |
138
|
|
|
'property' => $field, |
139
|
|
|
'required' => false |
140
|
|
|
]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $search; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
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