Completed
Branch dev (d5d70c)
by Raffael
11:00
created

Resource   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 123
Duplicated Lines 15.45 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 5
lcom 1
dl 19
loc 123
rs 10
c 0
b 0
f 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
B getAclRoles() 19 40 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2018 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\Server;
15
use Balloon\Server\AttributeDecorator;
16
use Balloon\Server\User;
17
use Micro\Http\Response;
18
use MongoDB\BSON\Regex;
19
20
class Resource
21
{
22
    /**
23
     * Server.
24
     *
25
     * @var Server
26
     */
27
    protected $server;
28
29
    /**
30
     * User.
31
     *
32
     * @var User
33
     */
34
    protected $user;
35
36
    /**
37
     * Attribute decorator.
38
     *
39
     * @var AttributeDecorator
40
     */
41
    protected $decorator;
42
43
    /**
44
     * Initialize.
45
     *
46
     * @param Server             $server
47
     * @param AttributeDecorator $decorator
48
     */
49
    public function __construct(Server $server, AttributeDecorator $decorator)
50
    {
51
        $this->server = $server;
52
        $this->user = $server->getIdentity();
53
        $this->decorator = $decorator;
54
    }
55
56
    /**
57
     * @api {get} /resource/acl-roles?q=:query Query available acl roles
58
     * @apiVersion 2.0.0
59
     * @apiName getAclRoles
60
     * @apiGroup Resource
61
     * @apiPermission none
62
     * @apiDescription Query available acl roles (user and groups)
63
     *
64
     * @apiExample Example usage:
65
     * curl -XGET "https://SERVER/api/v2/user/acl-roles?q=peter"
66
     *
67
     * @apiParam (GET Parameter) {string} [q] Search query (user/group)
68
     * @apiParam (GET Parameter) {boolean} [single] Search request for a single user (Don't have to be in namespace)
69
     * @apiParam (GET Parameter) {array} [attributes] Specify user/group attributes
70
     * @apiSuccess {number} status Status Code
71
     * @apiSuccess {object[]} data All roles found with query search string
72
     * @apiSuccess {string} data.type ACL role type (user|group)
73
     * @apiSuccess {object} data.role Role attributes (user/group)
74
     * @apiSuccessExample {json} Success-Response:
75
     * HTTP/1.1 200 OK
76
     * {
77
     *     "status": 200,
78
     *     "data": [
79
     *          {
80
     *              "type": "user",
81
     *              "role": {,
82
     *                  "id": "5a2e6e43db830e003c3eb3b2",
83
     *                  "username": "peter.example"
84
     *              }
85
     *          },
86
     *          {
87
     *              "type": "group",
88
     *              "role": {
89
     *                  "id": "5a2e6e43db830e003c3eb3ca",
90
     *                  "name": "testgroup"
91
     *              }
92
     *          }
93
     *      ]
94
     * }
95
     *
96
     * @param string $q
97
     * @param bool   $single
98
     * @param array  $attributes
99
     *
100
     * @return Response
101
     */
102
    public function getAclRoles(string $q, bool $single = false, array $attributes = []): Response
103
    {
104 View Code Duplication
        if (true === $single) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
105
            $regex = new Regex('^'.preg_quote($q).'$', 'i');
106
            $users_filter = [
107
                'username' => $regex,
108
            ];
109
            $groups_filter = [
110
                'name' => $regex,
111
            ];
112
        } else {
113
            $regex = new Regex('^'.preg_quote($q), 'i');
114
            $users_filter = [
115
                'username' => $regex,
116
                'namespace' => $this->user->getNamespace(),
117
            ];
118
            $groups_filter = [
119
                'name' => $regex,
120
                'namespace' => $this->user->getNamespace(),
121
            ];
122
        }
123
124
        $body = [];
125
126
        foreach ($this->server->getGroups($groups_filter) as $role) {
127
            $body[] = [
128
                'type' => 'group',
129
                'role' => $this->decorator->decorate($role, $attributes),
130
            ];
131
        }
132
133
        foreach ($this->server->getUsers($users_filter) as $role) {
134
            $body[] = [
135
                'type' => 'user',
136
                'role' => $this->decorator->decorate($role, $attributes),
137
            ];
138
        }
139
140
        return (new Response())->setCode(200)->setBody($body);
141
    }
142
}
143