Test Failed
Push — master ( 8c814b...380005 )
by Raffael
08:49
created

Groups::postUndelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 1
crap 2
nc 1
nop 1
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\Server;
16
use Balloon\Server\AttributeDecorator;
17
use Balloon\Server\Group;
18
use Balloon\Server\User;
19
use Balloon\Server\User\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...
20
use Micro\Http\Response;
21
use function MongoDB\BSON\fromJSON;
22
use MongoDB\BSON\ObjectId;
23
use function MongoDB\BSON\toPHP;
24
25
class Groups
26
{
27
    /**
28
     * User.
29
     *
30
     * @var User
31
     */
32
    protected $user;
33
34
    /**
35
     * Server.
36
     *
37
     * @var Server
38
     */
39
    protected $server;
40
41
    /**
42
     * Attribute decorator.
43
     *
44
     * @var AttributeDecorator
45
     */
46
    protected $decorator;
47
48
    /**
49
     * Initialize.
50
     *
51
     * @param AttributeDecorator
52
     */
53
    public function __construct(Server $server, AttributeDecorator $decorator)
54
    {
55
        $this->user = $server->getIdentity();
56
        $this->server = $server;
57
        $this->decorator = $decorator;
58
    }
59
60
    /**
61
     * Get group instance.
62
     */
63
    public function _getGroup(string $id, bool $require_admin = false): Group
64
    {
65
        if (true === $require_admin && !$this->user->isAdmin()) {
66
            throw new Exception\NotAdmin('submitted parameters require admin privileges');
67
        }
68
69
        return $this->server->getGroupById(new ObjectId($id));
70
    }
71
72
    /**
73
     * Get group member.
74
     */
75
    public function getMembers(string $id, array $attributes = [], int $offset = 0, int $limit = 20): Response
76
    {
77
        $group = $this->_getGroup($id);
78
        $result = $group->getResolvedMembers($offset, $limit);
79
        $uri = '/api/v2/groups/'.$group->getId().'/members';
80
        $pager = new Pager($this->decorator, $result, $attributes, $offset, $limit, $uri);
81
        $result = $pager->paging();
82
83
        return (new Response())->setCode(200)->setBody($result);
84
    }
85
86
    /**
87
     * Get group attributes.
88
     *
89
     * @param null|mixed $query
90
     */
91
    public function get(?string $id = null, $query = null, array $attributes = [], int $offset = 0, int $limit = 20): Response
92
    {
93
        if ($id === null) {
94
            if ($query === null) {
95
                $query = [];
96
            } elseif (is_string($query)) {
97
                $query = toPHP(fromJSON($query), [
98
                    'root' => 'array',
99
                    'document' => 'array',
100
                    'array' => 'array',
101
                ]);
102
            }
103
104
            $result = $this->server->getGroups($query, $offset, $limit);
105
            $pager = new Pager($this->decorator, $result, $attributes, $offset, $limit, '/api/v2/groups');
106
            $result = $pager->paging();
107
        } else {
108
            $result = $this->decorator->decorate($this->_getGroup($id), $attributes);
109
        }
110
111
        return (new Response())->setCode(200)->setBody($result);
112
    }
113
114
    /**
115
     * Create group.
116
     */
117
    public function post(string $name, array $member = [], ?string $namespace = null): Response
118
    {
119
        if (!$this->user->isAdmin()) {
120
            throw new Exception\NotAdmin('submitted parameters require admin privileges');
121
        }
122
123
        $attributes = compact('namespace');
124
        $attributes = array_filter($attributes, function ($attribute) {return !is_null($attribute); });
125
126
        $id = $this->server->addGroup($name, $member, $attributes);
127
        $result = $this->decorator->decorate($this->server->getGroupById($id));
128
129
        return (new Response())->setBody($result)->setCode(201);
130
    }
131
132
    /**
133
     * Change group attributes.
134
     */
135
    public function patch(string $id, ?string $name = null, ?array $member = null, ?string $namespace = null): Response
136
    {
137
        $attributes = compact('namespace', 'name', 'member');
138
        $attributes = array_filter($attributes, function ($attribute) {return !is_null($attribute); });
139
140
        $group = $this->_getGroup($id, true);
141
        $group->setAttributes($attributes);
142
        $result = $this->decorator->decorate($group);
143
144
        return (new Response())->setCode(200)->setBody($result);
145
    }
146
147
    /**
148
     * Delete group.
149
     */
150
    public function delete(string $id, bool $force = false): Response
151
    {
152
        $group = $this->_getGroup($id, true);
153
        $group->delete($force);
154
155
        return (new Response())->setCode(204);
156
    }
157
158
    /**
159
     * Restore group.
160
     */
161
    public function postUndelete(string $id): Response
162
    {
163
        $this->_getGroup($id, true)->undelete();
164
165
        return (new Response())->setCode(204);
166
    }
167
}
168