Passed
Pull Request — 3.x (#31)
by
unknown
13:18
created

Truncate::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 12
rs 10
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Cycle\Migrations\Operation\Table;
4
5
use Cycle\Migrations\CapsuleInterface;
6
use Cycle\Migrations\Exception\Operation\TableException;
7
use Cycle\Migrations\Operation\AbstractOperation;
8
9
final class Truncate extends AbstractOperation
10
{
11
    public function __construct(string $table, private string $strategy)
12
    {
13
        parent::__construct($table);
14
    }
15
16
    /**
17
     * {@inheritdoc}
18
     */
19
    public function execute(CapsuleInterface $capsule): void
20
    {
21
        $schema = $capsule->getSchema($this->getTable());
22
        $database = $this->database ?? '[default]';
0 ignored issues
show
Bug Best Practice introduced by
The property database does not exist on Cycle\Migrations\Operation\Table\Truncate. Did you maybe forget to declare it?
Loading history...
23
24
        if (!$schema->exists()) {
25
            throw new TableException(
26
                "Unable to truncate table '{$database}'.'{$this->getTable()}', table does not exists"
27
            );
28
        }
29
30
        $capsule->getDatabase()->execute(sprintf('TRUNCATE "%s" %s', $this->getTable(), $this->strategy));
31
    }
32
}
33