SqliteModel   A
last analyzed

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  
createSchema() 0 1 ?
A getTable() 0 4 1
A resolveConnection() 0 4 1
A createTable() 0 19 4
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 ($this instanceof FileModel === true) {
58 1
                $this->initializeTable($table);
0 ignored issues
show
Documentation Bug introduced by
The method initializeTable does not exist on object<Recca0120\Repository\SqliteModel>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
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