OrderCommand::factoryUpdate()   A
last analyzed

Complexity

Conditions 5
Paths 1

Size

Total Lines 38

Duplication

Lines 3
Ratio 7.89 %

Importance

Changes 0
Metric Value
dl 3
loc 38
rs 9.0008
c 0
b 0
f 0
cc 5
nc 1
nop 3
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 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
final class OrderCommand extends AbstractCommand
27
{
28
    protected $list = ['view', 'factoryForStatus', 'translateTo', 'translateFrom', 'queue'];
29
30
    protected $statusList = ['approved', 'invoiced', 'shipped', 'delivered', 'canceled', 'frozen'];
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 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...
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
                ->translatorFetch(0, 50, ['orderStatus' => $type]);
98
                $app->displayOrderList($collection, $output);
99
            });
100
    }
101
102
    protected function queue($app)
103
    {
104
        $this->getApp()->appendCommand('order:queue', 'Mostra os pedidos novos e que ainda aguardam processamento')
105 View Code Duplication
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
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...
106
                $list = $app->processInputParameters([], $input, $output);
107
                $collection = $app->factorySdk($list)->factoryManager('order')->fetchQueue();
108
                $app->displayOrderList($collection, $output);
109
            });
110
    }
111
112 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...
113
    {
114
        $this->getApp()->appendCommand('order:translate:to', 'Exporta o pedido no padrão comum')
115
            ->addArgument('orderId', InputArgument::REQUIRED, 'Order ID')
116
            ->addArgument('filenameOutput', InputArgument::REQUIRED, 'Caminho do arquivo que será gerado')
117
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
118
                $list = $app->processInputParameters([], $input, $output);
119
                $id = $input->getArgument('orderId');
120
                $filenameOutput = $input->getArgument('filenameOutput');
121
                $p = $app->factorySdk($list)->factoryManager('order')->translatorFindById($id);
122
123
                if (empty($p)) {
124
                    return $output->writeln('<error>Pedido não encontrado!</error>');
125
                }
126
127
                return $app->jsonSaveToFile($p->toArray(), $filenameOutput, $output);
128
            });
129
    }
130
131
    public function translateFrom($app)
132
    {
133
        $this->getApp()->appendCommand('order:translate:from', 'Importa o pedido no padrão comum')
134
            ->addArgument('filenameInput', InputArgument::REQUIRED, 'Arquivo json com dados do pedido')
135
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
136
                $list = $app->processInputParameters([], $input, $output);
137
                $foreign = new TranslatorDataCollection($app->jsonLoadFromFile($input->getArgument('filenameInput')));
138
139
                $p = $app->factorySdk($list)->factoryManager('order')->factoryTranslator([
140
                    'foreign' => $foreign,
141
                ]);
142
143
                $app->displayOrder($p->translateFrom(), $output);
144
            });
145
    }
146
}
147