Completed
Branch master (3adcdb)
by Raffael
08:09 queued 04:17
created

User::parseParams()   F

Complexity

Conditions 12
Paths 2048

Size

Total Lines 49
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 2.5363
c 0
b 0
f 0
cc 12
eloc 25
nc 2048
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Binary;
17
use MongoDB\BSON\ObjectId;
18
use Psr\Log\LoggerInterface;
19
20
class User
21
{
22
    /**
23
     * Getopt.
24
     *
25
     * @var GetOpt
26
     */
27
    protected $getopt;
28
29
    /**
30
     * Logger.
31
     *
32
     * @var LoggerInterface
33
     */
34
    protected $logger;
35
36
    /**
37
     * Server.
38
     *
39
     * @var Server
40
     */
41
    protected $server;
42
43
    /**
44
     * Constructor.
45
     *
46
     * @param Server          $server
47
     * @param LoggerInterface $logger
48
     * @param GetOpt          $getopt
49
     */
50
    public function __construct(Server $server, LoggerInterface $logger, GetOpt $getopt)
51
    {
52
        $this->server = $server;
53
        $this->logger = $logger;
54
        $this->getopt = $getopt;
55
    }
56
57
    /**
58
     * Get description.
59
     *
60
     * @return string
61
     */
62
    public function getDescription(): string
63
    {
64
        return 'Add a new user';
65
    }
66
67
    /**
68
     * Start.
69
     *
70
     * @return bool
71
     */
72 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...
73
    {
74
        $options = $this->parseParams();
75
        $result = $this->server->addUser($this->getopt->getOption('username'), $options);
76
77
        $this->logger->info('new user ['.$result.'] created', [
78
            'category' => get_class($this),
79
        ]);
80
81
        return true;
82
    }
83
84
    /**
85
     * Start.
86
     *
87
     * @return bool
88
     */
89 View Code Duplication
    public function edit(): 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...
90
    {
91
        $id = new ObjectId($this->getopt->getOperand('id'));
92
        $options = $this->parseParams();
93
94
        $user = $this->server->getUserById($id);
95
96
        $this->logger->info('update user ['.$user->getId().']', [
97
            'category' => get_class($this),
98
        ]);
99
100
        if ($this->getopt->getOption('username') !== null) {
101
            $options['username'] = $this->getopt->getOption('username');
102
        }
103
104
        $user->setAttributes($options);
105
106
        return true;
107
    }
108
109
    /**
110
     * Parse params.
111
     *
112
     * @return arrray
113
     */
114
    protected function parseParams(): array
115
    {
116
        $options = [];
117
        if ($this->getopt->getOption('firstname') !== null) {
118
            $options['first_name'] = $this->getopt->getOption('firstname');
119
        }
120
121
        if ($this->getopt->getOption('lastname') !== null) {
122
            $options['last_name'] = $this->getopt->getOption('lastname');
123
        }
124
125
        if ($this->getopt->getOption('mail') !== null) {
126
            $options['mail'] = $this->getopt->getOption('mail');
127
        }
128
129
        if ($this->getopt->getOption('softquota') !== null) {
130
            $options['soft_quota'] = $this->getopt->getOption('softquota');
131
        }
132
133
        if ($this->getopt->getOption('hardquota') !== null) {
134
            $options['hard_quota'] = $this->getopt->getOption('hardquota');
135
        }
136
137
        if ($this->getopt->getOption('namespace') !== null) {
138
            $options['namespace'] = $this->getopt->getOption('namespace');
139
        }
140
141
        if ($this->getopt->getOption('description') !== null) {
142
            $options['description'] = $this->getopt->getOption('description');
143
        }
144
145
        if ($this->getopt->getOption('password') !== null) {
146
            $options['password'] = $this->getopt->getOption('password');
147
        }
148
149
        if ($this->getopt->getOption('locale') !== null) {
150
            $options['locale'] = $this->getopt->getOption('locale');
151
        }
152
153
        if ($this->getopt->getOption('avatar') !== null) {
154
            $options['avatar'] = new Binary(file_get_contents($this->getopt->getOption('avatar')), Binary::TYPE_GENERIC);
155
        }
156
157
        if ($this->getopt->getOption('admin') !== null) {
158
            $options['admin'] = true;
159
        }
160
161
        return $options;
162
    }
163
}
164