Passed
Branch dev_2x (3e8772)
by Adrian
01:42
created

Timestamps   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 32
c 0
b 0
f 0
dl 0
loc 113
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Sirius\Orm\Blueprint\Behaviour;
5
6
use Sirius\Orm\Blueprint\Behaviour;
7
use Sirius\Orm\Blueprint\Column;
8
use Sirius\Orm\Blueprint\Mapper;
9
use Sirius\Orm\Blueprint\MapperAwareTrait;
10
use Sirius\Orm\CodeGenerator\Observer\Behaviour\TimestampsObserver;
11
12
class Timestamps extends Behaviour
13
{
14
    use MapperAwareTrait;
15
16
    protected $createdAtColumn = 'created_at';
17
18
    protected $updatedAtColumn = 'updated_at';
19
20
    /**
21
     * @var TimestampsObserver
22
     */
23
    protected $observer;
24
25
    public static function make($createdAtColumn = 'created_at', $updatedAtColumn = 'updated_at')
26
    {
27
        return (new static)
28
            ->setCreatedAtColumn($createdAtColumn)
29
            ->setUpdatedAtColumn($updatedAtColumn);
30
    }
31
32
    public function getObservers(): array
33
    {
34
        $observer = $this->getObserver()->with($this);
35
36
        return [
37
            $this->mapper->getName() . '_base_mapper' => [$observer],
38
            $this->mapper->getName() . '_base_query'  => [$observer]
39
        ];
40
    }
41
42
    public function getName(): string
43
    {
44
        return 'timestamps';
45
    }
46
47
    /**
48
     * @return string
49
     */
50
    public function getCreatedAtColumn(): string
51
    {
52
        return $this->createdAtColumn;
53
    }
54
55
    /**
56
     * @param string $createdAtColumn
57
     *
58
     * @return Timestamps
59
     */
60
    public function setCreatedAtColumn(string $createdAtColumn): Timestamps
61
    {
62
        $this->createdAtColumn = $createdAtColumn;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getUpdatedAtColumn(): string
71
    {
72
        return $this->updatedAtColumn;
73
    }
74
75
    /**
76
     * @param string $updatedAtColumn
77
     *
78
     * @return Timestamps
79
     */
80
    public function setUpdatedAtColumn(string $updatedAtColumn): Timestamps
81
    {
82
        $this->updatedAtColumn = $updatedAtColumn;
83
84
        return $this;
85
    }
86
87
    public function setMapper(Mapper $mapper): self
88
    {
89
        $this->mapper = $mapper;
90
91
        $columns = $mapper->getColumns();
92
93
        if ($this->createdAtColumn && ! array_key_exists($this->createdAtColumn, $columns)) {
94
            $mapper->addColumn(Column::datetime($this->createdAtColumn)
95
                                     ->setNullable(true));
96
        }
97
98
        if ($this->updatedAtColumn && ! array_key_exists($this->updatedAtColumn, $columns)) {
99
            $mapper->addColumn(Column::datetime($this->updatedAtColumn)
100
                                     ->setNullable(true)
101
                                     ->setAfter($this->createdAtColumn));
102
        }
103
104
        return $this;
105
    }
106
107
    /**
108
     * @return mixed
109
     */
110
    public function getObserver()
111
    {
112
        return $this->observer ?? new TimestampsObserver();
113
    }
114
115
    /**
116
     * @param mixed $observer
117
     *
118
     * @return Timestamps
119
     */
120
    public function setObserver($observer)
121
    {
122
        $this->observer = $observer;
123
124
        return $this;
125
    }
126
}
127