Completed
Branch CASC/base (3cd0b5)
by
unknown
24:25 queued 16:20
created
core/services/orm/tree_traversal/BaseNode.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@
 block discarded – undo
68 68
      */
69 69
     public function visit($model_objects_to_identify)
70 70
     {
71
-        if (! $this->isDiscovered()) {
71
+        if ( ! $this->isDiscovered()) {
72 72
             $this->discover();
73 73
         }
74 74
         if ($this->isComplete()) {
Please login to merge, or discard this patch.
Indentation   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -31,73 +31,73 @@
 block discarded – undo
31 31
  */
32 32
 abstract class BaseNode
33 33
 {
34
-    /**
35
-     * @var boolean
36
-     */
37
-    protected $complete;
38
-    /**
39
-     * Whether this item has already been initialized
40
-     */
41
-    abstract protected function isDiscovered();
34
+	/**
35
+	 * @var boolean
36
+	 */
37
+	protected $complete;
38
+	/**
39
+	 * Whether this item has already been initialized
40
+	 */
41
+	abstract protected function isDiscovered();
42 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();
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 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();
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 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);
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 67
 
68
-    /**
69
-     * Shows the entity/relation node as an array.
70
-     * @since $VID:$
71
-     * @return array
72
-     */
73
-    abstract public function toArray();
68
+	/**
69
+	 * Shows the entity/relation node as an array.
70
+	 * @since $VID:$
71
+	 * @return array
72
+	 */
73
+	abstract public function toArray();
74 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
-    }
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 94
 
95
-    /**
96
-     * Gets the IDs of completely identified model objects that can be deleted.
97
-     * @since $VID:$
98
-     * @return mixed
99
-     */
100
-    abstract public function getIds();
95
+	/**
96
+	 * Gets the IDs of completely identified model objects that can be deleted.
97
+	 * @since $VID:$
98
+	 * @return mixed
99
+	 */
100
+	abstract public function getIds();
101 101
 }
102 102
 // End of file BaseNode.php
103 103
 // Location: EventEspresso\core\services\orm\tree_traversal/BaseNode.php
Please login to merge, or discard this patch.
core/services/orm/tree_traversal/ModelObjNode.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -51,9 +51,9 @@  discard block
 block discarded – undo
51 51
         $this->relation_nodes = [];
52 52
         foreach ($this->model_obj->get_model()->relation_settings() as $relationName => $relation) {
53 53
             if ($relation instanceof EE_Has_Many_Relation) {
54
-                $this->relation_nodes[ $relationName ] = new RelationNode($this->model_obj, $relation->get_other_model());
54
+                $this->relation_nodes[$relationName] = new RelationNode($this->model_obj, $relation->get_other_model());
55 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());
56
+                $this->relation_nodes[$relation->get_join_model()->get_this_model_name()] = new RelationNode($this->model_obj, $relation->get_join_model());
57 57
             }
58 58
         }
59 59
         ksort($this->relation_nodes);
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
             $tree['rels'] = null;
123 123
         } else {
124 124
             foreach ($this->relation_nodes as $relation_name => $relation_node) {
125
-                $tree['rels'][ $relation_name ] = $relation_node->toArray();
125
+                $tree['rels'][$relation_name] = $relation_node->toArray();
126 126
             }
127 127
         }
128 128
         return $tree;
Please login to merge, or discard this patch.
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -21,134 +21,134 @@
 block discarded – undo
21 21
  */
22 22
 class ModelObjNode extends BaseNode
23 23
 {
24
-    /**
25
-     * @var EE_Base_Class
26
-     */
27
-    protected $model_obj;
24
+	/**
25
+	 * @var EE_Base_Class
26
+	 */
27
+	protected $model_obj;
28 28
 
29
-    /**
30
-     * @var RelationNode[]
31
-     */
32
-    protected $relation_nodes;
29
+	/**
30
+	 * @var RelationNode[]
31
+	 */
32
+	protected $relation_nodes;
33 33
 
34
-    public function __construct($instance)
35
-    {
36
-        $this->model_obj = $instance;
37
-    }
34
+	public function __construct($instance)
35
+	{
36
+		$this->model_obj = $instance;
37
+	}
38 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
-    }
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 61
 
62 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
-    }
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 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
-    }
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 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
-    }
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 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
-    }
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 130
 
131
-    /**
132
-     * @since $VID:$
133
-     * @return array|mixed
134
-     * @throws InvalidArgumentException
135
-     * @throws InvalidDataTypeException
136
-     * @throws InvalidInterfaceException
137
-     * @throws ReflectionException
138
-     * @throws \EE_Error
139
-     */
140
-    public function getIds()
141
-    {
142
-        $ids = [
143
-            $this->model_obj->get_model()->get_this_model_name() => [
144
-                $this->model_obj->ID() => $this->model_obj->ID()
145
-            ]
146
-        ];
147
-        foreach ($this->relation_nodes as $relation_node) {
148
-            $ids = array_replace_recursive($ids, $relation_node->getIds());
149
-        }
150
-        return $ids;
151
-    }
131
+	/**
132
+	 * @since $VID:$
133
+	 * @return array|mixed
134
+	 * @throws InvalidArgumentException
135
+	 * @throws InvalidDataTypeException
136
+	 * @throws InvalidInterfaceException
137
+	 * @throws ReflectionException
138
+	 * @throws \EE_Error
139
+	 */
140
+	public function getIds()
141
+	{
142
+		$ids = [
143
+			$this->model_obj->get_model()->get_this_model_name() => [
144
+				$this->model_obj->ID() => $this->model_obj->ID()
145
+			]
146
+		];
147
+		foreach ($this->relation_nodes as $relation_node) {
148
+			$ids = array_replace_recursive($ids, $relation_node->getIds());
149
+		}
150
+		return $ids;
151
+	}
152 152
 }
153 153
 // End of file Visitor.php
154 154
 // Location: EventEspresso\core\services\orm\tree_traversal/Visitor.php
Please login to merge, or discard this patch.
core/services/orm/tree_traversal/RelationNode.php 4 patches
Doc Comments   +5 added lines, -1 removed lines patch added patch discarded remove patch
@@ -46,6 +46,10 @@  discard block
 block discarded – undo
46 46
      */
47 47
     protected $model_obj_nodes;
48 48
 
49
+    /**
50
+     * @param EE_Base_Class $main_model_obj
51
+     * @param EEM_Base|null $related_model
52
+     */
49 53
     public function __construct($main_model_obj, $related_model)
50 54
     {
51 55
         $this->main_model_obj = $main_model_obj;
@@ -122,7 +126,7 @@  discard block
 block discarded – undo
122 126
      * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget.
123 127
      * @since $VID:$
124 128
      * @param ModelObjNode[] $model_obj_nodes
125
-     * @param $work_budget
129
+     * @param integer $work_budget
126 130
      * @return int
127 131
      */
128 132
     protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
Please login to merge, or discard this patch.
Unused Use Statements   -2 removed lines patch added patch discarded remove patch
@@ -5,11 +5,9 @@
 block discarded – undo
5 5
 use EE_Base_Class;
6 6
 use EE_Error;
7 7
 use EE_Has_Many_Any_Relation;
8
-use EE_Model_Relation_Base;
9 8
 use EEM_Base;
10 9
 use EventEspresso\core\exceptions\InvalidDataTypeException;
11 10
 use EventEspresso\core\exceptions\InvalidInterfaceException;
12
-use EventEspresso\core\services\payment_methods\forms\PayPalSettingsForm;
13 11
 use InvalidArgumentException;
14 12
 use ReflectionException;
15 13
 
Please login to merge, or discard this patch.
Indentation   +219 added lines, -219 removed lines patch added patch discarded remove patch
@@ -26,225 +26,225 @@
 block discarded – undo
26 26
  */
27 27
 class RelationNode extends BaseNode
28 28
 {
29
-    /**
30
-     * @var EE_Base_Class
31
-     */
32
-    protected $main_model_obj;
33
-
34
-    /**
35
-     * @var int
36
-     */
37
-    protected $count;
38
-
39
-    /**
40
-     * @var EEM_Base
41
-     */
42
-    protected $related_model;
43
-
44
-    /**
45
-     * @var ModelObjNode[]
46
-     */
47
-    protected $model_obj_nodes;
48
-
49
-    public function __construct($main_model_obj, $related_model)
50
-    {
51
-        $this->main_model_obj = $main_model_obj;
52
-        $this->related_model = $related_model;
53
-        $this->model_obj_nodes = [];
54
-    }
55
-
56
-
57
-    /**
58
-     * Here is where most of the work happens. We've counted how many related model objects exist, here we identify
59
-     * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc.
60
-     * @since $VID:$
61
-     * @param int $model_objects_to_identify
62
-     * @return int
63
-     * @throws EE_Error
64
-     * @throws InvalidArgumentException
65
-     * @throws InvalidDataTypeException
66
-     * @throws InvalidInterfaceException
67
-     * @throws ReflectionException
68
-     */
69
-    protected function work($model_objects_to_identify)
70
-    {
71
-        $num_identified = $this->visitAlreadyDiscoveredNodes($this->model_obj_nodes, $model_objects_to_identify);
72
-        if ($num_identified < $model_objects_to_identify) {
73
-            $related_model_objs = $this->related_model->get_all(
74
-                [
75
-                    $this->whereQueryParams(),
76
-                    'limit' => [
77
-                        count($this->model_obj_nodes),
78
-                        $model_objects_to_identify
79
-                    ]
80
-                ]
81
-            );
82
-            $new_item_nodes = [];
83
-
84
-            // Add entity nodes for each of the model objects we fetched.
85
-            foreach ($related_model_objs as $related_model_obj) {
86
-                $entity_node = new ModelObjNode($related_model_obj);
87
-                $this->model_obj_nodes[ $related_model_obj->ID() ] = $entity_node;
88
-                $new_item_nodes[ $related_model_obj->ID() ] = $entity_node;
89
-            }
90
-            $num_identified += count($new_item_nodes);
91
-            if ($num_identified < $model_objects_to_identify) {
92
-                // And lastly do the work.
93
-                $num_identified += $this->visitAlreadyDiscoveredNodes(
94
-                    $new_item_nodes,
95
-                    $model_objects_to_identify - $num_identified
96
-                );
97
-            }
98
-        }
99
-
100
-        if (count($this->model_obj_nodes) >= $this->count && $this->allChildrenComplete()) {
101
-            $this->complete = true;
102
-        }
103
-        return $num_identified;
104
-    }
105
-
106
-    /**
107
-     * Checks if all the identified child nodes are complete or not.
108
-     * @since $VID:$
109
-     * @return bool
110
-     */
111
-    protected function allChildrenComplete()
112
-    {
113
-        foreach ($this->model_obj_nodes as $model_obj_node) {
114
-            if (! $model_obj_node->isComplete()) {
115
-                return false;
116
-            }
117
-        }
118
-        return true;
119
-    }
120
-
121
-    /**
122
-     * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget.
123
-     * @since $VID:$
124
-     * @param ModelObjNode[] $model_obj_nodes
125
-     * @param $work_budget
126
-     * @return int
127
-     */
128
-    protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
129
-    {
130
-        $work_done = 0;
131
-        if (! $model_obj_nodes) {
132
-            return 0;
133
-        }
134
-        foreach ($model_obj_nodes as $model_obj_node) {
135
-            if ($work_done >= $work_budget) {
136
-                break;
137
-            }
138
-            $work_done += $model_obj_node->visit($work_budget - $work_done);
139
-        }
140
-        return $work_done;
141
-    }
142
-
143
-    /**
144
-     * Whether this item has already been initialized
145
-     */
146
-    protected function isDiscovered()
147
-    {
148
-        return $this->count !== null;
149
-    }
150
-
151
-    /**
152
-     * @since $VID:$
153
-     * @return boolean
154
-     */
155
-    public function isComplete()
156
-    {
157
-        if ($this->complete === null) {
158
-            if (count($this->model_obj_nodes) === $this->count) {
159
-                $this->complete = true;
160
-            } else {
161
-                $this->complete = false;
162
-            }
163
-        }
164
-        return $this->complete;
165
-    }
166
-
167
-    /**
168
-     * Discovers how many related model objects exist.
169
-     * @since $VID:$
170
-     * @return mixed|void
171
-     * @throws EE_Error
172
-     * @throws InvalidArgumentException
173
-     * @throws InvalidDataTypeException
174
-     * @throws InvalidInterfaceException
175
-     * @throws ReflectionException
176
-     */
177
-    protected function discover()
178
-    {
179
-        $this->count = $this->related_model->count([$this->whereQueryParams()]);
180
-    }
181
-
182
-    /**
183
-     * @since $VID:$
184
-     * @return array
185
-     * @throws EE_Error
186
-     * @throws InvalidDataTypeException
187
-     * @throws InvalidInterfaceException
188
-     * @throws InvalidArgumentException
189
-     * @throws ReflectionException
190
-     */
191
-    protected function whereQueryParams()
192
-    {
193
-        $where_params =  [
194
-            $this->related_model->get_foreign_key_to(
195
-                $this->main_model_obj->get_model()->get_this_model_name()
196
-            )->get_name() => $this->main_model_obj->ID()
197
-        ];
198
-        try {
199
-            $relation_settings = $this->main_model_obj->get_model()->related_settings_for($this->related_model->get_this_model_name());
200
-        } catch (EE_Error $e) {
201
-            // This will happen for has-and-belongs-to-many relations, when this node's related model is that join table
202
-            // which hasn't been explicitly declared in the main model object's model's relations.
203
-            $relation_settings = null;
204
-        }
205
-        if ($relation_settings instanceof EE_Has_Many_Any_Relation) {
206
-            $where_params[ $this->related_model->get_field_containing_related_model_name()->get_name() ] = $this->main_model_obj->get_model()->get_this_model_name();
207
-        }
208
-        return $where_params;
209
-    }
210
-    /**
211
-     * @since $VID:$
212
-     * @return array
213
-     */
214
-    public function toArray()
215
-    {
216
-        $tree = [
217
-            'count' => $this->count,
218
-            'complete' => $this->isComplete(),
219
-            'objs' => []
220
-        ];
221
-        foreach ($this->model_obj_nodes as $id => $model_obj_node) {
222
-            $tree['objs'][ $id ] = $model_obj_node->toArray();
223
-        }
224
-        return $tree;
225
-    }
226
-
227
-    /**
228
-     * Gets the IDs of all the model objects to delete; indexed first by model object name.
229
-     * @since $VID:$
230
-     * @return array
231
-     */
232
-    public function getIds()
233
-    {
234
-        if (empty($this->model_obj_nodes)) {
235
-            return [];
236
-        }
237
-        $ids = [
238
-            $this->related_model->get_this_model_name() => array_combine(
239
-                array_keys($this->model_obj_nodes),
240
-                array_keys($this->model_obj_nodes)
241
-            )
242
-        ];
243
-        foreach ($this->model_obj_nodes as $model_obj_node) {
244
-            $ids = array_replace_recursive($ids, $model_obj_node->getIds());
245
-        }
246
-        return $ids;
247
-    }
29
+	/**
30
+	 * @var EE_Base_Class
31
+	 */
32
+	protected $main_model_obj;
33
+
34
+	/**
35
+	 * @var int
36
+	 */
37
+	protected $count;
38
+
39
+	/**
40
+	 * @var EEM_Base
41
+	 */
42
+	protected $related_model;
43
+
44
+	/**
45
+	 * @var ModelObjNode[]
46
+	 */
47
+	protected $model_obj_nodes;
48
+
49
+	public function __construct($main_model_obj, $related_model)
50
+	{
51
+		$this->main_model_obj = $main_model_obj;
52
+		$this->related_model = $related_model;
53
+		$this->model_obj_nodes = [];
54
+	}
55
+
56
+
57
+	/**
58
+	 * Here is where most of the work happens. We've counted how many related model objects exist, here we identify
59
+	 * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc.
60
+	 * @since $VID:$
61
+	 * @param int $model_objects_to_identify
62
+	 * @return int
63
+	 * @throws EE_Error
64
+	 * @throws InvalidArgumentException
65
+	 * @throws InvalidDataTypeException
66
+	 * @throws InvalidInterfaceException
67
+	 * @throws ReflectionException
68
+	 */
69
+	protected function work($model_objects_to_identify)
70
+	{
71
+		$num_identified = $this->visitAlreadyDiscoveredNodes($this->model_obj_nodes, $model_objects_to_identify);
72
+		if ($num_identified < $model_objects_to_identify) {
73
+			$related_model_objs = $this->related_model->get_all(
74
+				[
75
+					$this->whereQueryParams(),
76
+					'limit' => [
77
+						count($this->model_obj_nodes),
78
+						$model_objects_to_identify
79
+					]
80
+				]
81
+			);
82
+			$new_item_nodes = [];
83
+
84
+			// Add entity nodes for each of the model objects we fetched.
85
+			foreach ($related_model_objs as $related_model_obj) {
86
+				$entity_node = new ModelObjNode($related_model_obj);
87
+				$this->model_obj_nodes[ $related_model_obj->ID() ] = $entity_node;
88
+				$new_item_nodes[ $related_model_obj->ID() ] = $entity_node;
89
+			}
90
+			$num_identified += count($new_item_nodes);
91
+			if ($num_identified < $model_objects_to_identify) {
92
+				// And lastly do the work.
93
+				$num_identified += $this->visitAlreadyDiscoveredNodes(
94
+					$new_item_nodes,
95
+					$model_objects_to_identify - $num_identified
96
+				);
97
+			}
98
+		}
99
+
100
+		if (count($this->model_obj_nodes) >= $this->count && $this->allChildrenComplete()) {
101
+			$this->complete = true;
102
+		}
103
+		return $num_identified;
104
+	}
105
+
106
+	/**
107
+	 * Checks if all the identified child nodes are complete or not.
108
+	 * @since $VID:$
109
+	 * @return bool
110
+	 */
111
+	protected function allChildrenComplete()
112
+	{
113
+		foreach ($this->model_obj_nodes as $model_obj_node) {
114
+			if (! $model_obj_node->isComplete()) {
115
+				return false;
116
+			}
117
+		}
118
+		return true;
119
+	}
120
+
121
+	/**
122
+	 * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget.
123
+	 * @since $VID:$
124
+	 * @param ModelObjNode[] $model_obj_nodes
125
+	 * @param $work_budget
126
+	 * @return int
127
+	 */
128
+	protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
129
+	{
130
+		$work_done = 0;
131
+		if (! $model_obj_nodes) {
132
+			return 0;
133
+		}
134
+		foreach ($model_obj_nodes as $model_obj_node) {
135
+			if ($work_done >= $work_budget) {
136
+				break;
137
+			}
138
+			$work_done += $model_obj_node->visit($work_budget - $work_done);
139
+		}
140
+		return $work_done;
141
+	}
142
+
143
+	/**
144
+	 * Whether this item has already been initialized
145
+	 */
146
+	protected function isDiscovered()
147
+	{
148
+		return $this->count !== null;
149
+	}
150
+
151
+	/**
152
+	 * @since $VID:$
153
+	 * @return boolean
154
+	 */
155
+	public function isComplete()
156
+	{
157
+		if ($this->complete === null) {
158
+			if (count($this->model_obj_nodes) === $this->count) {
159
+				$this->complete = true;
160
+			} else {
161
+				$this->complete = false;
162
+			}
163
+		}
164
+		return $this->complete;
165
+	}
166
+
167
+	/**
168
+	 * Discovers how many related model objects exist.
169
+	 * @since $VID:$
170
+	 * @return mixed|void
171
+	 * @throws EE_Error
172
+	 * @throws InvalidArgumentException
173
+	 * @throws InvalidDataTypeException
174
+	 * @throws InvalidInterfaceException
175
+	 * @throws ReflectionException
176
+	 */
177
+	protected function discover()
178
+	{
179
+		$this->count = $this->related_model->count([$this->whereQueryParams()]);
180
+	}
181
+
182
+	/**
183
+	 * @since $VID:$
184
+	 * @return array
185
+	 * @throws EE_Error
186
+	 * @throws InvalidDataTypeException
187
+	 * @throws InvalidInterfaceException
188
+	 * @throws InvalidArgumentException
189
+	 * @throws ReflectionException
190
+	 */
191
+	protected function whereQueryParams()
192
+	{
193
+		$where_params =  [
194
+			$this->related_model->get_foreign_key_to(
195
+				$this->main_model_obj->get_model()->get_this_model_name()
196
+			)->get_name() => $this->main_model_obj->ID()
197
+		];
198
+		try {
199
+			$relation_settings = $this->main_model_obj->get_model()->related_settings_for($this->related_model->get_this_model_name());
200
+		} catch (EE_Error $e) {
201
+			// This will happen for has-and-belongs-to-many relations, when this node's related model is that join table
202
+			// which hasn't been explicitly declared in the main model object's model's relations.
203
+			$relation_settings = null;
204
+		}
205
+		if ($relation_settings instanceof EE_Has_Many_Any_Relation) {
206
+			$where_params[ $this->related_model->get_field_containing_related_model_name()->get_name() ] = $this->main_model_obj->get_model()->get_this_model_name();
207
+		}
208
+		return $where_params;
209
+	}
210
+	/**
211
+	 * @since $VID:$
212
+	 * @return array
213
+	 */
214
+	public function toArray()
215
+	{
216
+		$tree = [
217
+			'count' => $this->count,
218
+			'complete' => $this->isComplete(),
219
+			'objs' => []
220
+		];
221
+		foreach ($this->model_obj_nodes as $id => $model_obj_node) {
222
+			$tree['objs'][ $id ] = $model_obj_node->toArray();
223
+		}
224
+		return $tree;
225
+	}
226
+
227
+	/**
228
+	 * Gets the IDs of all the model objects to delete; indexed first by model object name.
229
+	 * @since $VID:$
230
+	 * @return array
231
+	 */
232
+	public function getIds()
233
+	{
234
+		if (empty($this->model_obj_nodes)) {
235
+			return [];
236
+		}
237
+		$ids = [
238
+			$this->related_model->get_this_model_name() => array_combine(
239
+				array_keys($this->model_obj_nodes),
240
+				array_keys($this->model_obj_nodes)
241
+			)
242
+		];
243
+		foreach ($this->model_obj_nodes as $model_obj_node) {
244
+			$ids = array_replace_recursive($ids, $model_obj_node->getIds());
245
+		}
246
+		return $ids;
247
+	}
248 248
 }
249 249
 // End of file RelationNode.php
250 250
 // Location: EventEspresso\core\services\orm\tree_traversal/RelationNode.php
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -84,8 +84,8 @@  discard block
 block discarded – undo
84 84
             // Add entity nodes for each of the model objects we fetched.
85 85
             foreach ($related_model_objs as $related_model_obj) {
86 86
                 $entity_node = new ModelObjNode($related_model_obj);
87
-                $this->model_obj_nodes[ $related_model_obj->ID() ] = $entity_node;
88
-                $new_item_nodes[ $related_model_obj->ID() ] = $entity_node;
87
+                $this->model_obj_nodes[$related_model_obj->ID()] = $entity_node;
88
+                $new_item_nodes[$related_model_obj->ID()] = $entity_node;
89 89
             }
90 90
             $num_identified += count($new_item_nodes);
91 91
             if ($num_identified < $model_objects_to_identify) {
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
     protected function allChildrenComplete()
112 112
     {
113 113
         foreach ($this->model_obj_nodes as $model_obj_node) {
114
-            if (! $model_obj_node->isComplete()) {
114
+            if ( ! $model_obj_node->isComplete()) {
115 115
                 return false;
116 116
             }
117 117
         }
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
     protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
129 129
     {
130 130
         $work_done = 0;
131
-        if (! $model_obj_nodes) {
131
+        if ( ! $model_obj_nodes) {
132 132
             return 0;
133 133
         }
134 134
         foreach ($model_obj_nodes as $model_obj_node) {
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
      */
191 191
     protected function whereQueryParams()
192 192
     {
193
-        $where_params =  [
193
+        $where_params = [
194 194
             $this->related_model->get_foreign_key_to(
195 195
                 $this->main_model_obj->get_model()->get_this_model_name()
196 196
             )->get_name() => $this->main_model_obj->ID()
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
             $relation_settings = null;
204 204
         }
205 205
         if ($relation_settings instanceof EE_Has_Many_Any_Relation) {
206
-            $where_params[ $this->related_model->get_field_containing_related_model_name()->get_name() ] = $this->main_model_obj->get_model()->get_this_model_name();
206
+            $where_params[$this->related_model->get_field_containing_related_model_name()->get_name()] = $this->main_model_obj->get_model()->get_this_model_name();
207 207
         }
208 208
         return $where_params;
209 209
     }
@@ -219,7 +219,7 @@  discard block
 block discarded – undo
219 219
             'objs' => []
220 220
         ];
221 221
         foreach ($this->model_obj_nodes as $id => $model_obj_node) {
222
-            $tree['objs'][ $id ] = $model_obj_node->toArray();
222
+            $tree['objs'][$id] = $model_obj_node->toArray();
223 223
         }
224 224
         return $tree;
225 225
     }
Please login to merge, or discard this patch.