Passed
Push — master ( 756a96...c3a1af )
by Arthur
06:27
created

MongoCollectionMigration::up()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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