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

ResourceNamespaces::watchAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 2
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 Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Rs\Json\Patch;
20
use Tubee\Acl;
21
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
22
use Tubee\Rest\Helper;
23
use Zend\Diactoros\Response;
24
25 View Code Duplication
class ResourceNamespaces
0 ignored issues
show
Duplication introduced by
This class 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...
26
{
27
    /**
28
     * ResourceNamespace factory.
29
     *
30
     * @var ResourceNamespaceFactory
31
     */
32
    protected $namespace_factory;
33
34
    /**
35
     * Acl.
36
     *
37
     * @var Acl
38
     */
39
    protected $acl;
40
41
    /**
42
     * Init.
43
     */
44
    public function __construct(ResourceNamespaceFactory $namespace, Acl $acl)
45
    {
46
        $this->namespace_factory = $namespace;
47
        $this->acl = $acl;
48
    }
49
50
    /**
51
     * Entrypoint.
52
     */
53
    public function getAll(ServerRequestInterface $request, Identity $identity): ResponseInterface
54
    {
55
        $query = array_merge([
56
            'offset' => 0,
57
            'limit' => 20,
58
        ], $request->getQueryParams());
59
60
        if (isset($query['watch'])) {
61
            $cursor = $this->namespace_factory->watch(null, true, $query['query'], $query['offset'], $query['limit'], $query['sort']);
62
63
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
64
        }
65
66
        $namespaces = $this->namespace_factory->getAll($query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
67
68
        return Helper::getAll($request, $identity, $this->acl, $namespaces);
69
    }
70
71
    /**
72
     * Entrypoint.
73
     */
74
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface
75
    {
76
        $resource = $this->namespace_factory->getOne($namespace);
77
78
        return Helper::getOne($request, $identity, $resource);
79
    }
80
81
    /**
82
     * Delete.
83
     */
84
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace): 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...
85
    {
86
        $this->namespace_factory->deleteOne($namespace);
87
88
        return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
89
    }
90
91
    /**
92
     * Patch.
93
     */
94
    public function patch(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...
95
    {
96
        $body = $request->getParsedBody();
97
        $query = $request->getQueryParams();
98
        $namespace = $this->namespace_factory->getOne($namespace);
99
        $doc = ['data' => $namespace->getData()];
100
101
        $patch = new Patch(json_encode($doc), json_encode($body));
102
        $patched = $patch->apply();
103
        $update = json_decode($patched, true);
104
        $this->namespace_factory->update($namespace, $update);
105
106
        return new UnformattedResponse(
107
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
108
            $this->namespace_factory->getOne($namespace->getName())->decorate($request),
109
            ['pretty' => isset($query['pretty'])]
110
        );
111
    }
112
113
    /**
114
     * Create.
115
     */
116
    public function post(ServerRequestInterface $request, Identity $identity): 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
        $body = (array) $request->getParsedBody();
119
        $id = $this->namespace_factory->add($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...
120
        $query = $request->getQueryParams();
121
122
        return new UnformattedResponse(
123
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
124
            $this->namespace_factory->getOne($body['name'])->decorate($request),
125
            ['pretty' => isset($query['pretty'])]
126
        );
127
    }
128
}
129