Completed
Push — master ( e8ae8c...76d3ae )
by Gilmar
24:46
created

AbstractManager::fetchPrepare()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
ccs 0
cts 0
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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\Common\Entity\CollectionAbstract;
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(CollectionAbstract $entity)
0 ignored issues
show
Unused Code introduced by
The parameter $entity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
90
    {
91
        throw new \Exception('factoryTranslator() deve ser implementado!');
92
    }
93
94
    public function translatorUpdate(CollectionAbstract $entity, CollectionAbstract $existent = null)
95
    {
96
        $product = $this->factoryTranslator($entity)->translateFrom();
97
98
        if (!empty($existent)) {
99
            $previous = $this->factoryTranslator($existent)->translateFrom();
100
        }
101
102
        return $this->update($product, 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