Completed
Push — master ( 7c7fe3...6a7799 )
by Gilmar
03:09
created

Manager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 4
dl 0
loc 98
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
B update() 0 31 3
A save() 0 12 2
A getMap() 0 4 1
A updatePrice() 0 6 1
A updateStock() 0 6 1
A updateStatus() 0 6 1
A commit() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of gpupo component
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', '/loads/products'],
24
        'updateStatus' => ['PUT', '/sellerItems/{itemId}/status'], //Ativação/Desativação de produto no Marketplace
25
        'updateStock'  => ['PUT', '/sellerItems/{itemId}/stock'], //Atualização do estoque do item
26
        'updatePrice'  => ['PUT', '/sellerItems/{itemId}/prices'], //Atualização do preço do item
27
        'findById'     => ['GET', '/loads/products/{itemId}'],
28
        'fetch'        => ['GET', '/sellerItems?_offset={offset}&_limit={limit}&status={status}&createdAt={createdAt}'],
29
    ];
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function update(EntityInterface $entity, EntityInterface $existent)
35
    {
36
        parent::update($entity, $existent);
37
38
        $updated = [];
39
40
        foreach ([
41
            'Stock',
42
            'Price',
43
            //'Status'
44
        ] as $key) {
45
            $getter = 'get' . $key;
46
            if ($this->attributesDiff($entity->$getter(), $existent->$getter())) {
47
                $method = 'update' . $key;
48
                $updated[$key] = $this->$method($entity);
49
            }
50
        }
51
52
        $atualizado = ! empty($updated);
53
54
        $context = [
55
            'id'         => $entity->getId(),
56
            'atualizado' => $atualizado,
57
            'atributos'  => $updated,
58
        ];
59
60
        $this->log('info', 'Operação de Atualização de entity '
61
            . $this->entity, $context);
62
63
        return $atualizado;
64
    }
65
66
    public function save(EntityInterface $product, $route = 'save')
67
    {
68
        $existent = $product->getPrevious();
69
70
        $this->log('INFO', 'save', ['route' => $route, 'existent' => $existent]);
71
72
        if ($existent) {
73
            return $this->update($product, $existent);
74
        }
75
76
        return $this->getPool()->add($product);
77
    }
78
79
    protected function getMap($route, Product $product)
80
    {
81
        return $this->factoryMap($route, ['itemId' => $product->getId()]);
82
    }
83
84
    protected function updatePrice(Product $product)
85
    {
86
        $map = $this->getMap('updatePrice', $product);
87
88
        return $this->execute($map, $product->getPrice()->toJson());
0 ignored issues
show
Documentation Bug introduced by
The method getPrice does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Product>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
89
    }
90
91
    protected function updateStock(Product $product)
92
    {
93
        $map = $this->getMap('updateStock', $product);
94
95
        return $this->execute($map, $product->getStock()->toJson());
0 ignored issues
show
Documentation Bug introduced by
The method getStock does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Product>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
96
    }
97
98
    protected function updateStatus(Product $product)
99
    {
100
        $map = $this->getMap('updateStatus', $product);
101
102
        return $this->execute($map, $product->getStock()->toJson());
0 ignored issues
show
Documentation Bug introduced by
The method getStock does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Product>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
103
    }
104
105
    public function commit()
106
    {
107
        if ($this->getPool()->count() > 0) {
108
            return $this->execute($this->factoryMap('save'), $this->getPool()->toJson());
109
        }
110
111
        return false;
112
    }
113
}
114