Completed
Push — master ( d72e6b...d34f34 )
by Gilmar
24:51
created

Manager::factoryTranslator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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\CommonSdk\Traits\TranslatorManagerTrait;
19
use Gpupo\NetshoesSdk\Entity\AbstractManager;
20
use Gpupo\NetshoesSdk\Factory;
21
22
final class Manager extends AbstractManager
23
{
24
    use TranslatorManagerTrait;
25
26
    protected $entity = 'Product';
27
28
    protected $strategy = [
29
        'info' => false,
30
    ];
31
32
    /**
33
     * @codeCoverageIgnore
34
     */
35
    protected $maps = [
36
        'save'     => ['POST', '/products'],
37
        'findById' => ['GET', '/products/{itemId}'],
38
        'patch'    => ['PATCH', '/products/{itemId}'],
39
        'update'   => ['PUT', '/products/{itemId}'],
40
        'fetch'    => ['GET', '/products?page={offset}&size={limit}'],
41
    ];
42
43 1
    public function patch(EntityInterface $entity, $compare)
44
    {
45 1
        if (empty($compare)) {
46 1
            return false;
47
        }
48
49 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...
50 1
        $map = $this->factoryMap('patch', ['itemId' => $entity->getId()]);
51 1
        $operation = $this->execute($map, $json);
52
53
        $feedback = [
54 1
            'fields'        => $compare,
55 1
            'response_code' => $operation->getHttpStatusCode(),
56
        ];
57
58 1
        $this->log('info', 'Operação de Atualização de produto (PATCH)', $feedback);
59
60 1
        return $feedback;
61
    }
62
63 1
    private function skuManager()
64
    {
65 1
        return $this->factorySubManager(Factory::getInstance(), 'sku');
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 2
    public function update(EntityInterface $entity, EntityInterface $existent = null)
72
    {
73 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...
74 1
            throw new \InvalidArgumentException('Product precisa conter SKU!');
75
        }
76
77 1
        $response = [];
78
79 1
        if (true === $this->strategy['info']) {
80
            $compare = $this->attributesDiff($entity, $existent, ['department', 'productType']);
81
            $response['patch'] = $this->patch($entity, $compare);
82
        }
83
84 1
        $response['skus'] = [];
85
86 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...
87 1
            $previous = null;
88
89 1
            if ($existent instanceof EntityInterface) {
90 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...
91
            }
92
93 1
            $response['skus'][] = $this->skuManager()->update($sku, $previous);
94
        }
95
96 1
        return $response;
97
    }
98
99 1
    public function factoryTranslator(array $data = [])
100
    {
101 1
        $translator = new Translator($data);
102
103 1
        return $translator;
104
    }
105
106 2
    public function findById($itemId)
107
    {
108 2
        $product = parent::findById($itemId);
109
110 2
        if (empty($product)) {
111 1
            return false;
112
        }
113
114 1
        $sm = $this->skuManager();
115 1
        $product->getSkus()->forAll(function ($key, $element) use ($sm) {
116 1
            $sm->hydrate($element);
117 1
        });
118
119 1
        return $product;
120
    }
121
}
122