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

ModelObjNode::discover()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 0
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\orm\tree_traversal;
4
5
use EE_Base_Class;
6
use EE_HABTM_Relation;
7
use EE_Has_Many_Relation;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
use ReflectionException;
12
13
/**
14
 * Class ModelObjNode
15
 * Wraps a model object and stores which of its model's relations have already been traversed and which haven't.
16
 *
17
 * @package     Event Espresso
18
 * @author         Mike Nelson
19
 * @since         $VID:$
20
 *
21
 */
22
class ModelObjNode extends BaseNode
23
{
24
    /**
25
     * @var EE_Base_Class
26
     */
27
    protected $model_obj;
28
29
    /**
30
     * @var RelationNode[]
31
     */
32
    protected $relation_nodes;
33
34
    public function __construct($instance)
35
    {
36
        $this->model_obj = $instance;
37
    }
38
39
    /**
40
     * Creates a relation node for each relation of this model's relations.
41
     * Does NOT call `discover` on them yet though.
42
     * @since $VID:$
43
     * @throws \EE_Error
44
     * @throws InvalidDataTypeException
45
     * @throws InvalidInterfaceException
46
     * @throws InvalidArgumentException
47
     * @throws ReflectionException
48
     */
49
    protected function discover()
50
    {
51
        $this->relation_nodes = [];
52
        foreach ($this->model_obj->get_model()->relation_settings() as $relationName => $relation) {
53
            if ($relation instanceof EE_Has_Many_Relation) {
54
                $this->relation_nodes[ $relationName ] = new RelationNode($this->model_obj, $relation->get_other_model());
55
            } elseif ($relation instanceof EE_HABTM_Relation) {
56
                $this->relation_nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode($this->model_obj, $relation->get_join_model());
57
            }
58
        }
59
        ksort($this->relation_nodes);
60
    }
61
62
63
    /**
64
     * Whether this item has already been initialized
65
     */
66
    protected function isDiscovered()
67
    {
68
        return $this->relation_nodes !== null && is_array($this->relation_nodes);
69
    }
70
71
    /**
72
     * @since $VID:$
73
     * @return boolean
74
     */
75
    public function isComplete()
76
    {
77
        if ($this->complete === null) {
78
            $this->complete = false;
79
        }
80
        return $this->complete;
81
    }
82
83
    /**
84
     * Triggers working on each child relation node that has work to do.
85
     * @since $VID:$
86
     * @param $model_objects_to_identify
87
     * @return int units of work done
88
     */
89
    protected function work($model_objects_to_identify)
90
    {
91
        $num_identified = 0;
92
        // Begin assuming we'll finish all the work on this node and its children...
93
        $this->complete = true;
94
        foreach ($this->relation_nodes as $relation_node) {
95
            $num_identified += $relation_node->visit($model_objects_to_identify);
96
            if ($num_identified >= $model_objects_to_identify) {
97
                // ...but admit we're wrong if the work exceeded the budget.
98
                $this->complete = false;
99
                break;
100
            }
101
        }
102
        return $num_identified;
103
    }
104
105
    /**
106
     * @since $VID:$
107
     * @return array
108
     * @throws \EE_Error
109
     * @throws InvalidDataTypeException
110
     * @throws InvalidInterfaceException
111
     * @throws InvalidArgumentException
112
     * @throws ReflectionException
113
     */
114
    public function toArray()
115
    {
116
        $tree = [
117
            'id' => $this->model_obj->ID(),
118
            'complete' => $this->isComplete(),
119
            'rels' => []
120
        ];
121
        if ($this->relation_nodes === null) {
122
            $tree['rels'] = null;
123
        } else {
124
            foreach ($this->relation_nodes as $relation_name => $relation_node) {
125
                $tree['rels'][ $relation_name ] = $relation_node->toArray();
126
            }
127
        }
128
        return $tree;
129
    }
130
}
131
// End of file Visitor.php
132
// Location: EventEspresso\core\services\orm\tree_traversal/Visitor.php
133