Completed
Push — master ( 316f31...7ea7fa )
by Gilmar
24:54
created

Manager::findSkuById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 9
nc 2
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
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
    /**
42
     * @return Gpupo\Common\Entity\CollectionAbstract|null
43
     */
44
    public function findSkuById($itemId)
45
    {
46
        $response = $this->perform($this->factoryMap('findSkuById', [
47
            'productId' => $itemId,
48
            'itemId'    => $itemId,
49
        ]));
50
51
        $data = $this->processResponse($response);
52
53
        if (empty($data)) {
54
            return;
55
        }
56
57
        $sku = new Item($data->toArray());
58
59
        return $this->hydrate($sku);
60
    }
61
62
    protected function getDetail(EntityInterface $sku, $type)
63
    {
64
        $response = $this->perform($this->factoryMap('get'.$type, ['sku' => $sku->getId()]));
65
        $className = 'Gpupo\NetshoesSdk\Entity\Product\Sku\\'.$type;
66
        $data = $this->processResponse($response);
67
68
        $o = new $className($data->toArray());
69
70
        $this->getLogger()->addInfo('Detail', [
71
            'sku'       => $sku->getId(),
72
            'typ'       => $type,
73
            'response'  => $data,
74
            'className' => $className,
75
            'object'    => $o,
76
        ]);
77
78
        return $o;
79
    }
80
81
    public function saveDetail(Item $sku, $type)
82
    {
83
        return $this->execute($this->factoryMap('save'.$type, ['sku' => $sku->getId()]), $sku->toJson($type));
84
    }
85
86
    protected function hydrate(EntityInterface $sku)
87
    {
88
        $sku->setPrice($this->getDetail($sku, 'Price'))
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Gpupo\CommonSdk\Entity\EntityInterface as the method setPrice() does only exist in the following implementations of said interface: Gpupo\NetshoesSdk\Entity\Product\Sku\Price.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
89
            ->setPriceSchedule($this->getDetail($sku, 'PriceSchedule'))
90
            ->setStock($this->getDetail($sku, 'Stock'))
91
            ->setStatus($this->getDetail($sku, 'Status'));
92
93
        return $sku;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function update(EntityInterface $entity, EntityInterface $existent = null)
100
    {
101
        parent::update($entity, $existent);
102
103
        $m = $this->factoryMap('update', [
104
            'productId' => $entity->getId(),
105
            'itemId'    => $entity->getId(),
106
        ]);
107
108
        return $this->execute($m, $entity->toJson());
109
    }
110
}
111