Passed
Push — master ( dd424d...da8d36 )
by Luiz Kim
09:52 queued 07:21
created

HydratorService::error()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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