MongoCollectionMigration::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Modules\Mongo\Abstracts;
4
5
use Illuminate\Support\Facades\Schema;
6
use Jenssegers\Mongodb\Schema\Blueprint;
7
use Symfony\Component\CssSelector\Exception\InternalErrorException;
8
9
/**
10
 * Class MongoCollectionMigration.
11
 *
12
 * @method void migrate(Blueprint $collection)
13
 * @method void destroy()
14
 */
15
abstract class MongoCollectionMigration extends MongoMigration
16
{
17
    protected $collection;
18
19
    /**
20
     * MongoMigration constructor.
21
     */
22
    public function __construct()
23
    {
24
        if (! isset($this->collection) || $this->collection === '') {
25
            throw new InternalErrorException('Collection name must be specified on migration: '.get_called_class());
26
        }
27
    }
28
29
    /**
30
     * Run the migrations.
31
     *
32
     * @return void
33
     */
34
    final public function up()
35
    {
36
        if (! Schema::connection($this->connection)->hasTable($this->collection)) {
37
            Schema::connection($this->connection)->create($this->collection, function (Blueprint $collection) {
38
                if (method_exists($this, 'migrate')) {
39
                    $this->migrate($collection);
40
                }
41
            });
42
        }
43
    }
44
45
    /**
46
     * Reverse the migrations.
47
     *
48
     * @return void
49
     */
50
    final public function down()
51
    {
52
        if (Schema::connection($this->connection)->hasCollection($this->collection)) {
0 ignored issues
show
Bug introduced by
The method hasCollection() does not exist on Illuminate\Database\Schema\Builder. It seems like you code against a sub-type of Illuminate\Database\Schema\Builder such as Jenssegers\Mongodb\Schema\Builder. ( Ignorable by Annotation )

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

52
        if (Schema::connection($this->connection)->/** @scrutinizer ignore-call */ hasCollection($this->collection)) {
Loading history...
53
            if (method_exists($this, 'destroy')) {
54
                $this->destroy();
55
            }
56
            Schema::connection($this->connection)->drop($this->collection);
57
        }
58
    }
59
}
60