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

Collections   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 177
Duplicated Lines 61.58 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 6
dl 109
loc 177
ccs 0
cts 94
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 7 7 1
A getAll() 19 19 2
A getOne() 7 7 1
A delete() 7 7 1
A post() 14 14 1
A patch() 20 20 1
A getAllLogs() 35 35 4
A getOneLog() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Log\Factory as LogFactory;
24
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
25
use Tubee\Rest\Helper;
26
use Zend\Diactoros\Response;
27
28
class Collections
29
{
30
    /**
31
     * namespace factory.
32
     *
33
     * @var ResourceNamespaceFactory
34
     */
35
    protected $namespace_factory;
36
37
    /**
38
     * collection factory.
39
     *
40
     * @var CollectionFactory
41
     */
42
    protected $collection_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 View Code Duplication
    public function __construct(ResourceNamespaceFactory $namespace_factory, CollectionFactory $collection_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...
62
    {
63
        $this->namespace_factory = $namespace_factory;
64
        $this->collection_factory = $collection_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): 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
        $namespace = $this->namespace_factory->getOne($namespace);
80
81
        if (isset($query['watch'])) {
82
            $cursor = $this->collection_factory->watch($namespace, null, true, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
83
84
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
85
        }
86
87
        $collections = $namespace->getCollections($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
0 ignored issues
show
Unused Code introduced by
The call to ResourceNamespaceInterface::getCollections() 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, $collections);
90
    }
91
92
    /**
93
     * Entrypoint.
94
     */
95 View Code Duplication
    public function getOne(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...
96
    {
97
        $namespace = $this->namespace_factory->getOne($namespace);
98
        $collection = $namespace->getCollection($collection);
99
100
        return Helper::getOne($request, $identity, $collection);
101
    }
102
103
    /**
104
     * Delete.
105
     */
106 View Code Duplication
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection): 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...
107
    {
108
        $namespace = $this->namespace_factory->getOne($namespace);
109
        $this->collection_factory->deleteOne($namespace, $collection);
110
111
        return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
112
    }
113
114
    /**
115
     * Create.
116
     */
117 View Code Duplication
    public function post(ServerRequestInterface $request, Identity $identity, string $namespace): 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...
118
    {
119
        $body = $request->getParsedBody();
120
        $query = $request->getQueryParams();
121
122
        $namespace = $this->namespace_factory->getOne($namespace);
123
        $id = $this->collection_factory->add($namespace, $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...
124
125
        return new UnformattedResponse(
126
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
127
            $this->collection_factory->getOne($namespace, $body['name'])->decorate($request),
128
            ['pretty' => isset($query['pretty'])]
129
        );
130
    }
131
132
    /**
133
     * Patch.
134
     */
135 View Code Duplication
    public function patch(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...
136
    {
137
        $body = $request->getParsedBody();
138
        $query = $request->getQueryParams();
139
        $namespace = $this->namespace_factory->getOne($namespace);
140
        $collection = $namespace->getCollection($collection);
141
        $doc = ['data' => $collection->getData()];
142
143
        $patch = new Patch(json_encode($doc), json_encode($body));
144
        $patched = $patch->apply();
145
        $update = json_decode($patched, true);
146
147
        $this->collection_factory->update($collection, $update);
148
149
        return new UnformattedResponse(
150
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
151
            $this->collection_factory->getOne($namespace, $collection->getName())->decorate($request),
152
            ['pretty' => isset($query['pretty'])]
153
        );
154
    }
155
156
    /**
157
     * Entrypoint.
158
     */
159 View Code Duplication
    public function getAllLogs(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...
160
    {
161
        $query = array_merge([
162
            'offset' => 0,
163
            'limit' => 20,
164
        ], $request->getQueryParams());
165
166
        if (isset($query['watch'])) {
167
            $filter = [
168
                'fullDocument.context.namespace' => $namespace,
169
                'fullDocument.context.collection' => $collection,
170
            ];
171
172
            if (!empty($query['query'])) {
173
                $filter = ['$and' => [$filter, $query['query']]];
174
            }
175
176
            $logs = $this->log_factory->watch(null, isset($query['stream']), $filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
177
178
            return Helper::watchAll($request, $identity, $this->acl, $logs);
179
        }
180
181
        $filter = [
182
            'context.namespace' => $namespace,
183
            'context.collection' => $collection,
184
        ];
185
186
        if (!empty($query['query'])) {
187
            $filter = ['$and' => [$filter, $query['query']]];
188
        }
189
190
        $logs = $this->log_factory->getAll($filter, (int) $query['offset'], (int) $query['limit'], $query['sort']);
191
192
        return Helper::getAll($request, $identity, $this->acl, $logs);
193
    }
194
195
    /**
196
     * Entrypoint.
197
     */
198
    public function getOneLog(ServerRequestInterface $request, Identity $identity, string $namespace, string $collection, 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...
199
    {
200
        $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...
201
202
        return Helper::getOne($request, $identity, $resource);
203
    }
204
}
205