Passed
Push — master ( ddb678...e2f2ed )
by Peter
02:37
created

BaseMigration::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Databases\Migrations;
6
7
use AbterPhp\Framework\Filesystem\IFileFinder;
8
use DateTime;
9
use League\Flysystem\FileNotFoundException;
10
use Opulence\Databases\IConnection;
11
use Opulence\Databases\Migrations\Migration;
12
13
class BaseMigration extends Migration
14
{
15
    protected const FILENAME = 'foo-bar';
16
17
    protected const UP   = 'up';
18
    protected const DOWN = 'down';
19
20
    /** @var IFileFinder */
21
    protected $fileFinder;
22
23
    /**
24
     * Init constructor.
25
     *
26
     * @param IConnection $connection
27
     * @param IFileFinder $fileFinder
28
     */
29
    public function __construct(IConnection $connection, IFileFinder $fileFinder)
30
    {
31
        parent::__construct($connection);
32
33
        $this->fileFinder = $fileFinder;
34
    }
35
36
    /**
37
     * @return DateTime
38
     * @throws Exception
39
     */
40
    public static function getCreationDate(): DateTime
41
    {
42
        return new DateTime();
43
    }
44
45
    /**
46
     * Executes the query that rolls back the migration
47
     *
48
     * @throws FileNotFoundException
49
     * @throws Exception
50
     */
51
    public function down(): void
52
    {
53
        $filename = sprintf('%s/%s', static::DOWN, static::FILENAME);
54
55
        $query = $this->execute($filename);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $query is correct as $this->execute($filename) targeting AbterPhp\Framework\Datab...aseMigration::execute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
56
    }
57
58
    /**
59
     * Executes the query that commits the migration
60
     *
61
     * @throws FileNotFoundException
62
     * @throws Exception
63
     */
64
    public function up(): void
65
    {
66
        $filename = sprintf('%s/%s', static::UP, static::FILENAME);
67
68
        $query = $this->execute($filename);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $query is correct as $this->execute($filename) targeting AbterPhp\Framework\Datab...aseMigration::execute() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Unused Code introduced by
The assignment to $query is dead and can be removed.
Loading history...
69
70
    }
71
72
    /**
73
     * @param string $filename
74
     *
75
     * @throws FileNotFoundException
76
     */
77
    protected function execute(string $filename)
78
    {
79
        $content = $this->fileFinder->read($filename);
80
81
        $this->connection->beginTransaction();
82
83
        $statement = $this->connection->prepare($content);
84
        try {
85
            if (!$statement->execute()) {
86
                $this->connection->rollBack();
87
88
                throw new Exception($statement->errorInfo(), $content, get_class($this), $filename);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type null; however, parameter $query of AbterPhp\Framework\Datab...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
                throw new Exception($statement->errorInfo(), /** @scrutinizer ignore-type */ $content, get_class($this), $filename);
Loading history...
89
            }
90
        } catch (\PDOException $e) {
91
            $this->connection->rollBack();
92
93
            throw new Exception($statement->errorInfo(), $content, get_class($this), $filename, '', 0, $e);
94
        }
95
96
        $this->connection->commit();
97
    }
98
}
99