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

SoftDelete   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 78.26%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 71
ccs 18
cts 23
cp 0.7826
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A softDeleteSearchHook() 0 4 1
A getSoftDeleteFieldName() 0 4 1
A initSoftDelete() 0 15 1
A softDelete() 0 5 1
A softRestore() 0 5 1
A getDeletionStatus() 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_SOFT_DELETE_FIELD_KEY = "soft_delete";
9
10
trait SoftDelete
11
{
12
	/** @var boolean the soft delete status for the entity this trait is embedded into. */
13
	protected $softDelete;
14
15
	/**
16
	 * this method is required to be called in the constructor for each class that uses this trait. 
17
	 * It adds the required fields to the table definition and registers hooks
18
	 */
19 10
	protected function initSoftDelete()
20
	{
21 10
		$this->softDelete = false;
22
23 10
		$this->extendTableDefinition(TRAIT_SOFT_DELETE_FIELD_KEY, [
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...
24 10
			'value' => &$this->softDelete,
25
			'validate' => null,
26 10
			'default' => 0,
27 10
			'type' => 'INT',
28 10
			'length' => 1,
29 10
			'properties' => ColumnProperty::NOT_NULL
30
		]);
31
32 10
		$this->registerSearchHook(TRAIT_SOFT_DELETE_FIELD_KEY, 'softDeleteSearchHook');
0 ignored issues
show
Bug introduced by
It seems like registerSearchHook() 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...
33 10
	}
34
35
	/**
36
	 * The hook that gets called whenever a query is made
37
	 */
38
	protected function softDeleteSearchHook(Query $query)
39
	{
40
		$query->where(TRAIT_SOFT_DELETE_FIELD_KEY, '=', 0);
41
	}
42
43
	/**
44
	 * returns the name for the soft delete field in the database
45
	 * @return string
46
	 */
47
	public function getSoftDeleteFieldName()
48
	{
49
		return TRAIT_SOFT_DELETE_FIELD_KEY;
50
	}
51
	
52
	/**
53
	 * Mark the current record as soft deleted
54
	 * @return $this
55
	 */
56 1
	public function softDelete()
57
	{
58 1
		$this->softDelete = true;
59 1
		return $this;
60
	}
61
62
	/**
63
	 * Undo the current soft deletion status (mark it as non-soft deleted)
64
	 * @return $this
65
	 */
66 1
	public function softRestore()
67
	{
68 1
		$this->softDelete = false;
69 1
		return $this;
70
	}
71
72
	/**
73
	 * returns the current soft deletion status
74
	 * @return $this
75
	 */
76 1
	public function getDeletionStatus() 
77
	{
78 1
		return $this->softDelete;
79
	}
80
}