Completed
Push — master ( 651b99...f1c789 )
by Gilmar
21:08
created

OrderCommand::schema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
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 Closure;
18
use Gpupo\CommonSchema\TranslatorDataCollection;
19
use Gpupo\NetshoesSdk\Entity\Order\Schema;
20
use Symfony\Component\Console\Input\InputArgument;
21
use Symfony\Component\Console\Input\InputInterface;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * @codeCoverageIgnore
26
 */
27
class OrderCommand extends AbstractCommand
28
{
29
    protected $list = ['view', 'update', 'list', 'translateTo', 'translateFrom'];
30
31 View Code Duplication
    protected function update($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...
32
    {
33
        $this->factoryUpdate($app, 'approved');
34
        $this->factoryUpdate($app, 'invoiced');
35
        $this->factoryUpdate($app, 'shipped');
36
        $this->factoryUpdate($app, 'delivered');
37
    }
38
39 View Code Duplication
    protected 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...
40
    {
41
        $this->factoryList($app, 'approved');
42
        $this->factoryList($app, 'invoiced');
43
        $this->factoryList($app, 'shipped');
44
        $this->factoryList($app, 'delivered');
45
        $this->factoryList($app, 'canceled');
46
    }
47
48
    protected function factoryUpdate($app, $type, Closure $decorator = null)
49
    {
50
        $opts = [
51
            ['key' => 'file'],
52
        ];
53
54
        $this->getApp()->appendCommand('order:update:to:'.$type, 'Move um pedido para a situação ['.$type.']', $opts)
55
            ->addArgument('orderId', InputArgument::REQUIRED, 'Product ID')
56
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts, $type, $decorator) {
57
                $list = $app->processInputParameters($opts, $input, $output);
58
                $id = $input->getArgument('orderId');
59 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...
60
                    throw new \InvalidArgumentException('O arquivo ['.$list['file'].'] não existe!');
61
                }
62
                $data = $app->jsonLoadFromFile($list['file']);
63
                $sdk = $app->factorySdk($list);
64
                $manager = $sdk->factoryManager('order');
65
                $order = $sdk->createOrder($data);
66
                $order->setOrderNumber($id)->setOrderStatus($type);
67
68
                if (!empty($decorator)) {
69
                    $order = $decorator($order);
70
                }
71
72
                try {
73
                    $output->writeln('Iniciando mudança de status do pedido #<info>'
74
                    .$id.'</info> => <comment>'.$type.'</comment>');
75
76
                    $operation = $manager->updateStatus($order);
77
78
                    if (200 === $operation->getHttpStatusCode()) {
79
                        $output->writeln('<info>Successo!</info>');
80
                    }
81
                } catch (\Exception $e) {
82
                    $app->showException($e, $output, 'Erro na mudança de status');
83
                }
84
            });
85
    }
86
87 View Code Duplication
    protected 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...
88
    {
89
        $this->getApp()->appendCommand('order:view', 'Mostra detalhes de um pedido')
90
            ->addArgument('orderId', InputArgument::REQUIRED, 'Product ID')
91
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
92
                $list = $app->processInputParameters([], $input, $output);
93
                $id = $input->getArgument('orderId');
94
                $p = $app->factorySdk($list)->factoryManager('order')->findById($id);
95
                $app->displayOrder($p, $output);
96
            });
97
    }
98
99
    protected function factoryList($app, $type)
100
    {
101
        $this->getApp()->appendCommand('order:list:'.$type, 'Mostra os pedidos mais recentes em status ['.$type.']')
102
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $type) {
103
                $list = $app->processInputParameters([], $input, $output);
104
                $collection = $app->factorySdk($list)->factoryManager('order')
105
                ->fetch(0, 50, ['orderStatus' => $type]);
106
107
                if (0 === $collection->count()) {
108
                    return $output->writeln('<error>Nenhum pedido encontrado!</error>');
109
                }
110
                foreach ($collection as $p) {
111
                    $app->displayOrder($p, $output);
112
                    $output->writeln("\n\n--------------------------------------\n\n");
113
                }
114
            });
115
    }
116
117 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...
118
    {
119
        $this->getApp()->appendCommand('order:translate:to', 'Exporta o pedido no padrão comum')
120
            ->addArgument('orderId', InputArgument::REQUIRED, 'Order ID')
121
            ->addArgument('filenameOutput', InputArgument::REQUIRED, 'Caminho do arquivo que será gerado')
122
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
123
                $list = $app->processInputParameters([], $input, $output);
124
                $id = $input->getArgument('orderId');
125
                $filenameOutput = $input->getArgument('filenameOutput');
126
                $p = $app->factorySdk($list)->factoryManager('order')->translatorFindById($id);
127
128
                if (empty($p)) {
129
                    return $output->writeln('<error>Pedido não encontrado!</error>');
130
                }
131
                $json = json_encode($p->toArray(), JSON_PRETTY_PRINT);
132
                file_put_contents($filenameOutput, $json);
133
134
                return $output->writeln('Arquivo <info>'.$filenameOutput.'</info> gerado.');
135
            });
136
    }
137
138
    public function translateFrom($app)
139
    {
140
        $this->getApp()->appendCommand('order:translate:from', 'Importa o pedido no padrão comum')
141
            ->addArgument('filenameInput', InputArgument::REQUIRED, 'Arquivo json com dados do pedido')
142
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
143
                $list = $app->processInputParameters([], $input, $output);
144
                $foreign = new TranslatorDataCollection($app->jsonLoadFromFile($input->getArgument('filenameInput')));
145
146
                $p = $app->factorySdk($list)->factoryManager('order')->factoryTranslator([
147
                    'foreign' => $foreign,
148
                ]);
149
150
                $app->displayOrder($p->translateFrom(), $output);
151
            });
152
    }
153
}
154