Passed
Push — master ( 97b4b0...ba7d8d )
by Luiz Kim
02:00
created

HydratorService::getSearch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 29
rs 9.6333
cc 2
nc 2
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
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;
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['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';
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...
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;
0 ignored issues
show
Unused Code introduced by
The assignment to $page is dead and can be removed.
Loading history...
97
98
        $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...
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