Completed
Pull Request — master (#1)
by David
07:23
created

FluidColumnOptions   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 100
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A notNull() 0 5 1
A null() 0 5 1
A unique() 0 5 1
A index() 0 5 1
A comment() 0 5 1
A autoIncrement() 0 5 1
A primaryKey() 0 7 2
A default() 0 5 1
A then() 0 4 1
A column() 0 4 1
1
<?php
2
3
4
namespace TheCodingMachine\FluidSchema;
5
6
7
use Doctrine\DBAL\Schema\Column;
8
use Doctrine\DBAL\Types\Type;
9
10
class FluidColumnOptions
11
{
12
    /**
13
     * @var FluidTable
14
     */
15
    private $fluidTable;
16
    /**
17
     * @var Column
18
     */
19
    private $column;
20
21
    /**
22
     * FluidColumn constructor.
23
     * @param FluidTable $fluidTable
24
     * @param Column $column
25
     */
26
    public function __construct(FluidTable $fluidTable, Column $column)
27
    {
28
        $this->fluidTable = $fluidTable;
29
        $this->column = $column;
30
    }
31
32
    /**
33
     * Makes the column not nullable.
34
     * @return FluidColumnOptions
35
     */
36
    public function notNull(): FluidColumnOptions
37
    {
38
        $this->column->setNotnull(true);
39
        return $this;
40
    }
41
42
    /**
43
     * Makes the column nullable.
44
     * @return FluidColumnOptions
45
     */
46
    public function null(): FluidColumnOptions
47
    {
48
        $this->column->setNotnull(false);
49
        return $this;
50
    }
51
52
    /**
53
     * Automatically add a unique constraint for the column.
54
     *
55
     * @return FluidColumnOptions
56
     */
57
    public function unique(): FluidColumnOptions
58
    {
59
        $this->column->setCustomSchemaOption('unique', true);
60
        return $this;
61
    }
62
63
    /**
64
     * Automatically add an index for the column.
65
     *
66
     * @return FluidColumnOptions
67
     */
68
    public function index(): FluidColumnOptions
69
    {
70
        $this->fluidTable->index([$this->column->getName()]);
71
        return $this;
72
    }
73
    public function comment(string $comment): FluidColumnOptions
74
    {
75
        $this->column->setComment($comment);
76
        return $this;
77
    }
78
79
    public function autoIncrement(): FluidColumnOptions
80
    {
81
        $this->column->setAutoincrement(true);
82
        return $this;
83
    }
84
85
    public function primaryKey(?string $indexName = null): FluidColumnOptions
86
    {
87
        $newIndexName = $indexName ?: false;
88
89
        $this->fluidTable->primaryKey([$this->column->getName()], $newIndexName);
90
        return $this;
91
    }
92
93
    public function default($defaultValue): FluidColumnOptions
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
94
    {
95
        $this->column->setDefault($defaultValue);
96
        return $this;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
97
    }
98
99
    public function then(): FluidTable
100
    {
101
        return $this->fluidTable;
102
    }
103
104
    public function column($name): FluidColumn
105
    {
106
        return $this->fluidTable->column($name);
107
    }
108
109
}
110