Passed
Push — master ( 47e248...a43368 )
by Raffael
09:50
created

Group::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 11
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 11
loc 11
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
crap 2
eloc 6
nc 1
nop 0
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
     * Start.
58
     *
59
     * @return bool
60
     */
61 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...
62
    {
63
        $member = $this->parseMember();
64
        $result = $this->server->addGroup($this->getopt->getOption('name'), $member, $this->parseParams());
65
66
        $this->logger->info('new group ['.$result.'] created', [
67
            'category' => get_class($this),
68
       ]);
69
70
        return true;
71
    }
72
73
    /**
74
     * Start.
75
     *
76
     * @return bool
77
     */
78
    public function edit(): bool
79
    {
80
        $id = new ObjectId($this->getopt->getOperand('id'));
81
        $group = $this->server->getGroupById($id);
82
83
        $options = $this->parseParams();
84
        $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...
85
86
        if ($this->getopt->getOption('name') !== null) {
87
            $options['name'] = $this->getopt->getOption('name');
88
        }
89
90
        $group = $this->server->getGroupById($id);
91
92
        $this->logger->info('update group ['.$group->getId().']', [
93
            'category' => get_class($this),
94
        ]);
95
96
        $group->setAttributes($options);
97
98
        return true;
99
    }
100
101
    /**
102
     * Parse member.
103
     *
104
     * @param Group $group
105
     *
106
     * @return arrray
107
     */
108
    protected function parseMemberUpdate(Group $group): array
109
    {
110
        $member = [];
111
112
        $remove = [];
113
        if ($this->getopt->getOption('remove') !== null) {
114
            $remove = explode(',', $this->getopt->getOption('remove'));
115
            $remove = array_map('trim', $remove);
116
        }
117
118
        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...
119
            if (!in_array((string) $user->getId(), $remove)) {
120
                $member[] = $user;
121
            }
122
        }
123
124 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...
125
            $append = explode(',', $this->getopt->getOption('append'));
126
            $append = array_map('trim', $append);
127
128
            foreach ($append as $user) {
129
                $member[] = $this->server->getUserById(new ObjectId($user));
130
            }
131
        }
132
133
        return $member;
134
    }
135
136
    /**
137
     * Parse params.
138
     *
139
     * @return arrray
140
     */
141
    protected function parseParams(): array
142
    {
143
        $options = [];
144
        if ($this->getopt->getOption('namespace') !== null) {
145
            $options['namespace'] = $this->getopt->getOption('namespace');
146
        }
147
148
        return $options;
149
    }
150
151
    /**
152
     * Parse member.
153
     *
154
     * @return arrray
155
     */
156
    protected function parseMember(): array
157
    {
158
        $member = [];
159 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...
160
            $list = explode(',', $this->getopt->getOption('member'));
161
            $list = array_map('trim', $list);
162
163
            foreach ($list as $name) {
164
                $member[] = $this->server->getUserById(new ObjectId($name));
165
            }
166
        }
167
168
        return $member;
169
    }
170
}
171