SkuCommand   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 28.57 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 4
dl 18
loc 63
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A view() 15 15 2
A details() 0 16 2
A update() 3 25 3

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
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the information of copyright and license you should read the file
7
 * LICENSE which is distributed with this source code.
8
 * Para a informação dos direitos autorais e de licença você deve ler o arquivo
9
 * LICENSE que é distribuído com este código-fonte.
10
 * Para obtener la información de los derechos de autor y la licencia debe leer
11
 * el archivo LICENSE que se distribuye con el código fuente.
12
 * For more information, see <https://www.gpupo.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Console\Command;
16
17
use Symfony\Component\Console\Input\InputArgument;
18
use Symfony\Component\Console\Input\InputInterface;
19
use Symfony\Component\Console\Output\OutputInterface;
20
21
/**
22
 * @codeCoverageIgnore
23
 */
24
final class SkuCommand extends AbstractCommand
25
{
26
    protected $list = ['view', 'details', 'update'];
27
28 View Code Duplication
    public function view($app)
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...
29
    {
30
        $this->getApp()->appendCommand('product:sku:view', 'Mostra os SKUs de um Produto')
31
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
32
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
33
                $list = $app->processInputParameters([], $input, $output);
34
                $id = $input->getArgument('productId');
35
                $output->writeln('Exibindo informações do SKU #<info>'.$id.'</info>');
36
                $p = $app->factorySdk($list)->factoryManager('sku')->findById($id);
37
                if (empty($p)) {
38
                    return $output->writeln('<error>Sku não encontrado!</error>');
39
                }
40
                $app->displayTableResults($output, [$p]);
41
            });
42
    }
43
44
    public function details($app)
45
    {
46
        $this->getApp()->appendCommand('product:sku:details', 'Mostra preço, estoque e situação de um SKU')
47
            ->addArgument('skuId', InputArgument::REQUIRED, 'Sku ID')
48
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
49
                $list = $app->processInputParameters([], $input, $output);
50
                $sku = $app->factorySdk($list)->factoryManager('sku')
51
                    ->findById($input->getArgument('skuId'));
52
                $output->writeln('Stock: <info>'.$sku->getStock()->getAvailable().'</info>');
53
                $output->writeln('Status: <info>'.$sku->getStatus()->getActive().'</info>');
54
                $output->writeln('Price: R$<info>'.$sku->getPrice()->getPrice().'</info>');
55
                if ($sku->getPriceSchedule()) {
56
                    $app->displayTableResults($output, [$sku->getPriceSchedule()->toArray()]);
57
                }
58
            });
59
    }
60
61
    public function update($app)
62
    {
63
        $opts = [
64
            ['key' => 'file'],
65
        ];
66
67
        $this->getApp()->appendCommand('product:sku:update', 'Atualiza um SKU', $opts)
68
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
69
                $list = $app->processInputParameters($opts, $input, $output);
70 View Code Duplication
                if (!file_exists($list['file'])) {
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...
71
                    throw new \InvalidArgumentException('O arquivo ['.$list['file'].'] não existe!');
72
                }
73
                $data = $app->jsonLoadFromFile($list['file']);
74
                $sdk = $app->factorySdk($list);
75
                $sku = $sdk->createSku($data);
76
                $manager = $sdk->factoryManager('sku');
77
                $previous = $manager->findById($sku->getId());
78
                try {
79
                    $operation = $manager->update($sku, $previous);
80
                    $app->displayTableResults($output, [$operation]);
81
                } catch (\Exception $e) {
82
                    $app->showException($e, $output);
83
                }
84
            });
85
    }
86
}
87