Passed
Pull Request — 3.x (#31)
by
unknown
18:33 queued 03:34
created

Truncate   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 22
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A execute() 0 12 2
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