Completed
Push — master ( 9a7e01...73ae60 )
by Gilmar
23:32
created

OrderCommand::queue()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

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