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 Symfony\Component\Console\Input\InputArgument; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @codeCoverageIgnore |
23
|
|
|
*/ |
24
|
|
|
final class SkuCommand extends AbstractCommand |
25
|
|
|
{ |
26
|
|
|
protected $list = ['view', 'details', 'update']; |
27
|
|
|
|
28
|
|
View Code Duplication |
public function view($app) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->getApp()->appendCommand('product:sku:view', 'Mostra os SKUs de um Produto') |
31
|
|
|
->addArgument('productId', InputArgument::REQUIRED, 'Product ID') |
32
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) { |
33
|
|
|
$list = $app->processInputParameters([], $input, $output); |
34
|
|
|
$id = $input->getArgument('productId'); |
35
|
|
|
$output->writeln('Exibindo informações do SKU #<info>'.$id.'</info>'); |
36
|
|
|
$p = $app->factorySdk($list)->factoryManager('sku')->findById($id); |
37
|
|
|
if (empty($p)) { |
38
|
|
|
return $output->writeln('<error>Sku não encontrado!</error>'); |
39
|
|
|
} |
40
|
|
|
$app->displayTableResults($output, [$p]); |
41
|
|
|
}); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function details($app) |
45
|
|
|
{ |
46
|
|
|
$this->getApp()->appendCommand('product:sku:details', 'Mostra preço, estoque e situação de um SKU') |
47
|
|
|
->addArgument('skuId', InputArgument::REQUIRED, 'Sku ID') |
48
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) { |
49
|
|
|
$list = $app->processInputParameters([], $input, $output); |
50
|
|
|
$sku = $app->factorySdk($list)->factoryManager('sku') |
51
|
|
|
->findById($input->getArgument('skuId')); |
52
|
|
|
$output->writeln('Stock: <info>'.$sku->getStock()->getAvailable().'</info>'); |
53
|
|
|
$output->writeln('Status: <info>'.$sku->getStatus()->getActive().'</info>'); |
54
|
|
|
$output->writeln('Price: R$<info>'.$sku->getPrice()->getPrice().'</info>'); |
55
|
|
|
if ($sku->getPriceSchedule()) { |
56
|
|
|
$app->displayTableResults($output, [$sku->getPriceSchedule()->toArray()]); |
57
|
|
|
} |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function update($app) |
62
|
|
|
{ |
63
|
|
|
$opts = [ |
64
|
|
|
['key' => 'file'], |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->getApp()->appendCommand('product:sku:update', 'Atualiza um SKU', $opts) |
68
|
|
|
->setCode(function (InputInterface $input, OutputInterface $output) use ($app, $opts) { |
69
|
|
|
$list = $app->processInputParameters($opts, $input, $output); |
70
|
|
View Code Duplication |
if (!file_exists($list['file'])) { |
|
|
|
|
71
|
|
|
throw new \InvalidArgumentException('O arquivo ['.$list['file'].'] não existe!'); |
72
|
|
|
} |
73
|
|
|
$data = $app->jsonLoadFromFile($list['file']); |
74
|
|
|
$sdk = $app->factorySdk($list); |
75
|
|
|
$sku = $sdk->createSku($data); |
76
|
|
|
$manager = $sdk->factoryManager('sku'); |
77
|
|
|
$previous = $manager->findById($sku->getId()); |
78
|
|
|
try { |
79
|
|
|
$operation = $manager->update($sku, $previous); |
80
|
|
|
$app->displayTableResults($output, [$operation]); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
$app->showException($e, $output); |
83
|
|
|
} |
84
|
|
|
}); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.