Completed
Push — master ( 38800c...cebedb )
by Gilmar
25:00
created

AbstractManager::translatorFetch()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 0
crap 3
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;
16
17
use Gpupo\CommonSchema\TranslatorDataCollection;
18
use Gpupo\CommonSdk\Entity\EntityInterface;
19
use Gpupo\CommonSdk\Entity\ManagerAbstract;
20
use Gpupo\CommonSdk\Entity\ManagerInterface;
21
22
abstract class AbstractManager extends ManagerAbstract implements ManagerInterface
23
{
24 4
    protected function fetchDefaultParameters()
25
    {
26
        return [
27 4
            'buId' => 'ZT',
28
        ];
29
    }
30
31 15
    public function factoryMap($operation, array $parameters = null)
32
    {
33 15
        return parent::factoryMap($operation, array_merge($this->fetchDefaultParameters(), (array) $parameters));
34
    }
35
36
    /**
37
     * @codeCoverageIgnore
38
     *
39
     * @return Gpupo\Common\Entity\CollectionAbstract|null|false
40
     */
41
    protected function fetchPrepare($data)
42
    {
43
        if (empty($data)) {
44
            return false;
45
        }
46
47
        return $this->factoryEntityCollection($data);
48
    }
49
50 4
    protected function factoryEntityCollection($data)
51
    {
52 4
        return $this->factoryNeighborObject($this->getEntityName().'Collection', $data);
53
    }
54
55 1
    public function save(EntityInterface $entity, $route = 'save')
56
    {
57 1
        return $this->execute($this->factoryMap($route), $entity->toJson($route));
58
    }
59
60
    /**
61
     * @return Gpupo\Common\Entity\CollectionAbstract|false
62
     */
63 3
    public function findById($itemId)
64
    {
65 3
        $data = parent::findById($itemId);
66
67 3
        if (empty($data) || $data->get('type') === 'Resource_Not_Found') {
68 1
            return false;
69
        }
70
71 2
        return $this->factoryEntity($data->toArray());
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     *
77
     * @codeCoverageIgnore
78
     */
79
    public function update(EntityInterface $entity, EntityInterface $existent = null)
80
    {
81
        $text = 'Chamada a Atualização de entity '.$this->entity;
82
83
        return $this->log('debug', $text, [
84
            'entity'   => $entity,
85
            'existent' => $existent,
86
        ]);
87
    }
88
89
    protected function factoryTranslator(array $data = [])
90
    {
91
        throw new \Exception('factoryTranslator() deve ser implementado!');
92
    }
93
94
    public function translatorUpdate(TranslatorDataCollection $data, TranslatorDataCollection $existent = null)
95
    {
96
        $entity = $this->factoryTranslator($data)->translateFrom();
97
98
        if (!empty($existent)) {
99
            $previous = $this->factoryTranslator($existent)->translateFrom();
100
        }
101
102
        return $this->update($entity, empty($existent) ? $previous : null);
0 ignored issues
show
Bug introduced by
The variable $previous does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
103
    }
104
105 1
    public function translatorFetch()
106
    {
107 1
        $dataCollection = new TranslatorDataCollection();
108 1
        $collection = $this->fetch();
109
110 1
        if (0 < $collection->count()) {
111 1
            foreach ($collection as $entity) {
0 ignored issues
show
Bug introduced by
The expression $collection of type object<Gpupo\CommonSdk\E...ionAbstract>|null|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
112 1
                $dataCollection->add($this->factoryTranslator(['native' => $entity])->translateTo());
113
            }
114
        }
115
116 1
        return $dataCollection;
117
    }
118
119 1
    public function translateFindById($itemId)
120
    {
121 1
        $collection = $this->findById($itemId);
122
123 1
        if (empty($collection)) {
124
            return false;
125
        }
126
127 1
        return $this->factoryTranslator(['native' => $collection])->translateTo();
128
    }
129
}
130