Completed
Push — master ( 7475e6...f11209 )
by Gilmar
22:09
created

ProductCommand::append()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 51
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 51
rs 9.4109
cc 3
eloc 31
nc 1
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-sdk
5
 * Created by Gilmar Pupo <[email protected]>
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 * For more information, see <http://www.g1mr.com/>.
9
 */
10
namespace Gpupo\NetshoesSdk\Console\Command;
11
12
use Gpupo\NetshoesSdk\Console\Application;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Output\OutputInterface;
16
use Gpupo\CommonSdk\Exception\ClientException;
17
class ProductCommand
18
{
19
    public static function append(Application $app)
20
    {
21
        $app->appendCommand('product:view', 'Consulta a situação de um produto')
22
            ->addArgument('productId', InputArgument::REQUIRED, 'Product ID')
23
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
24
                $list = $app->processInputParameters([], $input, $output);
25
26
            $p = $app->factorySdk($list)->factoryManager('product')->findById($input->getArgument('productId'));
27
28
            $app->displayTableResults($output, [[
29
                'Id'           => $p->getProductId(),
30
                'Brand'        => $p->getBrand(),
31
                'Department'   => $p->getDepartment(),
32
                'Product Type' => $p->getProductType(),
33
            ]]);
34
35
            $output->writeln('<fg=yellow>Skus</>');
36
37
            $app->displayTableResults($output, $p->getSkus());
38
39
        });
40
41
        $insertOptions = [
42
            ['key'   => 'file'],
43
        ];
44
45
        $app->appendCommand('product:insert', 'Insere um produto a partir do Json de um arquivo')
46
            ->setDefinition($app->factoryDefinition($insertOptions))
47
            ->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $insertOptions) {
48
                $list = $app->processInputParameters($insertOptions, $input, $output);
49
50
                $data = json_decode(file_get_contents($list['file']), true);
51
                $sdk = $app->factorySdk($list);
52
                $product = $sdk->createProduct($data);
53
54
                try {
55
                    $operation = $sdk->factoryManager('product')->save($product);
56
57
                    if (202 === $operation->getHttpStatusCode()) {
58
                        $output->writeln('<info>Successo!</info>');
59
                    }
60
61
                } catch (\Exception $e) {
62
                    $output->writeln('<error>Erro na criação</error>');
63
                    $output->writeln('Message: <comment>'.$e->getMessage().'</comment>');
64
                    $output->writeln('Code: <comment>'.$e->getCode().'</comment>');
65
                }
66
        });
67
68
        return $app;
69
    }
70
}
71