MongoMigration::__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 4
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 03.10.18
6
 * Time: 20:57.
7
 */
8
9
namespace Foundation\Abstracts\Migrations;
10
11
use Illuminate\Support\Facades\Schema;
12
use Jenssegers\Mongodb\Schema\Blueprint;
13
use Symfony\Component\CssSelector\Exception\InternalErrorException;
14
15
abstract class MongoMigration extends \Illuminate\Database\Migrations\Migration
16
{
17
    protected $connection = 'mongodb';
18
19
    protected $collection;
20
21
    /**
22
     * MongoMigration constructor.
23
     */
24
    public function __construct()
25
    {
26
        if (! isset($this->collection) || $this->collection === '') {
27
            throw new InternalErrorException('Collection name must be specified on migration: '.get_called_class());
28
        }
29
    }
30
31
    abstract protected function migrate(Blueprint $collection);
32
33
    /**
34
     * Run the migrations.
35
     *
36
     * @return void
37
     */
38
    final public function up()
39
    {
40
        if (! Schema::connection($this->connection)->hasTable($this->collection)) {
41
            Schema::connection($this->connection)->create($this->collection, function (Blueprint $collection) {
42
                $this->migrate($collection);
43
            });
44
        }
45
    }
46
47
    /**
48
     * Reverse the migrations.
49
     *
50
     * @return void
51
     */
52
    final public function down()
53
    {
54
        if (Schema::connection($this->connection)->hasTable($this->collection)) {
55
            Schema::connection($this->connection)->drop($this->collection);
56
        }
57
    }
58
}
59