Delete::getCode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Maketok\DataMigration\Action\Type;
4
5
use Maketok\DataMigration\Action\ActionInterface;
6
use Maketok\DataMigration\Action\Exception\WrongContextException;
7
use Maketok\DataMigration\Unit\ImportDbUnitInterface;
8
use Maketok\DataMigration\Unit\UnitBagInterface;
9
use Maketok\DataMigration\Workflow\ResultInterface;
10
11
/**
12
 * Delete rows in table using tmp table as pk key mapper
13
 */
14
class Delete extends AbstractDbAction implements ActionInterface
15
{
16
    /**
17
     * @var UnitBagInterface|ImportDbUnitInterface[]
18
     */
19
    protected $bag;
20
21
    /**
22
     * {@inheritdoc}
23
     * @throws WrongContextException
24
     */
25 3
    public function process(ResultInterface $result)
26
    {
27 3
        $result->setActionStartTime($this->getCode(), new \DateTime());
28 3
        foreach ($this->bag as $unit) {
29 3
            if ($unit->getTmpTable() === null) {
30 1
                throw new WrongContextException(sprintf(
31 1
                    "Action can not be used for current unit %s. Tmp table is missing.",
32 1
                    $unit->getCode()
33 1
                ));
34
            }
35
            $args = [
36 2
                $unit->getTable(),
37 2
                $unit->getTmpTable()
38 2
            ];
39 2
            if ($unit->getPk() !== null) {
40 1
                $args[] = $unit->getPk();
41 1
            }
42 2
            $rowsDeleted = call_user_func_array([$this->resource, 'deleteUsingTempPK'], $args);
43 2
            $result->incrementActionProcessed($this->getCode(), $rowsDeleted);
44 2
        }
45 2
        $result->setActionEndTime($this->getCode(), new \DateTime());
46 2
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 4
    public function getCode()
52
    {
53 4
        return 'delete';
54
    }
55
}
56