Completed
Pull Request — master (#12)
by Tim
02:41
created

AbstractAction::persist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
ccs 0
cts 4
cp 0
rs 9.6666
cc 1
eloc 3
nc 1
nop 1
crap 2
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\Utils\EntityStatus;
24
use TechDivision\Import\Actions\Processors\ProcessorInterface;
25
26
/**
27
 * An abstract action implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import
33
 * @link      http://www.techdivision.com
34
 */
35
abstract class AbstractAction implements ActionInterface
36
{
37
38
    /**
39
     * The create processor instance.
40
     *
41
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
42
     */
43
    protected $createProcessor;
44
45
    /**
46
     * The delete processor instance.
47
     *
48
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
49
     */
50
    protected $deleteProcessor;
51
52
    /**
53
     * The update processor instance.
54
     *
55
     * @var \TechDivision\Import\Actions\Processors\ProcessorInterface
56
     */
57
    protected $updateProcessor;
58
59
    /**
60
     * Set's the create processor instance.
61
     *
62
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $createProcessor The create processor instance to use
63
     *
64
     * @return void
65
     */
66 1
    public function setCreateProcessor(ProcessorInterface $createProcessor)
67
    {
68 1
        $this->createProcessor = $createProcessor;
69 1
    }
70
71
    /**
72
     * Return's the create processor instance.
73
     *
74
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The create processor instance
75
     */
76 1
    public function getCreateProcessor()
77
    {
78 1
        return $this->createProcessor;
79
    }
80
81
    /**
82
     * Set's the delete processor instance.
83
     *
84
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $deleteProcessor The delete processor instance to use
85
     *
86
     * @return void
87
     */
88 1
    public function setDeleteProcessor(ProcessorInterface $deleteProcessor)
89
    {
90 1
        $this->deleteProcessor = $deleteProcessor;
91 1
    }
92
93
    /**
94
     * Return's the delete processor instance.
95
     *
96
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The delete processor instance
97
     */
98 1
    public function getDeleteProcessor()
99
    {
100 1
        return $this->deleteProcessor;
101
    }
102
103
    /**
104
     * Set's the update processor instance.
105
     *
106
     * @param \TechDivision\Import\Actions\Processors\ProcessorInterface $updateProcessor The update processor instance to use
107
     *
108
     * @return void
109
     */
110
    public function setUpdateProcessor(ProcessorInterface $updateProcessor)
111
    {
112
        $this->updateProcessor = $updateProcessor;
113
    }
114
115
    /**
116
     * Return's the update processor instance.
117
     *
118
     * @return \TechDivision\Import\Actions\Processors\ProcessorInterface The update processor instance
119
     */
120
    public function getUpdateProcessor()
121
    {
122
        return $this->updateProcessor;
123
    }
124
125
    /**
126
     * Helper method that create/update the passed entity, depending on
127
     * the entity's status.
128
     *
129
     * @param array $row The entity data to create/update
130
     *
131
     * @return void
132
     */
133
    public function persist(array $row)
134
    {
135
136
        // load the method name
137
        $methodName = $row[EntityStatus::MEMBER_NAME];
138
139
        // invoke the method
140
        $this->$methodName($row);
141
    }
142
143
    /**
144
     * Creates's the entity with the passed attributes.
145
     *
146
     * @param array       $row  The attributes of the entity to create
147
     * @param string|null $name The name of the prepared statement that has to be executed
148
     *
149
     * @return void
150
     */
151 1
    public function create($row, $name = null)
152
    {
153 1
        $this->getCreateProcessor()->execute($row, $name);
154 1
    }
155
156
    /**
157
     * Delete's the entity with the passed attributes.
158
     *
159
     * @param array       $row  The attributes of the entity to delete
160
     * @param string|null $name The name of the prepared statement that has to be executed
161
     *
162
     * @return void
163
     */
164 1
    public function delete($row, $name = null)
165
    {
166 1
        $this->getDeleteProcessor()->execute($row, $name);
167 1
    }
168
169
    /**
170
     * Update's the entity with the passed attributes.
171
     *
172
     * @param array       $row  The attributes of the entity to update
173
     * @param string|null $name The name of the prepared statement that has to be executed
174
     *
175
     * @return void
176
     */
177
    public function update($row, $name = null)
178
    {
179
        $this->getUpdateProcessor()->execute($row, $name);
180
    }
181
}
182