Completed
Push — master ( f1c789...f35966 )
by Gilmar
36:36 queued 11:26
created

OrderCommand::factoryForStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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