Completed
Push — master ( 38800c...cebedb )
by Gilmar
25:00
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\CommonSchema\TranslatorDataCollection;
18
use Gpupo\CommonSdk\Entity\EntityInterface;
19
use Gpupo\NetshoesSdk\Entity\AbstractManager;
20
use Gpupo\NetshoesSdk\Factory;
21
22
class Manager extends AbstractManager
23
{
24
    protected $entity = 'Product';
25
26
    /**
27
     * @codeCoverageIgnore
28
     */
29
    protected $maps = [
30
        'save'     => ['POST', '/products'],
31
        'findById' => ['GET', '/products/{itemId}'],
32
        'patch'    => ['PATCH', '/products/{itemId}'],
33
        'update'   => ['PUT', '/products/{itemId}'],
34
        'fetch'    => ['GET', '/products?page={offset}&size={limit}'],
35
    ];
36
37 1
    public function patch(EntityInterface $entity, $compare)
38
    {
39 1
        if (empty($compare)) {
40
            return false;
41
        }
42
43 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...
44 1
        $map = $this->factoryMap('patch', ['itemId' => $entity->getId()]);
45 1
        $operation = $this->execute($map, $json);
46
47
        $feedback = [
48 1
            'fields'        => $compare,
49 1
            'response_code' => $operation->getHttpStatusCode(),
50
        ];
51
52 1
        $this->log('info', 'Operação de Atualização de produto (PATCH)', $feedback);
53
54 1
        return $feedback;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function update(EntityInterface $entity, EntityInterface $existent = null)
61
    {
62 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...
63 1
            throw new \InvalidArgumentException('Product precisa conter SKU!');
64
        }
65
66 1
        $response = [];
67
68 1
        $compare = $this->attributesDiff($entity, $existent, ['department', 'productType']);
69 1
        $response['patch'] = $this->patch($entity, $compare);
70 1
        $response['skus'] = [];
71
72 1
        $skuManager = $this->factorySubManager(Factory::getInstance(), 'sku');
73
74 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...
75 1
            $previous = null;
76 1
            if ($existent) {
77 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...
78
            }
79
80 1
            $response['skus'][] = $skuManager->update($sku, $previous);
81
        }
82
83 1
        return $response;
84
    }
85
86
    protected function factoryTranslator(array $data = [])
87
    {
88
        $translator = new Translator($data);
89
90
        return $translator;
91
    }
92
}
93