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 description. |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getDescription(): string |
62
|
|
|
{ |
63
|
|
|
return 'Add a new group'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Start. |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
View Code Duplication |
public function add(): bool |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$member = $this->parseMember(); |
74
|
|
|
$result = $this->server->addGroup($this->getopt->getOption('name'), $member, $this->parseParams()); |
75
|
|
|
|
76
|
|
|
$this->logger->info('new group ['.$result.'] created', [ |
77
|
|
|
'category' => get_class($this), |
78
|
|
|
]); |
79
|
|
|
|
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Start. |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
View Code Duplication |
public function update(): bool |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$id = new ObjectId($this->getopt->getOperand('id')); |
91
|
|
|
|
92
|
|
|
$options = $this->parseParams(); |
93
|
|
|
$options['member'] = $this->parseMemberUpdate(); |
94
|
|
|
|
95
|
|
|
if ($this->getopt->getOption('name') !== null) { |
96
|
|
|
$options['name'] = $this->getopt->getOption('name'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$group = $this->server->getGroupById($id); |
100
|
|
|
|
101
|
|
|
$this->logger->info('update group ['.$group->getId().']', [ |
102
|
|
|
'category' => get_class($this), |
103
|
|
|
]); |
104
|
|
|
|
105
|
|
|
$group->setAttributes($options); |
106
|
|
|
|
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Parse member. |
112
|
|
|
* |
113
|
|
|
* @return arrray |
114
|
|
|
*/ |
115
|
|
|
protected function parseMemberUpdate(): array |
116
|
|
|
{ |
117
|
|
|
$group = $this->server->getGroupByName($this->getopt->getOption('name')); |
118
|
|
|
$member = []; |
119
|
|
|
|
120
|
|
|
$remove = []; |
121
|
|
|
if ($this->getopt->getOption('remove') !== null) { |
122
|
|
|
$remove = explode(',', $this->getopt->getOption('remove')); |
123
|
|
|
$remove = array_map('trim', $remove); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
foreach ($group->getResolvedMembers() as $user) { |
127
|
|
|
if (!in_array($user->getUsername(), $remove)) { |
128
|
|
|
$member[] = $user; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
View Code Duplication |
if ($this->getopt->getOption('append') !== null) { |
|
|
|
|
133
|
|
|
$append = explode(',', $this->getopt->getOption('append')); |
134
|
|
|
$append = array_map('trim', $append); |
135
|
|
|
|
136
|
|
|
foreach ($append as $user) { |
137
|
|
|
$member[] = $this->server->getUserByName($user); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $member; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Parse params. |
146
|
|
|
* |
147
|
|
|
* @return arrray |
148
|
|
|
*/ |
149
|
|
|
protected function parseParams(): array |
150
|
|
|
{ |
151
|
|
|
$options = []; |
152
|
|
|
if ($this->getopt->getOption('namespace') !== null) { |
153
|
|
|
$options['namespace'] = $this->getopt->getOption('namespace'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($this->getopt->getOption('description') !== null) { |
157
|
|
|
$options['description'] = $this->getopt->getOption('description'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $options; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Parse member. |
165
|
|
|
* |
166
|
|
|
* @return arrray |
167
|
|
|
*/ |
168
|
|
|
protected function parseMember(): array |
169
|
|
|
{ |
170
|
|
|
$member = []; |
171
|
|
View Code Duplication |
if ($this->getopt->getOption('member') !== null) { |
|
|
|
|
172
|
|
|
$list = explode(',', $this->getopt->getOption('member')); |
173
|
|
|
$list = array_map('trim', $list); |
174
|
|
|
|
175
|
|
|
foreach ($list as $name) { |
176
|
|
|
$member[] = $this->server->getUserByName($name); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $member; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
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.