Passed
Push — master ( 0ed784...b28e07 )
by CodexShaper
14:16
created

Table::paginate()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 1
b 0
f 0
nc 4
nop 4
dl 0
loc 13
rs 9.6111
1
<?php
2
3
namespace CodexShaper\DBM\Database\Schema;
4
5
use CodexShaper\DBM\Database\Schema\Column;
6
use CodexShaper\DBM\Database\Schema\ForeignKey;
7
use CodexShaper\DBM\Database\Schema\Index;
8
use CodexShaper\DBM\Database\Schema\UpdateManager;
9
use CodexShaper\DBM\Database\Types\Type;
10
use CodexShaper\DBM\Facades\Driver;
11
use CodexShaper\DBM\Facades\MongoDB;
12
use Doctrine\DBAL\Schema\SchemaException;
13
use Doctrine\DBAL\Schema\Table as DoctrineTable;
14
use Illuminate\Database\Eloquent\Collection;
15
use Illuminate\Pagination\LengthAwarePaginator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Pagination\LengthAwarePaginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Illuminate\Pagination\Paginator;
0 ignored issues
show
Bug introduced by
The type Illuminate\Pagination\Paginator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
18
class Table
19
{
20
    public static function all()
21
    {
22
        // MongoDb
23
        if (Driver::isMongoDB()) {
0 ignored issues
show
Bug introduced by
The method isMongoDB() does not exist on CodexShaper\DBM\Facades\Driver. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

23
        if (Driver::/** @scrutinizer ignore-call */ isMongoDB()) {
Loading history...
24
            return MongoDB::getCollectionNames();
25
        }
26
        return SchemaManager::getInstance()->listTableNames();
27
    }
28
29
    public static function getTable($tableName)
30
    {
31
        if (Driver::isMongoDB()) {
32
            return MongoDB::getCollection($tableName);
33
        }
34
35
        Type::registerCustomTypes();
36
37
        $table          = SchemaManager::getInstance()->listTableDetails($tableName);
38
        $primaryKeyName = $table->getPrimaryKey();
39
        $options        = $table->getOptions();
40
41
        return [
42
            'name'           => $table->getName(),
43
            'oldName'        => $table->getName(),
44
            'columns'        => static::getColumns($table),
45
            'indexes'        => static::getIndexes($table),
46
            'foreignKeys'    => static::getForeignKeys($table),
47
            'primaryKeyName' => $primaryKeyName,
48
            'options'        => $options,
49
        ];
50
    }
51
52
    public static function getColumnsName($tableName)
53
    {
54
        $columns = static::getTable($tableName)['columns'];
55
56
        $columnsName = [];
57
58
        foreach ($columns as $column) {
59
            $columnsName[] = $column->name;
60
        }
61
62
        return $columnsName;
63
    }
64
65
    public static function create($table = [])
66
    {
67
        if (!is_array($table)) {
68
            $table = json_decode($table, true);
69
        }
70
71
        // MongoDB
72
        if (Driver::isMongoDB()) {
73
            return MongoDB::createCollection($table['name']);
74
        }
75
76
        $newTable = self::prepareTable($table);
77
78
        $schema = SchemaManager::getInstance();
79
        return $schema->createTable($newTable);
80
    }
81
82
    public static function update($table = [])
83
    {
84
        if (!is_array($table)) {
85
            $table = json_decode($table, true);
86
        }
87
88
        if (Driver::isMongoDB()) {
89
            return MongoDB::updateCollection($table);
90
        }
91
92
        (new UpdateManager())->update($table);
93
    }
94
95
    public static function drop($tableName)
96
    {
97
        if (Driver::isMongoDB()) {
98
            return MongoDB::dropCollection($tableName);
0 ignored issues
show
Bug introduced by
Are you sure the usage of CodexShaper\DBM\Facades\...pCollection($tableName) targeting CodexShaper\DBM\Database...ngoDB::dropCollection() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

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

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

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

Loading history...
99
        }
100
101
        return SchemaManager::getInstance()->dropTable($tableName);
102
    }
103
104
    public static function prepareTable($table = [])
105
    {
106
107
        if (!is_array($table)) {
108
            $table = json_decode($table, true);
109
        }
110
111
        Type::registerCustomTypes();
112
113
        // DoctrineType::addType('varchar', 'App\CodexShaper\Database\Types\Common\VarCharType');
114
        // SchemaManager::getInstance()->getDatabasePlatform()->registerDoctrineTypeMapping('db_varchar', 'varchar');
115
116
        $conn = 'database.connections.' . config('database.default');
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

116
        $conn = 'database.connections.' . /** @scrutinizer ignore-call */ config('database.default');
Loading history...
117
118
        $table['options']['collate'] = config($conn . '.collation', 'utf8mb4_unicode_ci');
119
        $table['options']['charset'] = config($conn . '.charset', 'utf8mb4');
120
121
        $tableName   = $table['name'];
122
        $columns     = $table['columns'];
123
        $indexes     = $table['indexes'];
124
        $foreignKeys = $table['foreignKeys'];
125
126
        // Make Doctrain  columns
127
        $DoctrineColumns = [];
128
        foreach ($columns as $column) {
129
            $DoctrineColumn                              = Column::create($column);
130
            $DoctrineColumns[$DoctrineColumn->getName()] = $DoctrineColumn;
131
        }
132
133
        // Make Doctrain indexes
134
        $DoctrineIndexes = [];
135
        foreach ($indexes as $index) {
136
            $DoctrineIndex                              = Index::create($index);
137
            $DoctrineIndexes[$DoctrineIndex->getName()] = $DoctrineIndex;
138
        }
139
140
        // Make Doctrain Foreign Keys
141
        $DoctrineForeignKeys = [];
142
        foreach ($foreignKeys as $foreignKey) {
143
144
            $DoctrineForeignKey = ForeignKey::create($foreignKey);
145
146
            $DoctrineForeignKeys[$DoctrineForeignKey->getName()] = $DoctrineForeignKey;
147
        }
148
149
        $options = $table['options'];
150
151
        return new DoctrineTable($tableName, $DoctrineColumns, $DoctrineIndexes, $DoctrineForeignKeys, false, $options);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type integer expected by parameter $idGeneratorType of Doctrine\DBAL\Schema\Table::__construct(). ( Ignorable by Annotation )

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

151
        return new DoctrineTable($tableName, $DoctrineColumns, $DoctrineIndexes, $DoctrineForeignKeys, /** @scrutinizer ignore-type */ false, $options);
Loading history...
152
    }
153
154
    public static function getColumns(DoctrineTable $table)
155
    {
156
        $columns = [];
157
158
        $order = 1;
159
        foreach ($table->getColumns() as $column) {
160
            $columns[] = (object) array_merge(
161
                Column::toArray($column),
162
                array('order' => $order)
163
            );
164
            $order++;
165
        }
166
167
        return $columns;
168
    }
169
170
    public static function getIndexes(DoctrineTable $table)
171
    {
172
        $indexes = [];
173
174
        foreach ($table->getIndexes() as $index) {
175
            $indexes[] = (object) array_merge(
176
                Index::toArray($index),
177
                array('table' => $table->getName())
178
            );
179
        }
180
181
        return $indexes;
182
    }
183
184
    public static function getForeignKeys(DoctrineTable $table)
185
    {
186
        $foreignKeys = [];
187
188
        foreach ($table->getForeignKeys() as $name => $foreignKey) {
189
            $foreignKeys[$name] = ForeignKey::toArray($foreignKey);
190
        }
191
192
        return $foreignKeys;
193
    }
194
195
    public static function exists($tableName)
196
    {
197
        if (Driver::isMongoDB()) {
198
            return MongoDB::hasCollection($tableName);
199
        }
200
201
        if (!SchemaManager::getInstance()->tablesExist($tableName)) {
202
            throw SchemaException::tableDoesNotExist($tableName);
203
        }
204
205
        return true;
206
    }
207
208
    public static function paginate($perPage = 15, $page = null, $options = [], $query = "")
209
    {
210
        $page            = $page ?: (Paginator::resolveCurrentPage() ?: 1);
211
        $options['path'] = Paginator::resolveCurrentPath();
212
        $items           = static::all();
213
        $collection      = $items instanceof Collection ? $items : Collection::make($items);
214
        if (!empty($query)) {
215
            $collection = $collection->filter(function ($value, $key) use ($query) {
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

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

215
            $collection = $collection->filter(function ($value, /** @scrutinizer ignore-unused */ $key) use ($query) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
216
                return false !== stristr($value, $query);
217
            });
218
        }
219
220
        return new LengthAwarePaginator($collection->forPage($page, $perPage), $collection->count(), $perPage, $page, $options);
221
222
    }
223
}
224