Completed
Push — master ( f4b9fa...7f1616 )
by Gilmar
21:37
created

ProductCommand::view()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 31
rs 8.8571
cc 1
eloc 20
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'];
28
29
    public function insert($app)
30
    {
31
        $opts = [
32
            ['key' => 'file'],
33
        ];
34
35
        $this->getApp()->appendCommand('product:insert', 'Insere um produto a partir do Json de um arquivo')
36
            ->setDefinition($this->getApp()->factoryDefinition($opts))
37
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
38
                $list = $app->processInputParameters($opts, $input, $output);
39
40
                $data = json_decode(file_get_contents($list['file']), true);
41
                $sdk = $app->factorySdk($list);
42
                $product = $sdk->createProduct($data);
43
44
                try {
45
                    $operation = $sdk->factoryManager('product')->save($product);
46
47
                    if (202 === $operation->getHttpStatusCode()) {
48
                        $output->writeln('<info>Successo!</info>');
49
                    }
50
                } catch (\Exception $e) {
51
                    $output->writeln('<error>Erro na criação</error>');
52
                    $output->writeln('Message: <comment>'.$e->getMessage().'</comment>');
53
                    $output->writeln('Error Code: <comment>'.$e->getCode().'</comment>');
54
                }
55
            });
56
    }
57
58
    public function view($app)
59
    {
60
        $this->getApp()->appendCommand('product:view', 'Consulta a situação de um produto')
61
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
62
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
63
                $list = $app->processInputParameters([], $input, $output);
64
                $id = $input->getArgument('productId');
65
                $p = $app->factorySdk($list)->factoryManager('product')->findById($id);
66
67
                $app->displayTableResults($output, [[
68
                    'Id'           => $p->getProductId(),
69
                    'Brand'        => $p->getBrand(),
70
                    'Department'   => $p->getDepartment(),
71
                    'Product Type' => $p->getProductType(),
72
                ]]);
73
74
                $output->writeln('<fg=yellow>Skus</>');
75
76
                $app->displayTableResults($output, $p->getSkus());
77
78
                $output->writeln('<fg=yellow>Detalhes</>');
79
80
                $command = $app->find('product:sku:details');
81
                $t = new ArrayInput([
82
                    'command' => 'product:sku:details',
83
                    'skuId'   => $id,
84
85
                ]);
86
                $command->run($t, $output);
87
            });
88
    }
89
90
    public function update($app)
91
    {
92
        $opts = [
93
            ['key' => 'file-previous'],
94
            ['key' => 'file-current'],
95
        ];
96
97
        $this->getApp()->appendCommand('product:update', 'Atualiza um SKU')
98
            ->setDefinition($this->getApp()->factoryDefinition($opts))
99
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) {
100
                $list = $app->processInputParameters($opts, $input, $output);
101
102
                $data = [];
103
104
                foreach (['previous', 'current'] as $i) {
105
                    if (!file_exists($list['file-'.$i])) {
106
                        throw new \InvalidArgumentException('O arquivo ['.$list['file-'.$i].'] não existe!');
107
                    }
108
109
                    $data[$i] = json_decode(file_get_contents($list['file-'.$i]), true);
110
                }
111
112
                $sdk = $app->factorySdk($list);
113
                $current = $sdk->createProduct($data['current']);
114
                $previous = $sdk->createProduct($data['previous']);
115
                $manager = $sdk->factoryManager('product');
116
117
                try {
118
                    $operation = $manager->update($current, $previous);
119
                    $app->displayTableResults($output, [$operation]);
120
                } catch (\Exception $e) {
121
                    $output->writeln('<error>Erro na criação</error>');
122
                    $output->writeln('Message: <comment>'.$e->getMessage().'</comment>');
123
                    $output->writeln('Error Code: <comment>'.$e->getCode().'</comment>');
124
                }
125
            });
126
    }
127
}
128