Completed
Push — master ( 700e42...8caa47 )
by Gilmar
39:30 queued 14:31
created

Manager::saveDetail()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 2
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
11
namespace Gpupo\NetshoesSdk\Entity\Product\Sku;
12
13
use Gpupo\CommonSdk\Entity\EntityInterface;
14
use Gpupo\NetshoesSdk\Entity\AbstractManager;
15
16
class Manager extends AbstractManager
17
{
18
    protected $entity = 'SkuCollection';
19
20
    protected $maps = [
21
        'save'              => ['POST', '/products/{productId}/skus'], //Create a new sku for a product
22
        'findSkuById'       => ['GET', '/products/{productId}/skus/{itemId}'], // Get the a sku by product Id and sku Id
23
        'update'            => ['PUT', '/products/{productId}/skus/{itemId}'], //Update a product based on SKU
24
        'findById'          => ['GET', '/products/{itemId}/skus'], //Get the list of product skus
25
        'saveStatus'        => ['PUT', '/skus/{sku}/bus/{buId}/status'], //Enable or disable sku for sale
26
        'savePriceSchedule' => ['POST', '/skus/{sku}/priceSchedules'], //Save a price schedule
27
        'getPriceSchedule'  => ['GET', '/skus/{sku}/priceSchedules'], //Get PriceSchedule
28
        'getPrice'          => ['GET', '/skus/{sku}/prices'], //Get a base price
29
        'savePrice'         => ['PUT', '/skus/{sku}/prices'], //Save a base price
30
        'saveStock'         => ['PUT', '/skus/{sku}/stocks'], //Update stock quantity by sku
31
        'getStock'          => ['GET', '/skus/{sku}/stocks'], //Get Stock
32
        'saveStatus'         => ['GET', '/skus/{sku}/bus/{buId}/status'], //Save Status
33
        'getStatus'         => ['GET', '/skus/{sku}/bus/{buId}/status'], //Get Status
34
    ];
35
36
    public function save(EntityInterface $product, $route = 'save')
37
    {
38
        return $this->execute($this->factoryMap($route), $product->toJson());
39
    }
40
41
    protected function getDetail($skuId, $type)
42
    {
43
        $response = $this->perform($this->factoryMap('get'.$type, ['sku' => $skuId]));
44
        $className = 'Gpupo\NetshoesSdk\Entity\Product\Sku\\'.$type;
45
        $data = $this->processResponse($response);
46
47
        $o = new $className($data->toArray());
48
49
        $this->getLogger()->addInfo('Detail', [
50
            'sku'       => $skuId,
51
            'typ'       => $type,
52
            'response'  => $data,
53
            'className' => $className,
54
            'object'    => $o,
55
        ]);
56
57
        return $o;
58
    }
59
60
    public function saveDetail(EntityInterface $sku, $type)
61
    {
62
        return $this->execute($this->factoryMap('save'.$type, ['sku' => $sku->getId()]), $sku->toJson($type));
63
    }
64
65
    public function getDetailsById($skuId)
66
    {
67
        $o = new Item([
68
            'sku'   => $skuId,
69
        ]);
70
71
        $o->setPrice($this->getDetail($skuId, 'Price'));
0 ignored issues
show
Documentation Bug introduced by
The method setPrice does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? 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...
72
        $o->setPriceSchedule($this->getDetail($skuId, 'PriceSchedule'));
0 ignored issues
show
Documentation Bug introduced by
The method setPriceSchedule does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? 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...
73
        $o->setStock($this->getDetail($skuId, 'Stock'));
0 ignored issues
show
Documentation Bug introduced by
The method setStock does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? 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...
74
        $o->setStatus($this->getDetail($skuId, 'Status'));
0 ignored issues
show
Documentation Bug introduced by
The method setStatus does not exist on object<Gpupo\NetshoesSdk\Entity\Product\Sku\Item>? 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...
75
76
        return $o;
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function update(EntityInterface $entity, EntityInterface $existent = null)
83
    {
84
        parent::update($entity, $existent);
85
86
        $m = $this->factoryMap('update', [
87
            'productId' => $entity->getId(),
88
            'itemId'    => $entity->getId(),
89
        ]);
90
91
        return $this->execute($m, $entity->toJson());
92
    }
93
}
94