ProductCommand::update()   A
last analyzed

Complexity

Conditions 4
Paths 1

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 9.376
c 0
b 0
f 0
cc 4
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 <https://www.gpupo.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', 'fetch', 'translateTo', 'translateUpdate'];
29
30 View Code Duplication
    public function fetch($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
                $command->run($t, $output);
138
            });
139
    }
140
141 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...
142
    {
143
        $this->getApp()->appendCommand('product:status', 'Consulta o status de um produto')
144
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
145
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
146
                $list = $app->processInputParameters([], $input, $output);
147
                $id = $input->getArgument('productId');
148
                $output->writeln('Status do Product #<info>'.$id.'</info>');
149
                $status = $app->factorySdk($list)->factoryManager('product')->fetchStatusById($id);
150
                $app->displayTableResults($output, [$status->toLog()]);
151
            });
152
    }
153
154
    public function update($app)
155
    {
156
        $opts = [
157
            ['key' => 'file-previous'],
158
            ['key' => 'file-current'],
159
        ];
160
161
        $this->getApp()->appendCommand('product:update', 'Atualiza um SKU', $opts)
162
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
163
                $list = $app->processInputParameters($opts, $input, $output);
164
165
                $data = [];
166
167
                foreach (['previous', 'current'] as $i) {
168
                    if (!file_exists($list['file-'.$i])) {
169
                        throw new \InvalidArgumentException('O arquivo ['.$list['file-'.$i].'] não existe!');
170
                    }
171
172
                    $data[$i] = $app->jsonLoadFromFile($list['file-'.$i]);
173
                }
174
175
                $sdk = $app->factorySdk($list);
176
                $current = $sdk->createProduct($data['current']);
177
                $previous = $sdk->createProduct($data['previous']);
178
                $manager = $sdk->factoryManager('product');
179
180
                try {
181
                    $operation = $manager->update($current, $previous);
182
                    $app->displayTableResults($output, [$operation]);
183
                } catch (\Exception $e) {
184
                    $app->showException($e, $output);
185
                }
186
            });
187
    }
188
}
189