Capsule   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 14
dl 0
loc 55
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getTable() 0 3 1
A getDatabase() 0 3 1
A __construct() 0 2 1
A getSchema() 0 8 2
A execute() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\Migrations;
6
7
use Cycle\Database\DatabaseInterface;
8
use Cycle\Database\Schema\AbstractTable;
9
use Cycle\Database\TableInterface;
10
use Cycle\Migrations\Exception\CapsuleException;
11
12
/**
13
 * Isolates set of table specific operations and schemas into one place. Kinda repository.
14
 */
15
final class Capsule implements CapsuleInterface
16
{
17
    private array $schemas = [];
18
19 432
    public function __construct(private DatabaseInterface $database)
20
    {
21
    }
22
23
    /**
24
     * {@inheritdoc}
25
     */
26 312
    public function getDatabase(): DatabaseInterface
27
    {
28 312
        return $this->database;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 8
    public function getTable(string $table): TableInterface
35
    {
36 8
        return $this->database->table($table);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 408
    public function getSchema(string $table): AbstractTable
43
    {
44 408
        if (!isset($this->schemas[$table])) {
45
            //We have to declare existed to prevent dropping existed schema
46 408
            $this->schemas[$table] = $this->database->table($table)->getSchema();
0 ignored issues
show
Bug introduced by
The method getSchema() does not exist on Cycle\Database\TableInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Spiral\Database\TableInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
            $this->schemas[$table] = $this->database->table($table)->/** @scrutinizer ignore-call */ getSchema();
Loading history...
47
        }
48
49 408
        return $this->schemas[$table];
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     *
55
     * @throws \Throwable
56
     */
57 408
    public function execute(array $operations): void
58
    {
59 408
        foreach ($operations as $operation) {
60 408
            if (!$operation instanceof OperationInterface) {
61 8
                throw new CapsuleException(
62 8
                    sprintf(
63
                        'Migration operation expected to be an instance of `OperationInterface`, `%s` given',
64
                        $operation::class
65
                    )
66
                );
67
            }
68
69 400
            $operation->execute($this);
70
        }
71
    }
72
}
73