Completed
Push — master ( abe227...316baf )
by Raffael
13:55 queued 08:01
created

ObjectRelations   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 146
ccs 0
cts 80
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getAll() 0 20 2
A getOne() 0 15 2
A post() 0 14 1
A delete() 0 7 1
A patch() 0 20 1
A watchAll() 0 13 1
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\DataObjectRelation\Factory as DataObjectRelationFactory;
22
use Tubee\ResourceNamespace\Factory as ResourceNamespaceFactory;
23
use Tubee\Rest\Helper;
24
use Zend\Diactoros\Response;
25
26
class ObjectRelations
27
{
28
    /**
29
     * namespace factory.
30
     *
31
     * @var ResourceNamespaceFactory
32
     */
33
    protected $namespace_factory;
34
35
    /**
36
     * Dataobject relation factory.
37
     *
38
     * @var DataObjectRelationFactory
39
     */
40
    protected $relation_factory;
41
42
    /**
43
     * Acl.
44
     *
45
     * @var Acl
46
     */
47
    protected $acl;
48
49
    /**
50
     * Init.
51
     */
52
    public function __construct(ResourceNamespaceFactory $namespace_factory, DataObjectRelationFactory $relation_factory, Acl $acl)
53
    {
54
        $this->namespace_factory = $namespace_factory;
55
        $this->relation_factory = $relation_factory;
56
        $this->acl = $acl;
57
    }
58
59
    /**
60
     * Entrypoint.
61
     */
62
    public function getAll(ServerRequestInterface $request, Identity $identity, string $namespace, ?string $collection = null, ?string $object = null): ResponseInterface
63
    {
64
        $query = array_merge([
65
            'offset' => 0,
66
            'limit' => 20,
67
        ], $request->getQueryParams());
68
69
        if ($object !== null) {
70
            $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
71
            $object = $collection->getObject(['name' => $object]);
72
            $relatives = $object->getRelations($query['query'], false, (int) $query['offset'], (int) $query['limit'], $query['sort']);
73
74
            return Helper::getAll($request, $identity, $this->acl, $relatives);
75
        }
76
77
        $namespace = $this->namespace_factory->getOne($namespace);
78
        $result = $this->relation_factory->getAll($namespace, $query['query'], (int) $query['offset'], (int) $query['limit'], $query['sort']);
79
80
        return Helper::getAll($request, $identity, $this->acl, $result);
81
    }
82
83
    /**
84
     * Entrypoint.
85
     */
86
    public function getOne(ServerRequestInterface $request, Identity $identity, string $namespace, ?string $collection = null, ?string $object = null, ?string $relation = null): ResponseInterface
87
    {
88
        if ($object != null) {
89
            $collection = $this->namespace_factory->getOne($namespace)->getCollection($collection);
90
            $object = $collection->getObject(['name' => $object], false);
91
            $relative = $object->getRelation($relation);
92
93
            return Helper::getOne($request, $identity, $relative);
94
        }
95
96
        $namespace = $this->namespace_factory->getOne($namespace);
97
        $relative = $this->relation_factory->getOne($namespace, $relation);
98
99
        return Helper::getOne($request, $identity, $relative);
100
    }
101
102
    /**
103
     * Create object.
104
     */
105
    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...
106
    {
107
        $body = $request->getParsedBody();
108
        $query = $request->getQueryParams();
109
110
        $namespace = $this->namespace_factory->getOne($namespace);
111
        $this->relation_factory->add($namespace, $body);
112
113
        return new UnformattedResponse(
114
            (new Response())->withStatus(StatusCodeInterface::STATUS_CREATED),
115
            $this->relation_factory->getOne($namespace, $body['name'])->decorate($request),
116
            ['pretty' => isset($query['pretty'])]
117
        );
118
    }
119
120
    /**
121
     * Delete.
122
     */
123
    public function delete(ServerRequestInterface $request, Identity $identity, string $namespace, string $relation): 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...
124
    {
125
        $collection = $this->namespace_factory->getOne($namespace);
0 ignored issues
show
Unused Code introduced by
$collection 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...
126
        $this->relation_factory->deleteOne($namespace, $relation);
0 ignored issues
show
Documentation introduced by
$namespace is of type string, but the function expects a object<Tubee\DataObjectR...bjectRelationInterface>.

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...
Documentation introduced by
$relation is of type string, but the function expects a boolean.

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...
127
128
        return(new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT);
129
    }
130
131
    /**
132
     * Patch.
133
     */
134
    public function patch(ServerRequestInterface $request, Identity $identity, string $namespace, string $relation): 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...
135
    {
136
        $body = $request->getParsedBody();
137
        $query = $request->getQueryParams();
138
        $namespace = $this->namespace_factory->getOne($namespace);
139
        $relation = $this->relation_factory->getOne($namespace, $relation);
140
        $doc = ['data' => $relation->getData()];
141
142
        $patch = new Patch(json_encode($doc), json_encode($body));
143
        $patched = $patch->apply();
144
        $update = json_decode($patched, true);
145
146
        $this->relation_factory->update($relation, $update);
147
148
        return new UnformattedResponse(
149
            (new Response())->withStatus(StatusCodeInterface::STATUS_OK),
150
            $this->relation_factory->getOne($namespace, $relation->getName())->decorate($request),
151
            ['pretty' => isset($query['pretty'])]
152
        );
153
    }
154
155
    /**
156
     * Watch.
157
     */
158
    public function watchAll(ServerRequestInterface $request, Identity $identity, string $namespace): ResponseInterface
159
    {
160
        $query = array_merge([
161
            'offset' => null,
162
            'limit' => null,
163
            'existing' => true,
164
        ], $request->getQueryParams());
165
166
        $object = $this->namespace_factory->getOne($namespace);
0 ignored issues
show
Unused Code introduced by
$object 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...
167
        $cursor = $this->relation_factory->watch($namespace, $query['query'], $query['offset'], $query['limit'], $query['sort']);
0 ignored issues
show
Documentation introduced by
$namespace is of type string, but the function expects a object<Tubee\ResourceNam...urceNamespaceInterface>.

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...
Bug introduced by
It seems like $query['offset'] can also be of type null; however, Tubee\DataObjectRelation\Factory::watch() does only seem to accept boolean, 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...
168
169
        return Helper::watchAll($request, $identity, $this->acl, $cursor);
170
    }
171
}
172