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

User   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 110
Duplicated Lines 10 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
cbo 4
dl 11
loc 110
ccs 0
cts 51
cp 0
rs 10
c 0
b 0
f 0
lcom 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A add() 11 11 1
A edit() 0 19 2
B parseParams() 0 25 2

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\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
     * Start.
59
     *
60
     * @return bool
61
     */
62 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...
63
    {
64
        $options = $this->parseParams();
65
        $result = $this->server->addUser($this->getopt->getOption('username'), $options);
66
67
        $this->logger->info('new user ['.$result.'] created', [
68
            'category' => get_class($this),
69
        ]);
70
71
        return true;
72
    }
73
74
    /**
75
     * Start.
76
     *
77
     * @return bool
78
     */
79
    public function edit(): bool
80
    {
81
        $id = new ObjectId($this->getopt->getOperand('id'));
82
        $options = $this->parseParams();
83
84
        $user = $this->server->getUserById($id);
85
86
        $this->logger->info('update user ['.$user->getId().']', [
87
            'category' => get_class($this),
88
        ]);
89
90
        if ($this->getopt->getOption('username') !== null) {
91
            $options['username'] = $this->getopt->getOption('username');
92
        }
93
94
        $user->setAttributes($options);
95
96
        return true;
97
    }
98
99
    /**
100
     * Parse params.
101
     *
102
     * @return arrray
103
     */
104
    protected function parseParams(): array
105
    {
106
        $map = [
107
            'mail' => 'mail',
108
            'softquota' => 'soft_quota',
109
            'hardquota' => 'hard_quota',
110
            'namespace' => 'namespace',
111
            'password' => 'password',
112
            'locale' => 'locale',
113
            'avatar' => 'avatar',
114
            'admin' => 'admin',
115
        ];
116
117
        $options = array_intersect_key($this->getopt->getOptions(), $map);
118
        ksort($options);
119
        $map = array_intersect_key($map, $options);
120
        ksort($map);
121
        $options = array_combine(array_values($map), $options);
122
123
        if (isset($options['avatar'])) {
124
            $options['avatar'] = new Binary(file_get_contents($options['avatar']), Binary::TYPE_GENERIC);
125
        }
126
127
        return $options;
128
    }
129
}
130