Completed
Branch CASC/initial-ui (c5e0e6)
by
unknown
32:13 queued 24:29
created

BaseNode::visit()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
     * Whether this item has already been initialized
40
     */
41
    abstract protected function isDiscovered();
42
43
    /**
44
     * Determines if the work is done yet or not. Requires you to have first discovered what work exists by calling
45
     * discover().
46
     * @since $VID:$
47
     * @return boolean
48
     */
49
    abstract public function isComplete();
50
51
    /**
52
     * Discovers what work needs to be done to complete traversing this node and its children.
53
     * Note that this is separate from the constructor, so we can create child nodes without
54
     * discovering them immediately.
55
     * @since $VID:$
56
     * @return mixed
57
     */
58
    abstract protected function discover();
59
60
    /**
61
     * Identifies model objects, up to the limit $model_objects_to_identify.
62
     * @since $VID:$
63
     * @param int $model_objects_to_identify
64
     * @return int units of work done
65
     */
66
    abstract protected function work($model_objects_to_identify);
67
68
    /**
69
     * Shows the entity/relation node as an array.
70
     * @since $VID:$
71
     * @return array
72
     */
73
    abstract public function toArray();
74
75
    /**
76
     * Discovers how much work there is to do, double-checks the work isn't already finished, and then does the work.
77
     * Note: do not call when site is in maintenance mode level 2.
78
     *
79
     * @since $VID:$
80
     * @param $model_objects_to_identify
81
     * @return int number of model objects we want to identify during this call. On subsequent calls we'll continue
82
     * where we left off.
83
     */
84
    public function visit($model_objects_to_identify)
85
    {
86
        if (! $this->isDiscovered()) {
87
            $this->discover();
88
        }
89
        if ($this->isComplete()) {
90
            return 0;
91
        }
92
        return $this->work($model_objects_to_identify);
93
    }
94
}
95
// End of file BaseNode.php
96
// Location: EventEspresso\core\services\orm\tree_traversal/BaseNode.php
97