Test Failed
Push — v2 ( 737a06 )
by Berend
03:55
created

Datefields::DatefieldsCreateHook()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
	protected function initDatefields()
20
	{
21
		$this->extendTableDefinition(TRAIT_DATEFIELDS_CREATED, [
0 ignored issues
show
Bug introduced by
It seems like extendTableDefinition() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
22
			'value' => &$this->created,
23
			'validate' => null,
24
			'type' => 'DATETIME',
25
			'default' => 'CURRENT_TIMESTAMP',
26
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
27
		]);
28
29
		$this->extendTableDefinition(TRAIT_DATEFIELDS_LAST_MODIFIED, [
0 ignored issues
show
Bug introduced by
It seems like extendTableDefinition() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
30
			'value' => &$this->lastModified,
31
			'validate' => null,
32
			'type' => 'DATETIME',
33
			'default' => 'CURRENT_TIMESTAMP',
34
			// 'on_update' => 'CURRENT_TIMESTAMP',		// @TODO(Discuss): Should we support this? (would not sync with object)
35
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
36
		]);
37
		
38
		$this->registerUpdateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsUpdateHook');
0 ignored issues
show
Bug introduced by
It seems like registerUpdateHook() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
39
		$this->registerCreateHook(TRAIT_DATEFIELDS_LAST_MODIFIED, 'DatefieldsCreateHook');
0 ignored issues
show
Bug introduced by
It seems like registerCreateHook() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
41
		$this->created = null;
42
		$this->lastModified = null;
43
	}
44
45
	protected function DatefieldsCreateHook()
46
	{
47
		// Should this be split up to seperate hooks for "last_modified" and "created" for consistency?
48
		$this->created = (new \DateTime('now'))->format('Y-m-d H:i:s');
49
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
50
	}
51
52
	protected function DatefieldsUpdateHook()
53
	{
54
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
55
	}
56
57
	public function getLastModifiedDate()
58
	{
59
		return new \DateTime($this->lastModified);
60
	}
61
62
	public function getCreationDate()
63
	{
64
		return new \DateTime($this->created);
65
	}
66
}
67
68