Completed
Push — master ( b7e091...1ca564 )
by Gilmar
23:34
created

ProductCommand::list()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 8
nc 1
nop 1
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 <http://www.g1mr.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Console\Command;
16
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
/**
23
 * @codeCoverageIgnore
24
 */
25
class ProductCommand extends AbstractCommand
26
{
27
    protected $list = ['view', 'insert', 'update', 'list'];
28
29 View Code Duplication
    public function list($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...
30
    {
31
        $this->getApp()->appendCommand('product:list', 'List')
32
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
33
                $list = $app->processInputParameters([], $input, $output);
34
                $collection = $app->factorySdk($list)->factoryManager('product')->fetch(0, 50);
35
                foreach ($collection as $p) {
36
                    $app->displayProduct($p, $output);
37
                    $output->writeln("\n\n--------------------------------------\n\n");
38
                }
39
            });
40
    }
41
42
    public function insert($app)
43
    {
44
        $opts = [
45
            ['key' => 'file'],
46
        ];
47
48
        $this->getApp()->appendCommand('product:insert', 'Insere um produto a partir do Json de um arquivo', $opts)
49
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
50
                $list = $app->processInputParameters($opts, $input, $output);
51
52
                $data = json_decode(file_get_contents($list['file']), true);
53
                $sdk = $app->factorySdk($list);
54
                $product = $sdk->createProduct($data);
55
56
                try {
57
                    $operation = $sdk->factoryManager('product')->save($product);
58
59
                    if (202 === $operation->getHttpStatusCode()) {
60
                        $output->writeln('<info>Successo!</info>');
61
                    }
62
                } catch (\Exception $e) {
63
                    $app->showException($e, $output);
64
                }
65
            });
66
    }
67
68
    public function view($app)
69
    {
70
        $this->getApp()->appendCommand('product:view', 'Consulta a situação de um produto')
71
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
72
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
73
                $list = $app->processInputParameters([], $input, $output);
74
                $id = $input->getArgument('productId');
75
                $p = $app->factorySdk($list)->factoryManager('product')->findById($id);
76
77
                if (empty($p)) {
78
                    return $output->writeln('<error>Produto não encontrado!</error>');
79
                }
80
81
                $app->displayProduct($p, $output);
82
83
                $output->writeln('<fg=yellow>Detalhes</>');
84
                $command = $app->find('product:sku:details');
85
                $t = new ArrayInput([
86
                    'command' => 'product:sku:details',
87
                    'skuId'   => $id,
88
89
                ]);
90
                $command->run($t, $output);
91
            });
92
    }
93
94
    public function update($app)
95
    {
96
        $opts = [
97
            ['key' => 'file-previous'],
98
            ['key' => 'file-current'],
99
        ];
100
101
        $this->getApp()->appendCommand('product:update', 'Atualiza um SKU', $opts)
102
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
103
                $list = $app->processInputParameters($opts, $input, $output);
104
105
                $data = [];
106
107
                foreach (['previous', 'current'] as $i) {
108
                    if (!file_exists($list['file-'.$i])) {
109
                        throw new \InvalidArgumentException('O arquivo ['.$list['file-'.$i].'] não existe!');
110
                    }
111
112
                    $data[$i] = json_decode(file_get_contents($list['file-'.$i]), true);
113
                }
114
115
                $sdk = $app->factorySdk($list);
116
                $current = $sdk->createProduct($data['current']);
117
                $previous = $sdk->createProduct($data['previous']);
118
                $manager = $sdk->factoryManager('product');
119
120
                try {
121
                    $operation = $manager->update($current, $previous);
122
                    $app->displayTableResults($output, [$operation]);
123
                } catch (\Exception $e) {
124
                    $app->showException($e, $output);
125
                }
126
            });
127
    }
128
}
129