Completed
Push — master ( c9dd2f...f3d682 )
by Gilmar
25:00
created

AbstractManager::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
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;
16
17
use Gpupo\CommonSdk\Entity\EntityInterface;
18
use Gpupo\CommonSdk\Entity\ManagerAbstract;
19
use Gpupo\CommonSdk\Entity\ManagerInterface;
20
21
abstract class AbstractManager extends ManagerAbstract implements ManagerInterface
22
{
23 3
    protected function fetchDefaultParameters()
24
    {
25
        return [
26 3
            'buId' => 'ZT',
27
        ];
28
    }
29
30 7
    public function factoryMap($operation, array $parameters = null)
31
    {
32 7
        return parent::factoryMap($operation, array_merge($this->fetchDefaultParameters(), (array) $parameters));
33
    }
34
35
    /**
36
     * @return Gpupo\Common\Entity\CollectionAbstract|false
37
     */
38 2
    protected function fetchPrepare($data)
39
    {
40 2
        if (empty($data)) {
41
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Gpupo\CommonSdk\Entity\M...rAbstract::fetchPrepare of type Gpupo\CommonSdk\Entity\G...CollectionAbstract|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
42
        }
43
44 2
        return $this->factoryEntityCollection($data);
45
    }
46
47 2
    protected function factoryEntityCollection($data)
48
    {
49 2
        return $this->factoryNeighborObject($this->getEntityName().'Collection', $data);
50
    }
51
52 1
    public function save(EntityInterface $entity, $route = 'save')
53
    {
54 1
        return $this->execute($this->factoryMap($route), $entity->toJson($route));
55
    }
56
57
    /**
58
     * @return Gpupo\Common\Entity\CollectionAbstract|false
59
     */
60 3
    public function findById($itemId)
61
    {
62 3
        $data = parent::findById($itemId);
63
64 3
        if (empty($data) || $data->get('type') === 'Resource_Not_Found') {
65 1
            return false;
66
        }
67
68 2
        return $this->factoryEntity($data->toArray());
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     *
74
     * @codeCoverageIgnore
75
     */
76
    public function update(EntityInterface $entity, EntityInterface $existent = null)
77
    {
78
        $text = 'Chamada a Atualização de entity '.$this->entity;
79
80
        return $this->log('debug', $text, [
81
            'entity'   => $entity,
82
            'existent' => $existent,
83
        ]);
84
    }
85
}
86