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

AccessRules::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\AccessRule\Factory as AccessRuleFactory;
21
use Tubee\Acl;
22
use Tubee\Rest\Helper;
23
use Zend\Diactoros\Response;
24
25 View Code Duplication
class AccessRules
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
     * rule factory.
29
     *
30
     * @var AccessRuleFactory
31
     */
32
    protected $rule_factory;
33
34
    /**
35
     * Acl.
36
     *
37
     * @var Acl
38
     */
39
    protected $acl;
40
41
    /**
42
     * Init.
43
     */
44
    public function __construct(AccessRuleFactory $rule_factory, Acl $acl)
45
    {
46
        $this->rule_factory = $rule_factory;
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->rule_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
        $rules = $this->rule_factory->getAll($query['query'], $query['offset'], $query['limit'], $query['sort']);
67
68
        return Helper::getAll($request, $identity, $this->acl, $rules);
69
    }
70
71
    /**
72
     * Entrypoint.
73
     */
74
    public function getOne(ServerRequestInterface $request, Identity $identity, string $rule): ResponseInterface
75
    {
76
        $resource = $this->rule_factory->getOne($rule);
77
78
        return Helper::getOne($request, $identity, $resource);
79
    }
80
81
    /**
82
     * Add new access rule.
83
     */
84
    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...
85
    {
86
        $body = $request->getParsedBody();
87
        $query = $request->getQueryParams();
88
        $id = $this->rule_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...
89
        $rule = $this->rule_factory->getOne($body['name']);
90
91
        return new UnformattedResponse(
92
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
93
            $rule->decorate($request),
94
            ['pretty' => isset($query['pretty'])]
95
        );
96
    }
97
98
    /**
99
     * Create or replace access rule.
100
     */
101
    public function put(ServerRequestInterface $request, Identity $identity, string $rule): 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...
102
    {
103
        $body = $request->getParsedBody();
104
        $query = $request->getQueryParams();
105
106
        if ($this->rule_factory->has($rule)) {
107
            $this->rule_factory->update($rule, $body);
0 ignored issues
show
Documentation introduced by
$rule is of type string, but the function expects a object<Tubee\AccessRule\AccessRuleInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
            $rule = $this->rule_factory->getOne($rule);
109
110
            return new UnformattedResponse(
111
                (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
112
                $rule->decorate($request),
113
                ['pretty' => isset($query['pretty'])]
114
            );
115
        }
116
117
        $body['name'] = $rule;
118
        $id = $this->rule_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...
119
        $rule = $this->rule_factory->getOne($body['name']);
120
121
        return new UnformattedResponse(
122
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
123
            $rule->decorate($request),
124
            ['pretty' => isset($query['pretty'])]
125
        );
126
    }
127
128
    /**
129
     * Delete access rule.
130
     */
131
    public function delete(ServerRequestInterface $request, Identity $identity, string $rule): 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...
132
    {
133
        $body = $request->getParsedBody();
0 ignored issues
show
Unused Code introduced by
$body 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...
134
        $this->rule_factory->deleteOne($rule);
135
136
        return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
137
    }
138
139
    /**
140
     * Patch.
141
     */
142
    public function patch(ServerRequestInterface $request, Identity $identity, string $rule): 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...
143
    {
144
        $body = $request->getParsedBody();
145
        $query = $request->getQueryParams();
146
        $rule = $this->rule_factory->getOne($rule);
147
        $doc = ['data' => $rule->getData()];
148
149
        $patch = new Patch(json_encode($doc), json_encode($body));
150
        $patched = $patch->apply();
151
        $update = json_decode($patched, true);
152
153
        $this->rule_factory->update($rule, $update);
154
155
        return new UnformattedResponse(
156
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
157
            $this->rule_factory->getOne($rule->getName())->decorate($request),
158
            ['pretty' => isset($query['pretty'])]
159
        );
160
    }
161
}
162