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

AccessRoles::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 Psr\Log\LoggerInterface;
20
use Rs\Json\Patch;
21
use Tubee\AccessRole\Factory as AccessRoleFactory;
22
use Tubee\Acl;
23
use Tubee\Rest\Helper;
24
use Zend\Diactoros\Response;
25
26 View Code Duplication
class AccessRoles
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...
27
{
28
    /**
29
     * role factory.
30
     *
31
     * @var AccessRoleFactory
32
     */
33
    protected $role_factory;
34
35
    /**
36
     * Acl.
37
     *
38
     * @var Acl
39
     */
40
    protected $acl;
41
42
    /**
43
     * Logger.
44
     *
45
     * @var LoggerInterface
46
     */
47
    protected $logger;
48
49
    /**
50
     * Init.
51
     */
52
    public function __construct(AccessRoleFactory $role_factory, Acl $acl, LoggerInterface $logger)
53
    {
54
        $this->role_factory = $role_factory;
55
        $this->acl = $acl;
56
        $this->logger = $logger;
57
    }
58
59
    /**
60
     * Entrypoint.
61
     */
62
    public function getAll(ServerRequestInterface $request, Identity $identity): ResponseInterface
63
    {
64
        $query = array_merge([
65
            'offset' => 0,
66
            'limit' => 20,
67
        ], $request->getQueryParams());
68
69
        if (isset($query['watch'])) {
70
            $cursor = $this->role_factory->watch(null, true, $query['query'], $query['offset'], $query['limit'], $query['sort']);
71
72
            return Helper::watchAll($request, $identity, $this->acl, $cursor);
73
        }
74
75
        $roles = $this->role_factory->getAll($query['query'], $query['offset'], $query['limit'], $query['sort']);
76
77
        return Helper::getAll($request, $identity, $this->acl, $roles);
78
    }
79
80
    /**
81
     * Entrypoint.
82
     */
83
    public function getOne(ServerRequestInterface $request, Identity $identity, string $role): ResponseInterface
84
    {
85
        $resource = $this->role_factory->getOne($role);
86
87
        return Helper::getOne($request, $identity, $resource);
88
    }
89
90
    /**
91
     * Add new access role.
92
     */
93
    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...
94
    {
95
        $body = $request->getParsedBody();
96
        $query = $request->getQueryParams();
97
        $id = $this->role_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...
98
        $role = $this->role_factory->getOne($body['name']);
99
100
        return new UnformattedResponse(
101
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
102
            $role->decorate($request),
103
            ['pretty' => isset($query['pretty'])]
104
        );
105
    }
106
107
    /**
108
     * Create or replace access role.
109
     */
110
    public function put(ServerRequestInterface $request, Identity $identity, string $role): 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...
111
    {
112
        $body = $request->getParsedBody();
113
        $query = $request->getQueryParams();
114
115
        if ($this->role_factory->has()) {
0 ignored issues
show
Bug introduced by
The call to has() misses a required argument $name.

This check looks for function calls that miss required arguments.

Loading history...
116
            $this->role_factory->update($role, $body);
0 ignored issues
show
Documentation introduced by
$role is of type string, but the function expects a object<Tubee\AccessRole\AccessRoleInterface>.

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...
117
            $role = $this->role_factory->getOne($role);
118
119
            return new UnformattedResponse(
120
                (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
121
                $role->decorate($request),
122
                ['pretty' => isset($query['pretty'])]
123
            );
124
        }
125
126
        $body['name'] = $role;
127
        $id = $this->role_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...
128
        $role = $this->role_factory->getOne($body['name']);
129
130
        return new UnformattedResponse(
131
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
132
            $role->decorate($request),
133
            ['pretty' => isset($query['pretty'])]
134
        );
135
    }
136
137
    /**
138
     * Delete access role.
139
     */
140
    public function delete(ServerRequestInterface $request, Identity $identity, string $role): 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...
141
    {
142
        $this->role_factory->deleteOne($role);
143
144
        return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
145
    }
146
147
    /**
148
     * Patch.
149
     */
150
    public function patch(ServerRequestInterface $request, Identity $identity, string $role): 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...
151
    {
152
        $body = $request->getParsedBody();
153
        $query = $request->getQueryParams();
154
        $role = $this->role_factory->getOne($role);
155
        $doc = ['data' => $role->getData()];
156
157
        $patch = new Patch(json_encode($doc), json_encode($body));
158
        $patched = $patch->apply();
159
        $update = json_decode($patched, true);
160
        $this->role_factory->update($role, $update);
161
162
        return new UnformattedResponse(
163
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
164
            $this->role_factory->getOne($role->getName())->decorate($request),
165
            ['pretty' => isset($query['pretty'])]
166
        );
167
    }
168
}
169