Completed
Push — master ( b44bca...6404c7 )
by Gilmar
22:18
created

OrderCommand::factoryUpdate()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 38
Code Lines 25

Duplication

Lines 3
Ratio 7.89 %

Importance

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