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

Objects::getOneLog()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 6
crap 2
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\Collection\Factory as CollectionFactory;
23
use Tubee\DataObject\Factory as DataObjectFactory;
24
use Tubee\Log\Factory as LogFactory;
25
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
26
use Tubee\Rest\Helper;
27
use Tubee\Rest\Pager;
28
use Zend\Diactoros\Response;
29
30
class Objects
31
{
32
    /**
33
     * namespace factory.
34
     *
35
     * @var ResourceNamespaceFactory
36
     */
37
    protected $namespace_factory;
38
39
    /**
40
     * collection factory.
41
     *
42
     * @var CollectionFactory
43
     */
44
    protected $collection_factory;
45
46
    /**
47
     * Object factory.
48
     *
49
     * @var DataObjectFactory
50
     */
51
    protected $object_factory;
52
53
    /**
54
     * Acl.
55
     *
56
     * @var Acl
57
     */
58
    protected $acl;
59
60
    /**
61
     * Log factory.
62
     *
63
     * @var LogFactory
64
     */
65
    protected $log_factory;
66
67
    /**
68
     * Init.
69
     */
70 View Code Duplication
    public function __construct(ResourceNamespaceFactory $namespace_factory, CollectionFactory $collection_factory, DataObjectFactory $object_factory, Acl $acl, LogFactory $log_factory)
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...
71
    {
72
        $this->namespace_factory = $namespace_factory;
73
        $this->collection_factory = $collection_factory;
74
        $this->acl = $acl;
75
        $this->object_factory = $object_factory;
76
        $this->log_factory = $log_factory;
77
    }
78
79
    /**
80
     * Entrypoint.
81
     */
82
    public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): ResponseInterface
83
    {
84
        $query = array_merge([
85
            'offset' => 0,
86
            'limit' => 20,
87
        ], $request->getQueryParams());
88
89
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
90
91
        if (isset($query['watch'])) {
92
            $cursor = $this->object_factory->watch($collection, null, true, $query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to Factory::watch() 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...
93
94
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
95
        }
96
97
        $objects = $collection->getObjects($query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to CollectionInterface::getObjects() 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...
98
99
        return Helper::getAll($request, $identity, $this->acl, $objects);
100
    }
101
102
    /**
103
     * Entrypoint.
104
     */
105 View Code Duplication
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): 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...
106
    {
107
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
108
        $object = $collection->getObject(['name' => $object], false);
109
110
        return Helper::getOne($request, $identity, $object);
111
    }
112
113
    /**
114
     * Create object.
115
     */
116
    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...
117
    {
118
        $query = $request->getQueryParams();
119
120
        $body = array_merge([
121
            'data' => [],
122
            'endpoints' => null,
123
        ], $request->getParsedBody());
124
125
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
126
        $id = $collection->createObject($body, false, $body['endpoints']);
0 ignored issues
show
Bug introduced by
It seems like $body['endpoints'] can also be of type null; however, Tubee\Collection\Collect...terface::createObject() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
127
128
        return new UnformattedResponse(
129
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
130
            $collection->getObject(['_id' => $id], false)->decorate($request),
131
            ['pretty' => isset($query['pretty'])]
132
        );
133
    }
134
135
    /**
136
     * Entrypoint.
137
     */
138
    public function getHistory(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): 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...
139
    {
140
        $query = array_merge([
141
            'offset' => 0,
142
            'limit' => 20,
143
            'query' => [],
144
        ], $request->getQueryParams());
145
146
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
147
        $object = $collection->getObject(['name' => $object], false);
148
        $history = $object->getHistory();
149
        $body = Pager::fromRequest($history, $request);
150
151
        return new UnformattedResponse(
152
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
153
            $body,
154
            ['pretty' => isset($query['pretty'])]
155
        );
156
    }
157
158
    /**
159
     * Patch.
160
     */
161 View Code Duplication
    public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): 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...
162
    {
163
        $body = $request->getParsedBody();
164
        $query = $request->getQueryParams();
165
        $namespace = $this->namespace_factory->getOne($namespace);
166
        $collection = $namespace->getCollection($collection);
167
        $object = $collection->getObject(['name' => $object]);
168
        $doc = ['data' => $object->getData()];
169
        $patch = new Patch(json_encode($doc), json_encode($body));
170
        $patched = $patch->apply();
171
        $update = json_decode($patched, true);
172
173
        $this->object_factory->update($collection, $object, $update);
174
175
        return new UnformattedResponse(
176
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
177
            $collection->getObject(['_id' => $object->getId()])->decorate($request),
178
            ['pretty' => isset($query['pretty'])]
179
        );
180
    }
181
182
    /**
183
     * Delete.
184
     */
185 View Code Duplication
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): 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...
186
    {
187
        $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
188
        $this->object_factory->deleteOne($collection, $object);
189
190
        return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
191
    }
192
193
    /**
194
     * Entrypoint.
195
     */
196 View Code Duplication
    public function getAllLogs(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object): 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...
197
    {
198
        $query = array_merge([
199
            'offset' => 0,
200
            'limit' => 20,
201
        ], $request->getQueryParams());
202
203
        if (isset($query['watch'])) {
204
            $filter = [
205
                'fullDocument.context.namespace' => $namespace,
206
                'fullDocument.context.collection' => $collection,
207
                'fullDocument.context.object' => $object,
208
            ];
209
210
            if (!empty($query['query'])) {
211
                $filter = ['$and' => [$filter, $query['query']]];
212
            }
213
214
            $logs = $this->log_factory->watch(null, true, $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
215
216
            return Helper::watchAll($request, $identity, $this->acl, $logs);
217
        }
218
219
        $filter = [
220
            'context.namespace' => $namespace,
221
            'context.collection' => $collection,
222
            'context.object' => $object,
223
        ];
224
225
        if (!empty($query['query'])) {
226
            $filter = ['$and' => [$filter, $query['query']]];
227
        }
228
229
        $logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
230
231
        return Helper::getAll($request, $identity, $this->acl, $logs);
232
    }
233
234
    /**
235
     * Entrypoint.
236
     */
237
    public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, string $object, 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 $object 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...
238
    {
239
        $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...
240
241
        return Helper::getOne($request, $identity, $resource);
242
    }
243
}
244