Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

Collections::post()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 0
cts 12
cp 0
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 4
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Api\v2;
13
14
use Balloon\AttributeDecorator\Pager;
15
use Balloon\Filesystem\Exception;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Balloon\App\Api\v2\Exception.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
use Balloon\Filesystem\Node\Collection;
17
use Balloon\Server\AttributeDecorator as RoleAttributeDecorator;
18
use Micro\Http\Response;
19
use function MongoDB\BSON\fromJSON;
20
use function MongoDB\BSON\toPHP;
21
22
class Collections extends Nodes
23
{
24
    /**
25
     * Get children.
26
     *
27
     * @param null|mixed $query
28
     */
29
    public function getChildren(
30
        ?string $id = null,
31
        int $deleted = 0,
32
        $query = null,
33
        array $attributes = [],
34
        ?int $offset = 0,
35
        ?int $limit = 20,
36
        ?bool $recursive = false
37
    ): Response {
38
        $children = [];
0 ignored issues
show
Unused Code introduced by
$children 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...
39
40
        $node = $this->fs->getNode($id, Collection::class, false, true);
41
        if ($node->isRoot()) {
42
            $uri = '/api/v2/collections/children';
43
        } else {
44
            $uri = '/api/v2/collections/'.$node->getId().'/children';
45
        }
46
47
        if ($query === null) {
48
            $query = [];
49
        } elseif (is_string($query)) {
50
            $query = toPHP(fromJSON($query), [
51
                'root' => 'array',
52
                'document' => 'array',
53
                'array' => 'array',
54
            ]);
55
        }
56
57
        $nodes = $this->fs->getNode($id, Collection::class, false, true)->getChildNodes($deleted, $query, $offset, $limit, $recursive);
58
        $pager = new Pager($this->node_decorator, $nodes, $attributes, $offset, $limit, $uri);
59
        $result = $pager->paging();
60
61
        return (new Response())->setCode(200)->setBody($result);
62
    }
63
64
    /**
65
     * Get Share ACL.
66
     */
67
    public function getShare(RoleAttributeDecorator $role_decorator, string $id, array $attributes = []): Response
68
    {
69
        $node = $this->fs->getNode($id, Collection::class);
70
71
        if (!$node->isShared()) {
72
            throw new Exception\Conflict('node is not a share', Exception\Conflict::NOT_SHARED);
73
        }
74
75
        $acl = $node->getAcl();
76
77
        foreach ($acl as &$rule) {
78
            $rule['role'] = $role_decorator->decorate($rule['role'], $attributes);
79
        }
80
81
        return (new Response())->setCode(200)->setBody([
82
            'name' => $node->getShareName(),
83
            'acl' => $acl,
84
        ]);
85
    }
86
87
    /**
88
     * Create share.
89
     */
90
    public function postShare(array $acl, string $name, string $id): Response
91
    {
92
        $node = $this->fs->getNode($id, Collection::class);
93
        $node->share($acl, $name);
94
        $result = $this->node_decorator->decorate($node);
95
96
        return (new Response())->setCode(200)->setBody($result);
97
    }
98
99
    /**
100
     * Delete share.
101
     */
102
    public function deleteShare(string $id): Response
103
    {
104
        $node = $this->fs->getNode($id, Collection::class);
105
        $result = $node->unshare();
0 ignored issues
show
Unused Code introduced by
$result 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...
106
107
        return (new Response())->setCode(204);
108
    }
109
110
    /**
111
     * Create collection.
112
     */
113
    public function post(
114
        string $name,
115
        ?string $id = null,
116
        array $attributes = [],
117
        int $conflict = 0
118
    ): Response {
119
        $attributes = $this->_verifyAttributes($attributes);
120
        $parent = $this->fs->getNode($id, null, false, true);
121
        $result = $parent->addDirectory((string) $name, $attributes, $conflict);
122
        $result = $this->node_decorator->decorate($result);
123
124
        return (new Response())->setCode(201)->setBody($result);
125
    }
126
}
127