Completed
Push — master ( d7820a...8cd137 )
by recca
04:27
created

SqliteModel   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 64
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTable() 0 4 1
A resolveConnection() 0 4 1
A createTable() 0 19 4
createSchema() 0 1 ?
1
<?php
2
3
namespace Recca0120\Repository;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Schema\Blueprint;
7
use Recca0120\Repository\SqliteConnectionResolver as ConnectionResolver;
8
9
abstract class SqliteModel extends Model
10
{
11
    /**
12
     * $tableCreated.
13
     *
14
     * @var array
15
     */
16
    protected static $tableCreated = [];
17
18
    /**
19
     * Get the table associated with the model.
20
     *
21
     * @return string
22
     */
23 31
    public function getTable()
24
    {
25 31
        return $this->createTable(parent::getTable());
26
    }
27
28
    /**
29
     * Resolve a connection instance.
30
     *
31
     * @param  string|null  $connection
32
     * @return \Illuminate\Database\Connection
33
     */
34 31
    public static function resolveConnection($connection = null)
35
    {
36 31
        return ConnectionResolver::getInstance()->connection($connection);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     *
42
     * @param string $table
43
     * @return string
44
     */
45 31
    protected function createTable($table)
46
    {
47 31
        if (isset(static::$tableCreated[$table]) === true) {
48 31
            return $table;
49
        }
50
51 2
        $schema = $this->getConnection()->getSchemaBuilder();
52 2
        if ($schema->hasTable($table) === false) {
53 2
            $schema->create($table, function (Blueprint $table) {
54 2
                $this->createSchema($table);
55 2
            });
56 2
            static::$tableCreated[$table] = true;
57 2
            if (method_exists($this, 'handleTableCreated') === true) {
58 1
                $this->handleTableCreated($table);
0 ignored issues
show
Bug introduced by
The method handleTableCreated() does not exist on Recca0120\Repository\SqliteModel. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
            }
60
        }
61
62 2
        return $table;
63
    }
64
65
    /**
66
     * createSchema.
67
     *
68
     * @param  Blueprint $table
69
     * @return void
70
     */
71
    abstract protected function createSchema(Blueprint $table);
72
}
73