Completed
Push — master ( 603a9f...7527cc )
by Changwan
05:44
created

EloquentAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 6
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
1
<?php
2
namespace Wandu\Service\Eloquent;
3
4
use Illuminate\Database\Capsule\Manager;
5
use Illuminate\Database\Schema\Blueprint;
6
use Psr\Container\ContainerInterface;
7
use Wandu\Migrator\Configuration;
8
use Wandu\Migrator\Contracts\Adapter;
9
use Wandu\Migrator\Contracts\Migration;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Wandu\Service\Eloquent\Migration.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
10
11
class EloquentAdapter implements Adapter  
12
{
13
    /** @var \Illuminate\Database\Capsule\Manager */
14
    protected $manager;
15
    
16
    /** @var \Psr\Container\ContainerInterface */
17
    protected $container;
18
    
19
    /** @var \Wandu\Migrator\Configuration */
20
    protected $config;
21
    
22
    public function __construct(Manager $manager, ContainerInterface $container, Configuration $config)
23
    {
24
        $this->manager = $manager;
25
        $this->container = $container;
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function initialize()
33
    {
34
        if (!$this->hasMigrateTable()) {
35
            $this->getConnection()->getSchemaBuilder()->create($this->config->getTable(), function (Blueprint $table) {
36
                $table->string('version', 30);
37
                $table->index('version');
38
            });
39
        }
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAppliedIds(): array
46
    {
47
        if (!$this->hasMigrateTable()) {
48
            return [];
49
        }
50
        return $this->createMigrationQuery()->get()->map(function ($item) {
51
            return $item->version;
52
        })->toArray();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function isApplied($id): bool
59
    {
60
        if (!$this->hasMigrateTable()) {
61
            return false;
62
        }
63
        foreach ($this->getAppliedIds() as $appliedId) {
64
            if ($id == $appliedId) return true;
65
        }
66
        return false;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getMigrationInstance(string $id, string $name): Migration
73
    {
74
        return $this->container->get($name);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function addToMigrationTable($id)
81
    {
82
        $this->createMigrationQuery()->insert([
83
            'version' => $id,
84
        ]);
85
    }
86
87
    public function removeFromMigrationTable($id)
88
    {
89
        $this->createMigrationQuery()->where([
90
            'version' => $id,
91
        ])->delete();
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    protected function hasMigrateTable()
98
    {
99
        try {
100
            $this->createMigrationQuery()->first();
101
        } catch (\PDOException $e) {
102
            return false;
103
        }
104
        return true;
105
    }
106
107
    /**
108
     * @return \Illuminate\Database\Query\Builder
109
     */
110
    protected function createMigrationQuery()
111
    {
112
        return $this->getConnection()->table($this->config->getTable());
113
    }
114
115
    /**
116
     * @return \Illuminate\Database\Connection
117
     */
118
    protected function getConnection()
119
    {
120
        return $this->manager->getConnection($this->config->getConnection());
121
    }
122
}
123