GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Manager::update()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 15
cts 15
cp 1
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 19
nc 3
nop 2
crap 3
1
<?php
2
3
/*
4
 * This file is part of gpupo/cnova-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://opensource.gpupo.com/>.
13
 */
14
15
namespace Gpupo\CnovaSdk\Entity\Product;
16
17
use Gpupo\CnovaSdk\Entity\ManagerAbstract;
18
use Gpupo\CommonSdk\Entity\EntityInterface;
19
use Gpupo\CommonSdk\Traits\PoolTrait;
20
21
class Manager extends ManagerAbstract
22
{
23
    use PoolTrait;
24
25
    protected $entity = 'Product';
26
27
    protected $maps = [
28
        'save'         => ['POST', '/loads/products'],
29
        'updateStatus' => ['PUT', '/sellerItems/{itemId}/status'], //Ativação/Desativação de produto no Marketplace
30
        'updateStock'  => ['PUT', '/sellerItems/{itemId}/stock'], //Atualização do estoque do item
31
        'updatePrice'  => ['PUT', '/sellerItems/{itemId}/prices'], //Atualização do preço do item
32
        'findById'     => ['GET', '/loads/products/{itemId}'],
33
        'fetch'        => ['GET', '/sellerItems?_offset={offset}&_limit={limit}&status={status}&createdAt={createdAt}'],
34
    ];
35
36
    /**
37
     * {@inheritdoc}
38
     */
39 3
    public function update(EntityInterface $entity, EntityInterface $existent)
40
    {
41 3
        parent::update($entity, $existent);
42
43 3
        $updated = [];
44
45
        foreach ([
46 3
            'Stock',
47
            'Price',
48
            //'Status'
49
        ] as $key) {
50 3
            $getter = 'get'.$key;
51 3
            if ($this->attributesDiff($entity->$getter(), $existent->$getter())) {
52 1
                $method = 'update'.$key;
53 3
                $updated[$key] = $this->$method($entity);
54
            }
55
        }
56
57 3
        $atualizado = !empty($updated);
58
59
        $context = [
60 3
            'id'         => $entity->getId(),
61 3
            'atualizado' => $atualizado,
62 3
            'atributos'  => $updated,
63
        ];
64
65 3
        $this->log('info', 'Operação de Atualização de entity '
66 3
            .$this->entity, $context);
67
68 3
        return $atualizado;
69
    }
70
71 5
    public function save(EntityInterface $product, $route = 'save')
72
    {
73 5
        $existent = $product->getPrevious();
74
75 5
        $this->log('INFO', 'save', ['route' => $route, 'existent' => $existent]);
76
77 5
        if ($existent) {
78 3
            return $this->update($product, $existent);
79
        }
80
81 2
        return $this->getPool()->add($product);
82
    }
83
84 1
    protected function getMap($route, Product $product)
85
    {
86 1
        return $this->factoryMap($route, ['itemId' => $product->getId()]);
87
    }
88
89
    protected function updatePrice(Product $product)
90
    {
91
        $map = $this->getMap('updatePrice', $product);
92
93
        return $this->execute($map, $product->getPrice()->toJson());
94
    }
95
96 1
    protected function updateStock(Product $product)
97
    {
98 1
        $map = $this->getMap('updateStock', $product);
99
100 1
        return $this->execute($map, $product->getStock()->toJson());
101
    }
102
103
    protected function updateStatus(Product $product)
104
    {
105
        $map = $this->getMap('updateStatus', $product);
106
107
        return $this->execute($map, $product->getStock()->toJson());
108
    }
109
110 1
    public function commit()
111
    {
112 1
        if ($this->getPool()->count() > 0) {
113 1
            return $this->execute($this->factoryMap('save'), $this->getPool()->toJson());
114
        }
115
116
        return false;
117
    }
118
}
119