Completed
Push — master ( ebc742...ce1379 )
by Gilmar
21:51
created

OrderCommand::factoryUpdate()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
ccs 0
cts 0
cp 0
rs 8.5806
cc 4
eloc 26
nc 1
nop 3
crap 20
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 Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\OutputInterface;
21
22
class OrderCommand extends AbstractCommand
23
{
24
    protected $list = ['view', 'update'];
25
26
    protected function update($app)
27
    {
28
        $this->factoryUpdate($app, 'approved');
29
        $this->factoryUpdate($app, 'invoiced');
30
        $this->factoryUpdate($app, 'shipped');
31
        $this->factoryUpdate($app, 'delivered');
32
    }
33
34
    /**
35
     * @codeCoverageIgnore
36
     */
37
    protected function factoryUpdate($app, $type, Closure $orderDecorator = null)
38
    {
39
        $insertOptions = [
40
            ['key' => 'file'],
41
        ];
42
43
        $this->getApp()->appendCommand('order:update:to:'.$type, 'Move um pedido para a situação ['.$type.']')
44
        ->setDefinition($this->getApp()->factoryDefinition($insertOptions))
45
        ->addArgument('orderId', InputArgument::REQUIRED, 'Product ID')
46
        ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $insertOptions, $type, $orderDecorator) {
47
            $list = $app->processInputParameters($insertOptions, $input, $output);
48
            $id = $input->getArgument('orderId');
49
            $data = json_decode(file_get_contents($list['file']), true);
50
            $sdk = $app->factorySdk($list);
51
            $manager = $sdk->factoryManager('order');
52
            $order = $sdk->createOrder($data);
53
            $order->setOrderNumber($id)->setOrderStatus($type);
54
55
            if (!empty($orderDecorator)) {
56
                $order = $orderDecorator($order);
57
            }
58
59
            try {
60
                $output->writeln('Iniciando mudança de status do pedido #<info>'
61
                    .$id.'</info> => <comment>'.$type.'</comment>');
62
63
                $operation = $manager->updateStatus($order);
64
65
                if (200 === $operation->getHttpStatusCode()) {
66
                    $output->writeln('<info>Successo!</info>');
67
                }
68
            } catch (\Exception $e) {
69
                $output->writeln('<error>Erro na mudança de status</error>');
70
                $output->writeln('Message: <comment>'.$e->getMessage().'</comment>');
71
                $output->writeln('Error Code: <comment>'.$e->getCode().'</comment>');
72
            }
73
        });
74
    }
75
76
    /**
77
     * @codeCoverageIgnore
78
     */
79
    protected function view($app)
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
88
                $output->writeln('Order #<comment>'.$id.'</comment>');
89
                $app->displayTableResults($output, [$p->toLog()]);
90
                $output->writeln('Shipping - Order #<comment>'.$id.'</comment>');
91
                $app->displayTableResults($output, [$p->getShipping()->toLog()]);
92
93
                $output->writeln('Shipping Items - Order #<comment>'.$id.'</comment>');
94
                $app->displayTableResults($output, $p->getShipping()->getItems()->toLog());
95
            });
96
    }
97
}
98