Failed Conditions
Push — refactor/improve-static-analys... ( d71351...8065ea )
by Bas
05:41
created

DatabaseMigrationRepository::getSchemaManager()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Migrations;
6
7
use ArangoClient\Exceptions\ArangoException;
8
use ArangoClient\Schema\SchemaManager;
9
use Illuminate\Database\ConnectionResolverInterface as IlluminateResolver;
10
use Illuminate\Database\Migrations\DatabaseMigrationRepository as IlluminateDatabaseMigrationRepository;
11
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
12
use LaravelFreelancerNL\Aranguent\Connection;
13
use LaravelFreelancerNL\Aranguent\Exceptions\AranguentException;
14
use LaravelFreelancerNL\Aranguent\Query\Builder;
15
16
class DatabaseMigrationRepository extends IlluminateDatabaseMigrationRepository
17
{
18
    /**
19
     * The name of the migration collection.
20
     *
21
     * @var string
22
     */
23
    protected $table;
24
25
    /**
26 210
     * Create a new database migration repository instance.
27
     */
28 210
    public function __construct(IlluminateResolver $resolver, string $table)
29
    {
30 210
        parent::__construct($resolver, $table);
31 210
    }
32
33
    protected function getSchemaManager(): SchemaManager
34
    {
35
        $connection = $this->getConnection();
36
        assert($connection instanceof Connection);
37
38 164
        $arangoClient = $connection->getArangoClient();
39
40 164
        if ($arangoClient === null) {
41
            throw new AranguentException('No arangodb client set.');
42
        }
43
44
        return $arangoClient->schema();
45
46
    }
47
48 157
    /**
49
     * Create the migration repository data store.
50 157
     *
51 157
     * @return void
52 157
     */
53 157
    public function createRepository()
54
    {
55 157
        $schemaManager = $this->getSchemaManager();
56
57
        $schemaManager->createCollection($this->table);
58
    }
59
60
    /**
61
     * Get the list of migrations.
62
     *
63
     * @param  int  $steps
64
     * @return array<mixed>
65
     */
66
    public function getMigrations($steps)
67
    {
68
        //TODO: the only difference with the parent function is that type of the batch value:
69
        // 1 instead of '1'. This should probably be changed in the Laravel framework as it
70 1
        // seems unnecessary to use a numeric string here.
71
72 1
        $query = $this->table()
73 1
            ->where('batch', '>=', 1)
74 1
            ->orderBy('batch', 'desc')
75 1
            ->orderBy('migration', 'desc')
76 1
            ->take($steps);
77 1
78
        return $query->get()->all();
79 1
    }
80
81
82 138
    /**
83
     * Determine if the migration repository exists.
84 138
     *
85
     * @return bool
86 138
     * @throws ArangoException
87 138
     * @throws AranguentException
88 138
     */
89 138
    public function repositoryExists()
90 138
    {
91
        $schemaManager = $this->getSchemaManager();
92 138
93
        return $schemaManager->hasCollection($this->table);
94
    }
95
96
    /**
97
     * Get a query builder for the migration collection.
98
     *
99
     * @return IlluminateQueryBuilder
100 1
     */
101
    protected function collection()
102 1
    {
103 1
        return $this->table();
104 1
    }
105
}
106