Completed
Push — master ( 316baf...2178d1 )
by Raffael
67:25 queued 62:39
created

Endpoints::getAllLogs()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 37

Duplication

Lines 37
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 37
loc 37
ccs 0
cts 29
cp 0
rs 9.328
c 0
b 0
f 0
cc 4
nc 4
nop 5
crap 20
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\Rest\v1;
13
14
use Fig\Http\Message\StatusCodeInterface;
15
use Lcobucci\ContentNegotiation\UnformattedResponse;
16
use Micro\Auth\Identity;
17
use MongoDB\BSON\ObjectIdInterface;
18
use Psr\Http\Message\ResponseInterface;
19
use Psr\Http\Message\ServerRequestInterface;
20
use Rs\Json\Patch;
21
use Tubee\Acl;
22
use Tubee\Endpoint\Factory as EndpointFactory;
23
use Tubee\Log\Factory as LogFactory;
24
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
25
use Tubee\Rest\Helper;
26
use Zend\Diactoros\Response;
27
28
class Endpoints
29
{
30
    /**
31
     * namespace factory.
32
     *
33
     * @var ResourceNamespaceFactory
34
     */
35
    protected $namespace_factory;
36
37
    /**
38
     * Endpoint factory.
39
     *
40
     * @var EndpointFactory
41
     */
42
    protected $endpoint_factory;
43
44
    /**
45
     * Acl.
46
     *
47
     * @var Acl
48
     */
49
    protected $acl;
50
51
    /**
52
     * Log factory.
53
     *
54
     * @var LogFactory
55
     */
56
    protected $log_factory;
57
58
    /**
59
     * Init.
60
     */
61
    public function __construct(ResourceNamespaceFactory $namespace_factory, EndpointFactory $endpoint_factory, Acl $acl, LogFactory $log_factory)
62
    {
63
        $this->namespace_factory = $namespace_factory;
64
        $this->endpoint_factory = $endpoint_factory;
65
        $this->acl = $acl;
66
        $this->log_factory = $log_factory;
67
    }
68
69
    /**
70
     * Entrypoint.
71
     */
72 View Code Duplication
    public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
73
    {
74
        $query = array_merge([
75
            'offset' => 0,
76
            'limit' => 20,
77
        ], $request->getQueryParams());
78
79
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
80
81
        if (isset($query['watch'])) {
82
            $cursor = $this->endpoint_factory->watch($collection, null, true, $query['query'], $query['offset'], $query['limit'], $query['sort']);
83
84
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
85
        }
86
87
        $endpoints = $collection->getEndpoints($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to CollectionInterface::getEndpoints() has too many arguments starting with $query['sort'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
88
89
        return Helper::getAll($request, $identity, $this->acl, $endpoints);
90
    }
91
92
    /**
93
     * Entrypoint.
94
     */
95 View Code Duplication
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
    {
97
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
98
        $endpoint = $collection->getEndpoint($endpoint);
99
100
        return Helper::getOne($request, $identity, $endpoint);
101
    }
102
103
    /**
104
     * Create.
105
     */
106 View Code Duplication
    public function post(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
    {
108
        $body = $request->getParsedBody();
109
        $query = $request->getQueryParams();
110
111
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
112
        $id = $this->endpoint_factory->add($collection, $body);
0 ignored issues
show
Unused Code introduced by
$id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
113
114
        return new UnformattedResponse(
115
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
116
            $this->endpoint_factory->getOne($collection, $body['name'])->decorate($request),
117
            ['pretty' => isset($query['pretty'])]
118
        );
119
    }
120
121
    /**
122
     * Delete.
123
     */
124 View Code Duplication
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
127
        $this->endpoint_factory->deleteOne($collection, $endpoint);
128
129
        return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
130
    }
131
132
    /**
133
     * Patch.
134
     */
135 View Code Duplication
    public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $identity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $body = $request->getParsedBody();
138
        $query = $request->getQueryParams();
139
        $namespace = $this->namespace_factory->getOne($namespace);
140
        $collection = $namespace->getCollection($collection);
141
        $endpoint = $collection->getEndpoint($endpoint);
142
        $doc = ['data' => $endpoint->getData(), 'secrets' => $endpoint->getSecrets()];
143
144
        $patch = new Patch(json_encode($doc), json_encode($body));
145
        $patched = $patch->apply();
146
        $update = json_decode($patched, true);
147
148
        $this->endpoint_factory->update($endpoint, $update);
149
150
        return new UnformattedResponse(
151
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
152
            $collection->getEndpoint($endpoint->getName())->decorate($request),
153
            ['pretty' => isset($query['pretty'])]
154
        );
155
    }
156
157
    /**
158
     * Entrypoint.
159
     */
160
    public function getAllObjects(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
161
    {
162
        $query = array_merge([
163
            'offset' => 0,
164
            'limit' => 20,
165
        ], $request->getQueryParams());
166
167
        $endpoint = $this->namespace_factory->getOne($namespace)->getCollection($collection)->getEndpoint($endpoint);
168
        $endpoint->setup();
169
        $objects = $endpoint->getAll($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to EndpointInterface::getAll() has too many arguments starting with (int) $query['offset'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
170
171
        return Helper::getAll($request, $identity, $this->acl, $objects);
172
    }
173
174
    /**
175
     * Entrypoint.
176
     */
177 View Code Duplication
    public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint): ResponseInterface
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
    {
179
        $query = array_merge([
180
            'offset' => 0,
181
            'limit' => 20,
182
        ], $request->getQueryParams());
183
184
        if (isset($query['watch'])) {
185
            $filter = [
186
                'fullDocument.context.namespace' => $namespace,
187
                'fullDocument.context.collection' => $collection,
188
                'fullDocument.context.endpoint' => $endpoint,
189
            ];
190
191
            if (!empty($query['query'])) {
192
                $filter = ['$and' => [$filter, $query['query']]];
193
            }
194
195
            $logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
196
197
            return Helper::watchAll($request, $identity, $this->acl, $logs);
198
        }
199
200
        $filter = [
201
            'context.namespace' => $namespace,
202
            'context.collection' => $collection,
203
            'context.endpoint' => $endpoint,
204
        ];
205
206
        if (!empty($query['query'])) {
207
            $filter = ['$and' => [$filter, $query['query']]];
208
        }
209
210
        $logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
211
212
        return Helper::getAll($request, $identity, $this->acl, $logs);
213
    }
214
215
    /**
216
     * Entrypoint.
217
     */
218
    public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $endpoint, ObjectIdInterface $log): ResponseInterface
0 ignored issues
show
Unused Code introduced by
The parameter $namespace is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $collection is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $endpoint is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
219
    {
220
        $resource = $this->log_factory->getOne($log);
0 ignored issues
show
Bug introduced by
The call to getOne() misses a required argument $log.

This check looks for function calls that miss required arguments.

Loading history...
221
222
        return Helper::getOne($request, $identity, $resource);
223
    }
224
}
225