Completed
Push — master ( 25d97b...a01eb8 )
by Gilmar
24:21
created

ProductCommand::status()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 1
eloc 9
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 Gpupo\CommonSchema\TranslatorDataCollection;
18
use Symfony\Component\Console\Input\ArrayInput;
19
use Symfony\Component\Console\Input\InputArgument;
20
use Symfony\Component\Console\Input\InputInterface;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @codeCoverageIgnore
25
 */
26
final class ProductCommand extends AbstractCommand
27
{
28
    protected $list = ['view', 'status', 'insert', 'update', 'list', 'translateTo', 'translateUpdate'];
29
30 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...
31
    {
32
        $this->getApp()->appendCommand('product:list', 'List')
33
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
34
                $list = $app->processInputParameters([], $input, $output);
35
                $collection = $app->factorySdk($list)->factoryManager('product')->fetch(0, 50);
36
                foreach ($collection as $p) {
37
                    $app->displayProduct($p, $output);
38
                    $output->writeln("\n\n--------------------------------------\n\n");
39
                }
40
            });
41
    }
42
43
    public function insert($app)
44
    {
45
        $opts = [
46
            ['key' => 'file'],
47
        ];
48
49
        $this->getApp()->appendCommand('product:insert', 'Insere um produto a partir do Json de um arquivo', $opts)
50
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
51
                $list = $app->processInputParameters($opts, $input, $output);
52
53
                $data = $app->jsonLoadFromFile($list['file']);
54
                $sdk = $app->factorySdk($list);
55
                $product = $sdk->createProduct($data);
56
57
                try {
58
                    $operation = $sdk->factoryManager('product')->save($product);
59
60
                    if (202 === $operation->getHttpStatusCode()) {
61
                        $output->writeln('<info>Successo!</info>');
62
                    }
63
                } catch (\Exception $e) {
64
                    $app->showException($e, $output);
65
                }
66
            });
67
    }
68
69 View Code Duplication
    public function translateTo($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...
70
    {
71
        $this->getApp()->appendCommand('product:translate:to', 'Exporta o produto no padrão comum')
72
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
73
            ->addArgument('filenameOutput', InputArgument::REQUIRED, 'Caminho do arquivo que será gerado')
74
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
75
                $list = $app->processInputParameters([], $input, $output);
76
                $id = $input->getArgument('productId');
77
                $filenameOutput = $input->getArgument('filenameOutput');
78
                $p = $app->factorySdk($list)->factoryManager('product')->translatorFindById($id);
79
80
                if (empty($p)) {
81
                    return $output->writeln('<error>Produto não encontrado!</error>');
82
                }
83
84
                return $app->jsonSaveToFile($p->toArray(), $filenameOutput, $output);
85
            });
86
    }
87
88
    public function translateUpdate($app)
89
    {
90
        $this->getApp()->appendCommand('product:translate:update', 'Atualiza o produto (schema)')
91
            ->addArgument('filenameInput', InputArgument::REQUIRED, 'Arquivo json com dados do produto')
92
            ->addArgument('filenamePrevious', InputArgument::OPTIONAL, 'Previous file')
93
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
94
                $list = $app->processInputParameters([], $input, $output);
95
                $filenameInput = $input->getArgument('filenameInput');
96
                $current = new TranslatorDataCollection($app->jsonLoadFromFile($filenameInput));
97
                $previous = null;
98
                $filenamePrevious = $input->getArgument('filenamePrevious');
99
100
                if (!empty($filenamePrevious)) {
101
                    $previous = new TranslatorDataCollection($app->jsonLoadFromFile($filenamePrevious));
102
                }
103
104
                $sdk = $app->factorySdk($list);
105
                $manager = $sdk->factoryManager('product');
106
107
                try {
108
                    $operation = $manager->translatorUpdate($current, $previous);
109
                    $app->displayTableResults($output, [$operation]);
110
                } catch (\Exception $e) {
111
                    $app->showException($e, $output);
112
                }
113
            });
114
    }
115
116
    public function view($app)
117
    {
118
        $this->getApp()->appendCommand('product:view', 'Consulta a situação de um produto')
119
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
120
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
121
                $list = $app->processInputParameters([], $input, $output);
122
                $id = $input->getArgument('productId');
123
                $p = $app->factorySdk($list)->factoryManager('product')->findById($id);
124
125
                if (empty($p)) {
126
                    return $output->writeln('<error>Produto não encontrado!</error>');
127
                }
128
129
                $app->displayProduct($p, $output);
130
131
                $output->writeln('<fg=yellow>Detalhes</>');
132
                $command = $app->find('product:sku:details');
133
                $t = new ArrayInput([
134
                    'command' => 'product:sku:details',
135
                    'skuId'   => $id,
136
137
                ]);
138
                $command->run($t, $output);
139
            });
140
    }
141
142 View Code Duplication
    public function status($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...
143
    {
144
        $this->getApp()->appendCommand('product:status', 'Consulta o status de um produto')
145
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
146
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
147
                $list = $app->processInputParameters([], $input, $output);
148
                $id = $input->getArgument('productId');
149
                $output->writeln('Status do Product #<info>'.$id.'</info>');
150
                $status = $app->factorySdk($list)->factoryManager('product')->fetchStatusById($id);
151
                $app->displayTableResults($output, [$status->toLog()]);
152
            });
153
    }
154
155
    public function update($app)
156
    {
157
        $opts = [
158
            ['key' => 'file-previous'],
159
            ['key' => 'file-current'],
160
        ];
161
162
        $this->getApp()->appendCommand('product:update', 'Atualiza um SKU', $opts)
163
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
164
                $list = $app->processInputParameters($opts, $input, $output);
165
166
                $data = [];
167
168
                foreach (['previous', 'current'] as $i) {
169
                    if (!file_exists($list['file-'.$i])) {
170
                        throw new \InvalidArgumentException('O arquivo ['.$list['file-'.$i].'] não existe!');
171
                    }
172
173
                    $data[$i] = $app->jsonLoadFromFile($list['file-'.$i]);
174
                }
175
176
                $sdk = $app->factorySdk($list);
177
                $current = $sdk->createProduct($data['current']);
178
                $previous = $sdk->createProduct($data['previous']);
179
                $manager = $sdk->factoryManager('product');
180
181
                try {
182
                    $operation = $manager->update($current, $previous);
183
                    $app->displayTableResults($output, [$operation]);
184
                } catch (\Exception $e) {
185
                    $app->showException($e, $output);
186
                }
187
            });
188
    }
189
}
190