Datefields::DatefieldsLastModifiedCreateHook()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace miBadger\ActiveRecord\Traits;
4
5
use miBadger\Query\Query;
6
use miBadger\ActiveRecord\ColumnProperty;
7
8
const TRAIT_DATEFIELDS_LAST_MODIFIED = "last_modified";
9
const TRAIT_DATEFIELDS_CREATED = "created";
10
11
trait Datefields
12
{
13
	/** @var string The timestamp representing the moment this record was created */
14
	protected $created;
15
16
	/** @var string The timestamp representing the moment this record was last updated */
17
	protected $lastModified;
18
19
	/**
20
	 * this method is required to be called in the constructor for each class that uses this trait. 
21
	 * It adds the datefields to the table definition and registers the callback hooks
22
	 */
23 3
	protected function initDatefields()
24
	{
25 3
		$this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [
26 3
			'value' => &$this->created,
27
			'validate' => null,
28 3
			'type' => 'DATETIME',
29 3
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
30
		]);
31
32 3
		$this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [
33 3
			'value' => &$this->lastModified,
34
			'validate' => null,
35 3
			'type' => 'DATETIME',
36 3
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
37
		]);
38
		
39 3
		$this->registerCreateHook(TRAIT_DATEFIELDS_CREATED, 'DatefieldsLastModifiedCreateHook');
40 3
		$this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreatedCreateHook');
41 3
		$this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook');
42
43 3
		$this->created = null;
44 3
		$this->lastModified = null;
45 3
	}
46
47
	/**
48
	 * The hook that gets called to set the last modified timestamp whenever a new record is created
49
	 */
50 2
	protected function DatefieldsLastModifiedCreateHook()
51
	{
52 2
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
53 2
	}
54
55
	/**
56
	 * The hook that gets called to set the created timestamp whenever a new record is created
57
	 */
58 2
	protected function DatefieldsCreatedCreateHook()
59
	{
60 2
		$this->created = (new \DateTime('now'))->format('Y-m-d H:i:s');
61 2
	}
62
63
	/**
64
	 * The hook that gets called to set the timestamp whenever a record gets updated
65
	 */
66 1
	protected function DatefieldsUpdateHook()
67
	{
68 1
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
69 1
	}
70
71
	/**
72
	 * Returns the timestamp of last update for this record
73
	 * @return \DateTime
74
	 */
75 1
	public function getLastModifiedDate()
76
	{
77 1
		return new \DateTime($this->lastModified);
78
	}
79
80
	/**
81
	 * Returns the timestamp of when this record was created
82
	 * @return \DateTime
83
	 */
84 2
	public function getCreationDate()
85
	{
86 2
		return new \DateTime($this->created);
87
	}
88
89
	/**
90
	 * @return void
91
	 */
92
	abstract protected function extendTableDefinition(string $columnName, $definition);
93
	
94
	/**
95
	 * @return void
96
	 */
97
	abstract protected function registerSearchHook(string $columnName, $fn);
98
99
	/**
100
	 * @return void
101
	 */
102
	abstract protected function registerDeleteHook(string $columnName, $fn);
103
104
	/**
105
	 * @return void
106
	 */
107
	abstract protected function registerUpdateHook(string $columnName, $fn);
108
109
	/**
110
	 * @return void
111
	 */
112
	abstract protected function registerReadHook(string $columnName, $fn);
113
114
	/**
115
	 * @return void
116
	 */
117
	abstract protected function registerCreateHook(string $columnName, $fn);
118
}
119
120