NestedSet   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
dl 0
loc 92
c 0
b 0
f 0
wmc 5
lcom 0
cbo 1
ccs 10
cts 15
cp 0.6667
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultColumns() 0 4 1
A columns() 0 8 1
A dropColumns() 0 7 1
A isNode() 0 4 2
1
<?php namespace Arcanedev\LaravelNestedSet\Utilities;
2
3
use Arcanedev\LaravelNestedSet\NodeTrait;
4
use Illuminate\Database\Schema\Blueprint;
5
6
/**
7
 * Class     NestedSet
8
 *
9
 * @package  Arcanedev\LaravelNestedSet\Utilities
10
 * @author   ARCANEDEV <[email protected]>
11
 */
12
class NestedSet
13
{
14
    /* -----------------------------------------------------------------
15
     |  Constants
16
     | -----------------------------------------------------------------
17
     */
18
    /**
19
     * The name of default lft column.
20
     */
21
    const LFT       = '_lft';
22
23
    /**
24
     * The name of default rgt column.
25
     */
26
    const RGT       = '_rgt';
27
28
    /**
29
     * The name of default parent id column.
30
     */
31
    const PARENT_ID = 'parent_id';
32
33
    /**
34
     * Insert direction.
35
     */
36
    const BEFORE    = 1;
37
38
    /**
39
     * Insert direction.
40
     */
41
    const AFTER     = 2;
42
43
    /* -----------------------------------------------------------------
44
     |  Getters & Setters
45
     | -----------------------------------------------------------------
46
     */
47
    /**
48
     * Get a list of default columns.
49
     *
50
     * @return array
51
     */
52 216
    public static function getDefaultColumns()
53
    {
54 216
        return [self::LFT, self::RGT, self::PARENT_ID];
55
    }
56
57
    /* -----------------------------------------------------------------
58
     |  Migration Methods
59
     | -----------------------------------------------------------------
60
     */
61
    /**
62
     * Add default nested set columns to the table. Also create an index.
63
     *
64
     * @param  \Illuminate\Database\Schema\Blueprint  $table
65
     */
66 216
    public static function columns(Blueprint $table)
67
    {
68 216
        $table->unsignedInteger(self::LFT);
69 216
        $table->unsignedInteger(self::RGT);
70 216
        $table->unsignedInteger(self::PARENT_ID)->nullable();
71
72 216
        $table->index(self::getDefaultColumns());
73 216
    }
74
75
    /**
76
     * Drop NestedSet columns.
77
     *
78
     * @param  \Illuminate\Database\Schema\Blueprint  $table
79
     */
80
    public static function dropColumns(Blueprint $table)
81
    {
82
        $columns = self::getDefaultColumns();
83
84
        $table->dropIndex($columns);
85
        $table->dropColumn($columns);
86
    }
87
88
    /* -----------------------------------------------------------------
89
     |  Check Methods
90
     | -----------------------------------------------------------------
91
     */
92
    /**
93
     * Replaces instanceof calls for this trait.
94
     *
95
     * @param  mixed  $node
96
     *
97
     * @return bool
98
     */
99 72
    public static function isNode($node)
100
    {
101 72
        return is_object($node) && in_array(NodeTrait::class, (array) $node);
102
    }
103
}
104