Completed
Branch CASC/base (79f9d1)
by
unknown
19:48 queued 09:56
created

BaseNode   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 102
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
isDiscovered() 0 1 ?
isComplete() 0 1 ?
discover() 0 1 ?
work() 0 1 ?
toArray() 0 1 ?
A visit() 0 10 3
getIds() 0 1 ?
A __sleep() 0 9 1
A __wakeup() 0 5 1
1
<?php
2
3
namespace EventEspresso\core\services\orm\tree_traversal;
4
5
/**
6
 * Class BaseNode
7
 *
8
 * Represents a task to be done when traversing a model object tree to identify all the dependent model objects.
9
 * See the concrete classes for details, but the basic structure is you have a starter ModelObjNode that wraps the
10
 * model object whose dependent related objects you want to identify, it has a list of RelationNodes (one for each of
11
 * its model's relations); each of those builds a list of ModelObjNodes for each of the related model objects,
12
 * recursively.
13
 * Client code creates a ModelObjNode, provides  it with a model object, and repeatedly `visit()` on it until all of its
14
 * related model objects are identified.
15
 *
16
 * When traversing the tree of objects, the pseudo code is this:
17
 *
18
 * Start off with a model object, and a budget of how many model objects we want to discover
19
 * For each of its relations:
20
 *      count the number of related model objects
21
 *      then fetch some of them from the DB (no more than what's in our budget)
22
 *      record how many we fetched, and compare it to our budget
23
 *      if we're at the budget's limit, stop.
24
 *      otherwise, for each of them:
25
 *          start again with it being the root model object.
26
 *
27
 * @package     Event Espresso
28
 * @author         Mike Nelson
29
 * @since         $VID:$
30
 *
31
 */
32
abstract class BaseNode
33
{
34
    /**
35
     * @var boolean
36
     */
37
    protected $complete;
38
39
40
    /**
41
     * @var array of model names we don't want to traverse
42
     */
43
    protected $dont_traverse_models;
44
45
    /**
46
     * Whether this item has already been initialized
47
     */
48
    abstract protected function isDiscovered();
49
50
    /**
51
     * Determines if the work is done yet or not. Requires you to have first discovered what work exists by calling
52
     * discover().
53
     * @since $VID:$
54
     * @return boolean
55
     */
56
    abstract public function isComplete();
57
58
    /**
59
     * Discovers what work needs to be done to complete traversing this node and its children.
60
     * Note that this is separate from the constructor, so we can create child nodes without
61
     * discovering them immediately.
62
     * @since $VID:$
63
     * @return mixed
64
     */
65
    abstract protected function discover();
66
67
    /**
68
     * Identifies model objects, up to the limit $model_objects_to_identify.
69
     * @since $VID:$
70
     * @param int $model_objects_to_identify
71
     * @return int units of work done
72
     */
73
    abstract protected function work($model_objects_to_identify);
74
75
    /**
76
     * Shows the entity/relation node as an array.
77
     * @since $VID:$
78
     * @return array
79
     */
80
    abstract public function toArray();
81
82
    /**
83
     * Discovers how much work there is to do, double-checks the work isn't already finished, and then does the work.
84
     * Note: do not call when site is in maintenance mode level 2.
85
     *
86
     * @since $VID:$
87
     * @param $model_objects_to_identify
88
     * @return int number of model objects we want to identify during this call. On subsequent calls we'll continue
89
     * where we left off.
90
     */
91
    public function visit($model_objects_to_identify)
92
    {
93
        if (! $this->isDiscovered()) {
94
            $this->discover();
95
        }
96
        if ($this->isComplete()) {
97
            return 0;
98
        }
99
        return $this->work($model_objects_to_identify);
100
    }
101
102
    /**
103
     * Gets the IDs of completely identified model objects that can be deleted.
104
     * @since $VID:$
105
     * @return mixed
106
     */
107
    abstract public function getIds();
108
109
    /**
110
     * Make sure we encode whether its complete or not, but don't use such a long name.
111
     * @since $VID:$
112
     * @return array
113
     */
114
    public function __sleep()
115
    {
116
        $this->c = $this->complete;
0 ignored issues
show
Bug introduced by
The property c does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
117
        $this->dtm = $this->dont_traverse_models;
0 ignored issues
show
Bug introduced by
The property dtm does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
118
        return [
119
            'c',
120
            'dtm'
121
        ];
122
    }
123
124
    /**
125
     * Use the dynamic property to set the "complete" property.
126
     * @since $VID:$
127
     */
128
    public function __wakeup()
129
    {
130
        $this->complete = $this->c;
131
        $this->dont_traverse_models = $this->dtm;
132
    }
133
}
134
// End of file BaseNode.php
135
// Location: EventEspresso\core\services\orm\tree_traversal/BaseNode.php
136