AbstractCommand   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 189
Duplicated Lines 9.52 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 5
dl 18
loc 189
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 23 1
A initialize() 0 5 1
A getAccountId() 9 9 3
A getAccessToken() 9 9 3
A getConfig() 0 7 2
A out() 0 11 2
B outputFormatDefault() 0 19 6
C outputFormatJson() 0 26 8

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
 * Magedownload CLI
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageDownload
8
 * @package   MageDownload
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magedownload-cli
13
 */
14
15
namespace MageDownload\Command;
16
17
use MageDownload\Config;
18
use Symfony\Component\Console\Command\Command;
19
use Symfony\Component\Console\Helper\Table;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Input\InputOption;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Abstract scan command
26
 *
27
 * @category  MageDownload
28
 * @package   MageDownload
29
 * @author    Steve Robbins <[email protected]>
30
 * @copyright 2015 Steve Robbins
31
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
32
 * @link      https://github.com/steverobbins/magedownload-cli
33
 */
34
abstract class AbstractCommand extends Command
35
{
36
    /**
37
     * Input object
38
     *
39
     * @var \Symfony\Component\Console\Input\InputInterface
40
     */
41
    protected $input;
42
43
    /**
44
     * Output object
45
     *
46
     * @var \Symfony\Component\Console\Output\OutputInterface
47
     */
48
    protected $output;
49
50
    /**
51
     * Cached user config
52
     *
53
     * @var Config
54
     */
55
    protected $config;
56
57
    /**
58
     * Configure command
59
     *
60
     * @return void
61
     */
62
    protected function configure()
63
    {
64
        $this
65
            ->addOption(
66
                'id',
67
                null,
68
                InputOption::VALUE_REQUIRED,
69
                'Magento account ID'
70
            )
71
            ->addOption(
72
                'token',
73
                null,
74
                InputOption::VALUE_REQUIRED,
75
                'Magento access token'
76
            )
77
            ->addOption(
78
                'format',
79
                null,
80
                InputOption::VALUE_REQUIRED,
81
                'Specify output format (default, json)',
82
                'default'
83
            );
84
    }
85
86
    /**
87
     * Initialize command
88
     *
89
     * @param InputInterface  $input
90
     * @param OutputInterface $output
91
     *
92
     * @return void
93
     */
94
    protected function initialize(InputInterface $input, OutputInterface $output)
95
    {
96
        $this->input = $input;
97
        $this->output = $output;
98
    }
99
100
    /**
101
     * Get the account id specified, or from the config
102
     *
103
     * @return string|boolean
104
     */
105 View Code Duplication
    public function getAccountId()
106
    {
107
        if ($this->input->getOption('id')) {
108
            return $this->input->getOption('id');
109
        } elseif ($this->getConfig()->getAccountId()) {
110
            return $this->getConfig()->getAccountId();
111
        }
112
        throw new \InvalidArgumentException('You must specify or configure an account id');
113
    }
114
115
    /**
116
     * Get the access token specified, or from the config
117
     *
118
     * @return string|boolean
119
     */
120 View Code Duplication
    public function getAccessToken()
121
    {
122
        if ($this->input->getOption('token')) {
123
            return $this->input->getOption('token');
124
        } elseif ($this->getConfig()->getAccessToken()) {
125
            return $this->getConfig()->getAccessToken();
126
        }
127
        throw new \InvalidArgumentException('You must specify or configure an access token');
128
    }
129
130
    /**
131
     * Get the user's config
132
     *
133
     * @return Config
134
     */
135
    public function getConfig()
136
    {
137
        if ($this->config === null) {
138
            $this->config = new Config;
139
        }
140
        return $this->config;
141
    }
142
143
    /**
144
     * Output information in the correct format
145
     *
146
     * @param array|string $messages
147
     *
148
     * @return void
149
     */
150
    protected function out($messages = array())
151
    {
152
        $format = $this->input->getOption('format');
153
        $method = 'outputFormat' . ucfirst($format);
154
        if (!method_exists($this, $method)) {
155
            throw new \InvalidArgumentException(
156
                'Format "' . $format . '" is not supported'
157
            );
158
        }
159
        $this->$method($messages);
160
    }
161
162
    /**
163
     * Output in default format
164
     *
165
     * @param array|string $messages
166
     *
167
     * @return void
168
     */
169
    protected function outputFormatDefault($messages)
170
    {
171
        if (!is_array($messages)) {
172
            return $this->output->writeln($messages);
173
        }
174
        foreach ($messages as $message) {
175
            switch (isset($message['type']) ? $message['type'] : false) {
176
                case 'table':
177
                    $tableHelper = new Table($this->output);
178
                    $tableHelper
179
                        ->setHeaders($message['data'][0])
180
                        ->setRows($message['data'][1])
181
                        ->render();
182
                    break;
183
                default:
184
                    $this->output->writeln(is_array($message) ? $message['data'] : $message);
185
            }
186
        }
187
    }
188
189
    /**
190
     * Output in json format
191
     *
192
     * @param array|string $messages
193
     *
194
     * @return void
195
     */
196
    protected function outputFormatJson($messages)
197
    {
198
        $json = array();
199
        if (!is_array($messages)) {
200
            $json[] = strip_tags($messages);
201
        } else {
202
            foreach ($messages as $message) {
203
                switch (isset($message['type']) ? $message['type'] : false) {
204
                    case 'table':
205
                        $result = array();
206
                        $headers = $message['data'][0];
207
                        array_map('strtolower', $headers);
208
                        foreach ($message['data'][1] as $row) {
209
                            foreach ($headers as $key => $name) {
210
                                $result[$name] = strip_tags($row[$key]);
211
                            }
212
                            $json[] = $result;
213
                        }
214
                        break;
215
                    default:
216
                        $json[] = strip_tags(is_array($message) ? $message['data'] : $message);
217
                }
218
            }
219
        }
220
        $this->output->write(json_encode($json), false, OutputInterface::OUTPUT_RAW);
221
    }
222
}
223