Passed
Push — v2 ( ee04d9...67b65f )
by Berend
02:48
created

Datefields   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 73
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initDatefields() 0 24 1
A DatefieldsCreateHook() 0 6 1
A DatefieldsUpdateHook() 0 4 1
A getLastModifiedDate() 0 4 1
A getCreationDate() 0 4 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, [
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...
26 3
			'value' => &$this->created,
27
			'validate' => null,
28 3
			'type' => 'DATETIME',
29 3
			'default' => 'CURRENT_TIMESTAMP',
30 3
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
31
		]);
32
33 3
		$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...
34 3
			'value' => &$this->lastModified,
35
			'validate' => null,
36 3
			'type' => 'DATETIME',
37 3
			'default' => 'CURRENT_TIMESTAMP',
38 3
			'properties' => ColumnProperty::NOT_NULL | ColumnProperty::IMMUTABLE
39
		]);
40
		
41 3
		$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...
42 3
		$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...
43
44 3
		$this->created = null;
45 3
		$this->lastModified = null;
46 3
	}
47
48
	/**
49
	 * The hook that gets called to set the timestamp whenever a new record is created
50
	 */
51 2
	protected function DatefieldsCreateHook()
52
	{
53
		// @TODO: Should this be split up to seperate hooks for "last_modified" and "created" for consistency?
54 2
		$this->created = (new \DateTime('now'))->format('Y-m-d H:i:s');
55 2
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
56 2
	}
57
58
	/**
59
	 * The hook that gets called to set the timestamp whenever a record gets updated
60
	 */
61 1
	protected function DatefieldsUpdateHook()
62
	{
63 1
		$this->lastModified = (new \DateTime('now'))->format('Y-m-d H:i:s');
64 1
	}
65
66
	/**
67
	 * Returns the timestamp of last update for this record
68
	 * @return DateTime
0 ignored issues
show
Documentation introduced by
Should the return type not be \DateTime?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
69
	 */
70 1
	public function getLastModifiedDate()
71
	{
72 1
		return new \DateTime($this->lastModified);
73
	}
74
75
	/**
76
	 * Returns the timestamp of when this record was created
77
	 * @return DateTime
0 ignored issues
show
Documentation introduced by
Should the return type not be \DateTime?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
78
	 */
79 2
	public function getCreationDate()
80
	{
81 2
		return new \DateTime($this->created);
82
	}
83
}
84
85