Passed
Push — master ( 10a192...087136 )
by Aleksei
07:38 queued 05:42
created

Capsule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license MIT
7
 * @author  Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Cycle\Migrations;
13
14
use Cycle\Database\Database;
15
use Cycle\Database\DatabaseInterface;
16
use Cycle\Database\DatabaseManager;
17
use Cycle\Database\Schema\AbstractTable;
18
use Cycle\Database\TableInterface;
19
use Cycle\Migrations\Exception\CapsuleException;
20
21
/**
22
 * Isolates set of table specific operations and schemas into one place. Kinda repository.
23
 */
24
final class Capsule implements CapsuleInterface
25
{
26
    /** @var DatabaseManager */
27
    private $database = null;
28
29
    /** @var array */
30
    private $schemas = [];
31
32
    /**
33
     * @param Database $database
34
     */
35
    public function __construct(Database $database)
36
    {
37
        $this->database = $database;
0 ignored issues
show
Documentation Bug introduced by
It seems like $database of type Cycle\Database\Database is incompatible with the declared type Cycle\Database\DatabaseManager of property $database.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getDatabase(): DatabaseInterface
44
    {
45
        return $this->database;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->database returns the type Cycle\Database\DatabaseManager which is incompatible with the type-hinted return Cycle\Database\DatabaseInterface.
Loading history...
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getTable(string $table): TableInterface
52
    {
53
        return $this->database->table($table);
0 ignored issues
show
Bug introduced by
The method table() does not exist on Cycle\Database\DatabaseManager. ( Ignorable by Annotation )

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

53
        return $this->database->/** @scrutinizer ignore-call */ table($table);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getSchema(string $table): AbstractTable
60
    {
61
        if (!isset($this->schemas[$table])) {
62
            //We have to declare existed to prevent dropping existed schema
63
            $this->schemas[$table] = $this->database->table($table)->getSchema();
64
        }
65
66
        return $this->schemas[$table];
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     *
72
     * @throws \Throwable
73
     */
74
    public function execute(array $operations): void
75
    {
76
        foreach ($operations as $operation) {
77
            if (!$operation instanceof OperationInterface) {
78
                throw new CapsuleException(
79
                    sprintf(
80
                        'Migration operation expected to be an instance of `OperationInterface`, `%s` given',
81
                        get_class($operation)
82
                    )
83
                );
84
            }
85
86
            $operation->execute($this);
87
        }
88
    }
89
}
90