Completed
Pull Request — master (#3)
by Tim
04:34
created

AbstractAction::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Actions\AbstractAction
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Actions;
22
23
use TechDivision\Import\Actions\Processors\ProcessorInterface;
24
25
/**
26
 * An abstract action implementation.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import
32
 * @link      http://www.techdivision.com
33
 */
34
abstract class AbstractAction implements ActionInterface
35
{
36
37
    /**
38
     * The persist processor instance.
39
     *
40
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
41
     */
42
    protected $persistProcessor;
43
44
    /**
45
     * The remove processor instance.
46
     *
47
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
48
     */
49
    protected $removeProcessor;
50
51
    /**
52
     * Set's the persist processor instance.
53
     *
54
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $persistProcessor The persist processor instance to use
55
     *
56
     * @return void
57
     */
58 1
    public function setPersistProcessor(ProcessorInterface $persistProcessor)
59
    {
60 1
        $this->persistProcessor = $persistProcessor;
61 1
    }
62
63
    /**
64
     * Return's the processor instance.
65
     *
66
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The processor instance
67
     */
68 1
    public function getPersistProcessor()
69
    {
70 1
        return $this->persistProcessor;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->persistProcessor; (TechDivision\Import\Acti...sors\ProcessorInterface) is incompatible with the return type declared by the interface TechDivision\Import\Acti...ce::getPersistProcessor of type ITechDivision\Import\Act...sors\ProcessorInterface.

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...
71
    }
72
73
    /**
74
     * Set's the remove processor instance.
75
     *
76
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $removeProcessor The remove processor instance to use
77
     *
78
     * @return void
79
     */
80 1
    public function setRemoveProcessor(ProcessorInterface $removeProcessor)
81
    {
82 1
        $this->removeProcessor = $removeProcessor;
83 1
    }
84
85
    /**
86
     * Return's the processor instance.
87
     *
88
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The processor instance
89
     */
90 1
    public function getRemoveProcessor()
91
    {
92 1
        return $this->removeProcessor;
93
    }
94
95
    /**
96
     * Persist's the passed row.
97
     *
98
     * @param array $row The row to persist
99
     *
100
     * @return void
101
     */
102 1
    public function persist($row)
103
    {
104 1
        $this->getPersistProcessor()->execute($row);
105 1
    }
106
107
    /**
108
     * Remove's the entity with the passed attributes.
109
     *
110
     * @param array $row The attributes of the entity to remove
111
     *
112
     * @return void
113
     */
114 1
    public function remove($row)
115
    {
116 1
        $this->getRemoveProcessor()->execute($row);
117 1
    }
118
}
119