Passed
Push — master ( 304892...44e430 )
by Raffael
04:59
created

Group::getOperands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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\Cli\Console;
13
14
use Balloon\Server;
15
use GetOpt\GetOpt;
16
use MongoDB\BSON\ObjectId;
17
use Psr\Log\LoggerInterface;
18
19
class Group
20
{
21
    /**
22
     * Getopt.
23
     *
24
     * @var GetOpt
25
     */
26
    protected $getopt;
27
28
    /**
29
     * Logger.
30
     *
31
     * @var LoggerInterface
32
     */
33
    protected $logger;
34
35
    /**
36
     * Server.
37
     *
38
     * @var Server
39
     */
40
    protected $server;
41
42
    /**
43
     * Constructor.
44
     *
45
     * @param Server          $server
46
     * @param LoggerInterface $logger
47
     * @param GetOpt          $getopt
48
     */
49
    public function __construct(Server $server, LoggerInterface $logger, GetOpt $getopt)
50
    {
51
        $this->server = $server;
52
        $this->logger = $logger;
53
        $this->getopt = $getopt;
54
    }
55
56
    /**
57
     * Get help.
58
     */
59
    public function help(): Group
60
    {
61
        echo "add\n";
62
        echo "Add a new group\n\n";
63
64
        echo "edit\n";
65
        echo "Edit a group\n\n";
66
        echo $this->getopt->getHelpText();
67
68
        return $this;
69
    }
70
71
    /*
72
     * Get operands
73
     *
74
     * @return array
75
     */
76
    public static function getOperands(): array
77
    {
78
        return [
79
            \GetOpt\Operand::create('action', \GetOpt\Operand::REQUIRED),
80
            \GetOpt\Operand::create('id', \GetOpt\Operand::OPTIONAL),
81
        ];
82
    }
83
84
    /**
85
     * Get group options.
86
     *
87
     * @return array
88
     */
89
    public static function getOptions(): array
90
    {
91
        return [
92
            \GetOpt\Option::create('g', 'name', GetOpt::REQUIRED_ARGUMENT)
93
                ->setDescription('Specify the groupname [REQUIRED]'),
94
            \GetOpt\Option::create('m', 'member', GetOpt::REQUIRED_ARGUMENT)
95
                ->setDescription('A list of usernames to add to the group (comma separated)'),
96
            \GetOpt\Option::create('n', 'namespace', GetOpt::REQUIRED_ARGUMENT)
97
                ->setDescription('A namespace'),
98
        ];
99
    }
100
101
    /**
102
     * Start.
103
     *
104
     * @return bool
105
     */
106 View Code Duplication
    public function add(): bool
0 ignored issues
show
Duplication introduced by
This method 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...
107
    {
108
        $member = $this->parseMember();
109
        $result = $this->server->addGroup($this->getopt->getOption('name'), $member, $this->parseParams());
110
111
        $this->logger->info('new group ['.$result.'] created', [
112
            'category' => get_class($this),
113
       ]);
114
115
        return true;
116
    }
117
118
    /**
119
     * Start.
120
     *
121
     * @return bool
122
     */
123
    public function edit(): bool
124
    {
125
        $id = new ObjectId($this->getopt->getOperand('id'));
126
        $group = $this->server->getGroupById($id);
127
128
        $options = $this->parseParams();
129
        $options['member'] = $this->parseMemberUpdate($group);
0 ignored issues
show
Documentation introduced by
$group is of type object<Balloon\Server\Group>, but the function expects a object<Balloon\App\Cli\Console\Group>.

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...
130
131
        if ($this->getopt->getOption('name') !== null) {
132
            $options['name'] = $this->getopt->getOption('name');
133
        }
134
135
        $group = $this->server->getGroupById($id);
136
137
        $this->logger->info('update group ['.$group->getId().']', [
138
            'category' => get_class($this),
139
        ]);
140
141
        $group->setAttributes($options);
142
143
        return true;
144
    }
145
146
    /**
147
     * Parse member.
148
     *
149
     * @param Group $group
150
     *
151
     * @return arrray
152
     */
153
    protected function parseMemberUpdate(Group $group): array
154
    {
155
        $member = [];
156
157
        $remove = [];
158
        if ($this->getopt->getOption('remove') !== null) {
159
            $remove = explode(',', $this->getopt->getOption('remove'));
160
            $remove = array_map('trim', $remove);
161
        }
162
163
        foreach ($group->getResolvedMembers() as $user) {
0 ignored issues
show
Bug introduced by
The method getResolvedMembers() does not seem to exist on object<Balloon\App\Cli\Console\Group>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
            if (!in_array((string) $user->getId(), $remove)) {
165
                $member[] = $user;
166
            }
167
        }
168
169 View Code Duplication
        if ($this->getopt->getOption('append') !== null) {
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...
170
            $append = explode(',', $this->getopt->getOption('append'));
171
            $append = array_map('trim', $append);
172
173
            foreach ($append as $user) {
174
                $member[] = $this->server->getUserById(new ObjectId($user));
175
            }
176
        }
177
178
        return $member;
179
    }
180
181
    /**
182
     * Parse params.
183
     *
184
     * @return arrray
185
     */
186
    protected function parseParams(): array
187
    {
188
        $options = [];
189
        if ($this->getopt->getOption('namespace') !== null) {
190
            $options['namespace'] = $this->getopt->getOption('namespace');
191
        }
192
193
        return $options;
194
    }
195
196
    /**
197
     * Parse member.
198
     *
199
     * @return arrray
200
     */
201
    protected function parseMember(): array
202
    {
203
        $member = [];
204 View Code Duplication
        if ($this->getopt->getOption('member') !== null) {
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...
205
            $list = explode(',', $this->getopt->getOption('member'));
206
            $list = array_map('trim', $list);
207
208
            foreach ($list as $name) {
209
                $member[] = $this->server->getUserById(new ObjectId($name));
210
            }
211
        }
212
213
        return $member;
214
    }
215
}
216