Completed
Push — master ( 76d3ae...6d9057 )
by Gilmar
22:35
created

Manager::update()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.5806
cc 4
eloc 14
nc 4
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of gpupo/netshoes-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 <http://www.g1mr.com/>.
13
 */
14
15
namespace Gpupo\NetshoesSdk\Entity\Product;
16
17
use Gpupo\CommonSdk\Entity\EntityInterface;
18
use Gpupo\NetshoesSdk\Entity\AbstractManager;
19
use Gpupo\NetshoesSdk\Factory;
20
21
class Manager extends AbstractManager
22
{
23
    protected $entity = 'Product';
24
25
    /**
26
     * @codeCoverageIgnore
27
     */
28
    protected $maps = [
29
        'save'     => ['POST', '/products'],
30
        'findById' => ['GET', '/products/{itemId}'],
31
        'patch'    => ['PATCH', '/products/{itemId}'],
32
        'update'   => ['PUT', '/products/{itemId}'],
33
        'fetch'    => ['GET', '/products?page={offset}&size={limit}'],
34
    ];
35
36 1
    public function patch(EntityInterface $entity, $compare)
37
    {
38 1
        if (empty($compare)) {
39
            return false;
40
        }
41
42 1
        $json = json_encode($entity->toPatch($compare));
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 toPatch() does only exist in the following implementations of said interface: Gpupo\NetshoesSdk\Entity\Product\Product.

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...
43 1
        $map = $this->factoryMap('patch', ['itemId' => $entity->getId()]);
44 1
        $operation = $this->execute($map, $json);
45
46
        $feedback = [
47 1
            'fields'        => $compare,
48 1
            'response_code' => $operation->getHttpStatusCode(),
49
        ];
50
51 1
        $this->log('info', 'Operação de Atualização de produto (PATCH)', $feedback);
52
53 1
        return $feedback;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2
    public function update(EntityInterface $entity, EntityInterface $existent = null)
60
    {
61 2
        if (0 === $entity->getSkus()->count()) {
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 getSkus() does only exist in the following implementations of said interface: Gpupo\NetshoesSdk\Entity\Product\Product.

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...
62 1
            throw new \InvalidArgumentException('Product precisa conter SKU!');
63
        }
64
65 1
        $response = [];
66
67 1
        $compare = $this->attributesDiff($entity, $existent, ['department', 'productType']);
68 1
        $response['patch'] = $this->patch($entity, $compare);
69 1
        $response['skus'] = [];
70
71 1
        $skuManager = $this->factorySubManager(Factory::getInstance(), 'sku');
72
73 1
        foreach ($entity->getSkus() as $sku) {
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 getSkus() does only exist in the following implementations of said interface: Gpupo\NetshoesSdk\Entity\Product\Product.

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...
74 1
            $previous = null;
75 1
            if ($existent) {
76 1
                $previous = $existent->getSkus()->findById($sku->getId());
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 getSkus() does only exist in the following implementations of said interface: Gpupo\NetshoesSdk\Entity\Product\Product.

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...
77
            }
78
79 1
            $response['skus'][] = $skuManager->update($sku, $previous);
80
        }
81
82 1
        return $response;
83
    }
84
85
    protected function factoryTranslator(CollectionAbstract $entity)
86
    {
87
        $translator = new Translator();
88
        $translator->setInput($entity);
89
90
        return $translator;
91
    }
92
}
93