Completed
Push — master ( dd25b7...31bd97 )
by Gilmar
28:58 queued 04:06
created

Manager::updateStock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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 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\Entity\Product;
11
12
use Gpupo\CommonSdk\Entity\EntityInterface;
13
use Gpupo\CommonSdk\Traits\PoolTrait;
14
use Gpupo\NetshoesSdk\Entity\ManagerAbstract;
15
16
class Manager extends ManagerAbstract
17
{
18
    use PoolTrait;
19
20
    protected $entity = 'Product';
21
22
    protected $maps = [
23
        'save'     => ['POST', '/products'],
24
        'findById' => ['GET', '/products/{itemId}'],
25
        'patch'    => ['PATCH', '/products/{itemId}'],
26
        'update'   => ['PUT', '/products/{itemId}'],
27
        'fetch'    => ['GET', '/products?page={offset}&size={limit}'],
28
    ];
29
30
    public function save(EntityInterface $product, $route = 'save')
31
    {
32
        $existent = $product->getPrevious();
33
34
        $this->log('INFO', 'save', ['route' => $route, 'existent' => $existent]);
35
36
        if ($existent) {
37
            return $this->update($product, $existent);
38
        }
39
40
        return $this->getPool()->add($product);
41
    }
42
43
    protected function getMap($route, Product $product)
44
    {
45
        return $this->factoryMap($route, ['itemId' => $product->getId()]);
46
    }
47
48
    public function commit()
49
    {
50
        if ($this->getPool()->count() > 0) {
51
            return $this->execute($this->factoryMap('save'), $this->getPool()->toJson());
52
        }
53
54
        return false;
55
    }
56
}
57