Completed
Branch CASC/dont-traverse-models (6dd8d8)
by
unknown
19:31 queued 10:56
created
core/libraries/batch/JobHandlers/PreviewEventDeletion.php 2 patches
Doc Comments   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -97,7 +97,7 @@
 block discarded – undo
97 97
     /**
98 98
      * @since $VID:$
99 99
      * @param EE_Base_Class[] $model_objs
100
-     * @param array $dont_traverse_models
100
+     * @param string[] $dont_traverse_models
101 101
      */
102 102
     protected function createModelObjNodes($model_objs, array $dont_traverse_models = [])
103 103
     {
Please login to merge, or discard this patch.
Indentation   +154 added lines, -154 removed lines patch added patch discarded remove patch
@@ -28,164 +28,164 @@
 block discarded – undo
28 28
 class PreviewEventDeletion extends JobHandler
29 29
 {
30 30
 
31
-    /**
32
-     * @var NodeGroupDao
33
-     */
34
-    protected $model_obj_node_group_persister;
35
-    public function __construct(NodeGroupDao $model_obj_node_group_persister)
36
-    {
37
-        $this->model_obj_node_group_persister = $model_obj_node_group_persister;
38
-    }
31
+	/**
32
+	 * @var NodeGroupDao
33
+	 */
34
+	protected $model_obj_node_group_persister;
35
+	public function __construct(NodeGroupDao $model_obj_node_group_persister)
36
+	{
37
+		$this->model_obj_node_group_persister = $model_obj_node_group_persister;
38
+	}
39 39
 
40
-    // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
41
-    /**
42
-     *
43
-     * @param JobParameters $job_parameters
44
-     * @throws BatchRequestException
45
-     * @return JobStepResponse
46
-     */
47
-    public function create_job(JobParameters $job_parameters)
48
-    {
49
-        // Set the "root" model objects we will want to delete (record their ID and model)
50
-        $event_ids = $job_parameters->request_datum('EVT_IDs', array());
51
-        // Find all the root nodes to delete (this isn't just events, because there's other data, like related tickets,
52
-        // prices, message templates, etc, whose model definition doesn't make them dependent on events. But,
53
-        // we have no UI to access them independent of events, so they may as well get deleted too.)
54
-        $model_objects_to_delete = [];
55
-        $roots = [];
56
-        foreach ($event_ids as $event_id) {
57
-            $roots[] = new ModelObjNode(
58
-                $event_id,
59
-                EEM_Event::instance()
60
-            );
61
-            // Also, we want to delete their related, non-global, tickets, prices and message templates
62
-            $related_non_global_tickets = EEM_Ticket::instance()->get_all_deleted_and_undeleted(
63
-                [
64
-                    [
65
-                        'TKT_is_default' => false,
66
-                        'Datetime.EVT_ID' => $event_id
67
-                    ]
68
-                ]
69
-            );
70
-            $related_non_global_prices = EEM_Price::instance()->get_all_deleted_and_undeleted(
71
-                [
72
-                    [
73
-                        'PRC_is_default' => false,
74
-                        'Ticket.Datetime.EVT_ID' => $event_id
75
-                    ]
76
-                ]
77
-            );
78
-            $roots = array_merge(
79
-                $roots,
80
-                // Dont have ticket nodes also traverse registrations, its unnecessary because
81
-                // registrations also depend on events so they will already get traversed.
82
-                $this->createModelObjNodes($related_non_global_tickets, ['Registration']),
83
-                $this->createModelObjNodes($related_non_global_prices)
84
-            );
85
-        }
86
-        $job_parameters->add_extra_data('roots', $roots);
87
-        // Set an estimate of how long this will take (we're discovering as we go, so it seems impossible to give
88
-        // an accurate count.)
89
-        $estimated_work_per_model_obj = 100;
90
-        $job_parameters->set_job_size(count($roots) * $estimated_work_per_model_obj);
91
-        return new JobStepResponse(
92
-            $job_parameters,
93
-            esc_html__('Generating preview of data to be deleted...', 'event_espresso')
94
-        );
95
-    }
40
+	// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
41
+	/**
42
+	 *
43
+	 * @param JobParameters $job_parameters
44
+	 * @throws BatchRequestException
45
+	 * @return JobStepResponse
46
+	 */
47
+	public function create_job(JobParameters $job_parameters)
48
+	{
49
+		// Set the "root" model objects we will want to delete (record their ID and model)
50
+		$event_ids = $job_parameters->request_datum('EVT_IDs', array());
51
+		// Find all the root nodes to delete (this isn't just events, because there's other data, like related tickets,
52
+		// prices, message templates, etc, whose model definition doesn't make them dependent on events. But,
53
+		// we have no UI to access them independent of events, so they may as well get deleted too.)
54
+		$model_objects_to_delete = [];
55
+		$roots = [];
56
+		foreach ($event_ids as $event_id) {
57
+			$roots[] = new ModelObjNode(
58
+				$event_id,
59
+				EEM_Event::instance()
60
+			);
61
+			// Also, we want to delete their related, non-global, tickets, prices and message templates
62
+			$related_non_global_tickets = EEM_Ticket::instance()->get_all_deleted_and_undeleted(
63
+				[
64
+					[
65
+						'TKT_is_default' => false,
66
+						'Datetime.EVT_ID' => $event_id
67
+					]
68
+				]
69
+			);
70
+			$related_non_global_prices = EEM_Price::instance()->get_all_deleted_and_undeleted(
71
+				[
72
+					[
73
+						'PRC_is_default' => false,
74
+						'Ticket.Datetime.EVT_ID' => $event_id
75
+					]
76
+				]
77
+			);
78
+			$roots = array_merge(
79
+				$roots,
80
+				// Dont have ticket nodes also traverse registrations, its unnecessary because
81
+				// registrations also depend on events so they will already get traversed.
82
+				$this->createModelObjNodes($related_non_global_tickets, ['Registration']),
83
+				$this->createModelObjNodes($related_non_global_prices)
84
+			);
85
+		}
86
+		$job_parameters->add_extra_data('roots', $roots);
87
+		// Set an estimate of how long this will take (we're discovering as we go, so it seems impossible to give
88
+		// an accurate count.)
89
+		$estimated_work_per_model_obj = 100;
90
+		$job_parameters->set_job_size(count($roots) * $estimated_work_per_model_obj);
91
+		return new JobStepResponse(
92
+			$job_parameters,
93
+			esc_html__('Generating preview of data to be deleted...', 'event_espresso')
94
+		);
95
+	}
96 96
 
97
-    /**
98
-     * @since $VID:$
99
-     * @param EE_Base_Class[] $model_objs
100
-     * @param array $dont_traverse_models
101
-     */
102
-    protected function createModelObjNodes($model_objs, array $dont_traverse_models = [])
103
-    {
104
-        $nodes = [];
105
-        foreach ($model_objs as $model_obj) {
106
-            $nodes[] = new ModelObjNode(
107
-                $model_obj->ID(),
108
-                $model_obj->get_model(),
109
-                $dont_traverse_models
110
-            );
111
-        }
112
-        return $nodes;
113
-    }
97
+	/**
98
+	 * @since $VID:$
99
+	 * @param EE_Base_Class[] $model_objs
100
+	 * @param array $dont_traverse_models
101
+	 */
102
+	protected function createModelObjNodes($model_objs, array $dont_traverse_models = [])
103
+	{
104
+		$nodes = [];
105
+		foreach ($model_objs as $model_obj) {
106
+			$nodes[] = new ModelObjNode(
107
+				$model_obj->ID(),
108
+				$model_obj->get_model(),
109
+				$dont_traverse_models
110
+			);
111
+		}
112
+		return $nodes;
113
+	}
114 114
 
115
-    /**
116
-     * Performs another step of the job
117
-     * @param JobParameters $job_parameters
118
-     * @param int $batch_size
119
-     * @return JobStepResponse
120
-     * @throws BatchRequestException
121
-     */
122
-    public function continue_job(JobParameters $job_parameters, $batch_size = 50)
123
-    {
124
-        // Serializing and unserializing is what really makes this drag on (eg on localhost, the ajax requests took
125
-        // about 4 seconds when the batch size was 250, but 3 seconds when the batch size was 50. So like
126
-        // 50% of the request is just serializing and unserializing.) So, make the batches much bigger.
127
-        $batch_size *= 3;
128
-        $units_processed = 0;
129
-        foreach ($job_parameters->extra_datum('roots', array()) as $root_node) {
130
-            if ($units_processed >= $batch_size) {
131
-                break;
132
-            }
133
-            if (! $root_node instanceof ModelObjNode) {
134
-                throw new InvalidClassException('ModelObjNode');
135
-            }
136
-            if ($root_node->isComplete()) {
137
-                continue;
138
-            }
139
-            $units_processed += $root_node->visit($batch_size - $units_processed);
140
-        }
141
-        $job_parameters->mark_processed($units_processed);
142
-        // If the most-recently processed root node is complete, we must be all done because we're doing them
143
-        // sequentially.
144
-        if (isset($root_node) && $root_node instanceof ModelObjNode && $root_node->isComplete()) {
145
-            $job_parameters->set_status(JobParameters::status_complete);
146
-            // Show a full progress bar.
147
-            $job_parameters->set_units_processed($job_parameters->job_size());
148
-            $deletion_job_code = $job_parameters->request_datum('deletion_job_code');
149
-            $this->model_obj_node_group_persister->persistModelObjNodesGroup(
150
-                $job_parameters->extra_datum('roots'),
151
-                $deletion_job_code
152
-            );
153
-            return new JobStepResponse(
154
-                $job_parameters,
155
-                esc_html__('Finished identifying items for deletion.', 'event_espresso'),
156
-                [
157
-                    'deletion_job_code' => $deletion_job_code
158
-                ]
159
-            );
160
-        } else {
161
-            // Because the job size was a guess, it may have likely been provden wrong. We don't want to show more work
162
-            // done than we originally said there would be. So adjust the estimate.
163
-            if (($job_parameters->units_processed() / $job_parameters->job_size()) > .8) {
164
-                $job_parameters->set_job_size($job_parameters->job_size() * 2);
165
-            }
166
-            return new JobStepResponse(
167
-                $job_parameters,
168
-                sprintf(
169
-                    esc_html__('Identified %d items for deletion.', 'event_espresso'),
170
-                    $units_processed
171
-                )
172
-            );
173
-        }
174
-    }
115
+	/**
116
+	 * Performs another step of the job
117
+	 * @param JobParameters $job_parameters
118
+	 * @param int $batch_size
119
+	 * @return JobStepResponse
120
+	 * @throws BatchRequestException
121
+	 */
122
+	public function continue_job(JobParameters $job_parameters, $batch_size = 50)
123
+	{
124
+		// Serializing and unserializing is what really makes this drag on (eg on localhost, the ajax requests took
125
+		// about 4 seconds when the batch size was 250, but 3 seconds when the batch size was 50. So like
126
+		// 50% of the request is just serializing and unserializing.) So, make the batches much bigger.
127
+		$batch_size *= 3;
128
+		$units_processed = 0;
129
+		foreach ($job_parameters->extra_datum('roots', array()) as $root_node) {
130
+			if ($units_processed >= $batch_size) {
131
+				break;
132
+			}
133
+			if (! $root_node instanceof ModelObjNode) {
134
+				throw new InvalidClassException('ModelObjNode');
135
+			}
136
+			if ($root_node->isComplete()) {
137
+				continue;
138
+			}
139
+			$units_processed += $root_node->visit($batch_size - $units_processed);
140
+		}
141
+		$job_parameters->mark_processed($units_processed);
142
+		// If the most-recently processed root node is complete, we must be all done because we're doing them
143
+		// sequentially.
144
+		if (isset($root_node) && $root_node instanceof ModelObjNode && $root_node->isComplete()) {
145
+			$job_parameters->set_status(JobParameters::status_complete);
146
+			// Show a full progress bar.
147
+			$job_parameters->set_units_processed($job_parameters->job_size());
148
+			$deletion_job_code = $job_parameters->request_datum('deletion_job_code');
149
+			$this->model_obj_node_group_persister->persistModelObjNodesGroup(
150
+				$job_parameters->extra_datum('roots'),
151
+				$deletion_job_code
152
+			);
153
+			return new JobStepResponse(
154
+				$job_parameters,
155
+				esc_html__('Finished identifying items for deletion.', 'event_espresso'),
156
+				[
157
+					'deletion_job_code' => $deletion_job_code
158
+				]
159
+			);
160
+		} else {
161
+			// Because the job size was a guess, it may have likely been provden wrong. We don't want to show more work
162
+			// done than we originally said there would be. So adjust the estimate.
163
+			if (($job_parameters->units_processed() / $job_parameters->job_size()) > .8) {
164
+				$job_parameters->set_job_size($job_parameters->job_size() * 2);
165
+			}
166
+			return new JobStepResponse(
167
+				$job_parameters,
168
+				sprintf(
169
+					esc_html__('Identified %d items for deletion.', 'event_espresso'),
170
+					$units_processed
171
+				)
172
+			);
173
+		}
174
+	}
175 175
 
176
-    /**
177
-     * Performs any clean-up logic when we know the job is completed
178
-     * @param JobParameters $job_parameters
179
-     * @return JobStepResponse
180
-     */
181
-    public function cleanup_job(JobParameters $job_parameters)
182
-    {
183
-        // Nothing much to do. We can't delete the option with the built tree because we may need it in a moment for the deletion
184
-        return new JobStepResponse(
185
-            $job_parameters,
186
-            esc_html__('All done', 'event_espresso')
187
-        );
188
-    }
176
+	/**
177
+	 * Performs any clean-up logic when we know the job is completed
178
+	 * @param JobParameters $job_parameters
179
+	 * @return JobStepResponse
180
+	 */
181
+	public function cleanup_job(JobParameters $job_parameters)
182
+	{
183
+		// Nothing much to do. We can't delete the option with the built tree because we may need it in a moment for the deletion
184
+		return new JobStepResponse(
185
+			$job_parameters,
186
+			esc_html__('All done', 'event_espresso')
187
+		);
188
+	}
189 189
 }
190 190
 // End of file EventDeletion.php
191 191
 // Location: EventEspressoBatchRequest\JobHandlers/EventDeletion.php
Please login to merge, or discard this patch.
core/services/orm/tree_traversal/ModelObjNode.php 1 patch
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -22,203 +22,203 @@
 block discarded – undo
22 22
  */
23 23
 class ModelObjNode extends BaseNode
24 24
 {
25
-    /**
26
-     * @var int|string
27
-     */
28
-    protected $id;
25
+	/**
26
+	 * @var int|string
27
+	 */
28
+	protected $id;
29 29
 
30
-    /**
31
-     * @var EEM_Base
32
-     */
33
-    protected $model;
30
+	/**
31
+	 * @var EEM_Base
32
+	 */
33
+	protected $model;
34 34
 
35
-    /**
36
-     * @var RelationNode[]
37
-     */
38
-    protected $nodes;
35
+	/**
36
+	 * @var RelationNode[]
37
+	 */
38
+	protected $nodes;
39 39
 
40
-    /**
41
-     * We don't pass the model objects because this needs to serialize to something tiny for effiency.
42
-     * @param $model_obj_id
43
-     * @param EEM_Base $model
44
-     */
45
-    public function __construct($model_obj_id, EEM_Base $model, array $dont_traverse_models = []])
46
-    {
47
-        $this->id = $model_obj_id;
48
-        $this->model = $model;
49
-        $this->dont_traverse_models = $dont_traverse_models;
50
-    }
40
+	/**
41
+	 * We don't pass the model objects because this needs to serialize to something tiny for effiency.
42
+	 * @param $model_obj_id
43
+	 * @param EEM_Base $model
44
+	 */
45
+	public function __construct($model_obj_id, EEM_Base $model, array $dont_traverse_models = []])
46
+	{
47
+		$this->id = $model_obj_id;
48
+		$this->model = $model;
49
+		$this->dont_traverse_models = $dont_traverse_models;
50
+	}
51 51
 
52
-    /**
53
-     * Creates a relation node for each relation of this model's relations.
54
-     * Does NOT call `discover` on them yet though.
55
-     * @since $VID:$
56
-     * @throws \EE_Error
57
-     * @throws InvalidDataTypeException
58
-     * @throws InvalidInterfaceException
59
-     * @throws InvalidArgumentException
60
-     * @throws ReflectionException
61
-     */
62
-    protected function discover()
63
-    {
64
-        $this->nodes = [];
65
-        foreach ($this->model->relation_settings() as $relationName => $relation) {
66
-            // Make sure this isn't one of the models we were told to not traverse into.
67
-            if (in_array($relationName, $this->dont_traverse_models)) {
68
-                continue;
69
-            }
70
-            if ($relation instanceof EE_Has_Many_Relation) {
71
-                $this->nodes[ $relationName ] = new RelationNode(
72
-                    $this->id,
73
-                    $this->model,
74
-                    $relation->get_other_model(),
75
-                    $this->dont_traverse_models
76
-                );
77
-            } elseif ($relation instanceof EE_HABTM_Relation &&
78
-                ! in_array(
79
-                    $relation->get_join_model()->get_this_model_name(),
80
-                    $this->dont_traverse_models
81
-                )) {
82
-                $this->nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode(
83
-                    $this->id,
84
-                    $this->model,
85
-                    $relation->get_join_model(),
86
-                    $this->dont_traverse_models
87
-                );
88
-            }
89
-        }
90
-        ksort($this->nodes);
91
-    }
52
+	/**
53
+	 * Creates a relation node for each relation of this model's relations.
54
+	 * Does NOT call `discover` on them yet though.
55
+	 * @since $VID:$
56
+	 * @throws \EE_Error
57
+	 * @throws InvalidDataTypeException
58
+	 * @throws InvalidInterfaceException
59
+	 * @throws InvalidArgumentException
60
+	 * @throws ReflectionException
61
+	 */
62
+	protected function discover()
63
+	{
64
+		$this->nodes = [];
65
+		foreach ($this->model->relation_settings() as $relationName => $relation) {
66
+			// Make sure this isn't one of the models we were told to not traverse into.
67
+			if (in_array($relationName, $this->dont_traverse_models)) {
68
+				continue;
69
+			}
70
+			if ($relation instanceof EE_Has_Many_Relation) {
71
+				$this->nodes[ $relationName ] = new RelationNode(
72
+					$this->id,
73
+					$this->model,
74
+					$relation->get_other_model(),
75
+					$this->dont_traverse_models
76
+				);
77
+			} elseif ($relation instanceof EE_HABTM_Relation &&
78
+				! in_array(
79
+					$relation->get_join_model()->get_this_model_name(),
80
+					$this->dont_traverse_models
81
+				)) {
82
+				$this->nodes[ $relation->get_join_model()->get_this_model_name() ] = new RelationNode(
83
+					$this->id,
84
+					$this->model,
85
+					$relation->get_join_model(),
86
+					$this->dont_traverse_models
87
+				);
88
+			}
89
+		}
90
+		ksort($this->nodes);
91
+	}
92 92
 
93 93
 
94
-    /**
95
-     * Whether this item has already been initialized
96
-     */
97
-    protected function isDiscovered()
98
-    {
99
-        return $this->nodes !== null && is_array($this->nodes);
100
-    }
94
+	/**
95
+	 * Whether this item has already been initialized
96
+	 */
97
+	protected function isDiscovered()
98
+	{
99
+		return $this->nodes !== null && is_array($this->nodes);
100
+	}
101 101
 
102
-    /**
103
-     * @since $VID:$
104
-     * @return boolean
105
-     */
106
-    public function isComplete()
107
-    {
108
-        if ($this->complete === null) {
109
-            $this->complete = false;
110
-        }
111
-        return $this->complete;
112
-    }
102
+	/**
103
+	 * @since $VID:$
104
+	 * @return boolean
105
+	 */
106
+	public function isComplete()
107
+	{
108
+		if ($this->complete === null) {
109
+			$this->complete = false;
110
+		}
111
+		return $this->complete;
112
+	}
113 113
 
114
-    /**
115
-     * Triggers working on each child relation node that has work to do.
116
-     * @since $VID:$
117
-     * @param $model_objects_to_identify
118
-     * @return int units of work done
119
-     */
120
-    protected function work($model_objects_to_identify)
121
-    {
122
-        $num_identified = 0;
123
-        // Begin assuming we'll finish all the work on this node and its children...
124
-        $this->complete = true;
125
-        foreach ($this->nodes as $model_name => $relation_node) {
126
-            $num_identified += $relation_node->visit($model_objects_to_identify - $num_identified);
127
-            // To save on space when serializing, only bother keeping a record of relation nodes that actually found
128
-            // related model objects.
129
-            if ($relation_node->isComplete() && $relation_node->countSubNodes() === 0) {
130
-                unset($this->nodes[ $model_name ]);
131
-            }
132
-            if ($num_identified >= $model_objects_to_identify) {
133
-                // ...but admit we're wrong if the work exceeded the budget.
134
-                $this->complete = false;
135
-                break;
136
-            }
137
-        }
138
-        return $num_identified;
139
-    }
114
+	/**
115
+	 * Triggers working on each child relation node that has work to do.
116
+	 * @since $VID:$
117
+	 * @param $model_objects_to_identify
118
+	 * @return int units of work done
119
+	 */
120
+	protected function work($model_objects_to_identify)
121
+	{
122
+		$num_identified = 0;
123
+		// Begin assuming we'll finish all the work on this node and its children...
124
+		$this->complete = true;
125
+		foreach ($this->nodes as $model_name => $relation_node) {
126
+			$num_identified += $relation_node->visit($model_objects_to_identify - $num_identified);
127
+			// To save on space when serializing, only bother keeping a record of relation nodes that actually found
128
+			// related model objects.
129
+			if ($relation_node->isComplete() && $relation_node->countSubNodes() === 0) {
130
+				unset($this->nodes[ $model_name ]);
131
+			}
132
+			if ($num_identified >= $model_objects_to_identify) {
133
+				// ...but admit we're wrong if the work exceeded the budget.
134
+				$this->complete = false;
135
+				break;
136
+			}
137
+		}
138
+		return $num_identified;
139
+	}
140 140
 
141
-    /**
142
-     * @since $VID:$
143
-     * @return array
144
-     * @throws \EE_Error
145
-     * @throws InvalidDataTypeException
146
-     * @throws InvalidInterfaceException
147
-     * @throws InvalidArgumentException
148
-     * @throws ReflectionException
149
-     */
150
-    public function toArray()
151
-    {
152
-        $tree = [
153
-            'id' => $this->id,
154
-            'complete' => $this->isComplete(),
155
-            'rels' => []
156
-        ];
157
-        if ($this->nodes === null) {
158
-            $tree['rels'] = null;
159
-        } else {
160
-            foreach ($this->nodes as $relation_name => $relation_node) {
161
-                $tree['rels'][ $relation_name ] = $relation_node->toArray();
162
-            }
163
-        }
164
-        return $tree;
165
-    }
141
+	/**
142
+	 * @since $VID:$
143
+	 * @return array
144
+	 * @throws \EE_Error
145
+	 * @throws InvalidDataTypeException
146
+	 * @throws InvalidInterfaceException
147
+	 * @throws InvalidArgumentException
148
+	 * @throws ReflectionException
149
+	 */
150
+	public function toArray()
151
+	{
152
+		$tree = [
153
+			'id' => $this->id,
154
+			'complete' => $this->isComplete(),
155
+			'rels' => []
156
+		];
157
+		if ($this->nodes === null) {
158
+			$tree['rels'] = null;
159
+		} else {
160
+			foreach ($this->nodes as $relation_name => $relation_node) {
161
+				$tree['rels'][ $relation_name ] = $relation_node->toArray();
162
+			}
163
+		}
164
+		return $tree;
165
+	}
166 166
 
167
-    /**
168
-     * @since $VID:$
169
-     * @return array|mixed
170
-     * @throws InvalidArgumentException
171
-     * @throws InvalidDataTypeException
172
-     * @throws InvalidInterfaceException
173
-     * @throws ReflectionException
174
-     * @throws \EE_Error
175
-     */
176
-    public function getIds()
177
-    {
178
-        $ids = [
179
-            $this->model->get_this_model_name() => [
180
-                $this->id => $this->id
181
-            ]
182
-        ];
183
-        if ($this->nodes && is_array($this->nodes)) {
184
-            foreach ($this->nodes as $relation_node) {
185
-                $ids = array_replace_recursive($ids, $relation_node->getIds());
186
-            }
187
-        }
188
-        return $ids;
189
-    }
167
+	/**
168
+	 * @since $VID:$
169
+	 * @return array|mixed
170
+	 * @throws InvalidArgumentException
171
+	 * @throws InvalidDataTypeException
172
+	 * @throws InvalidInterfaceException
173
+	 * @throws ReflectionException
174
+	 * @throws \EE_Error
175
+	 */
176
+	public function getIds()
177
+	{
178
+		$ids = [
179
+			$this->model->get_this_model_name() => [
180
+				$this->id => $this->id
181
+			]
182
+		];
183
+		if ($this->nodes && is_array($this->nodes)) {
184
+			foreach ($this->nodes as $relation_node) {
185
+				$ids = array_replace_recursive($ids, $relation_node->getIds());
186
+			}
187
+		}
188
+		return $ids;
189
+	}
190 190
 
191
-    /**
192
-     * Don't serialize the models. Just record their names on some dynamic properties.
193
-     * @since $VID:$
194
-     */
195
-    public function __sleep()
196
-    {
197
-        $this->m = $this->model->get_this_model_name();
198
-        return array_merge(
199
-            [
200
-                'm',
201
-                'id',
202
-                'nodes',
203
-            ],
204
-            parent::__sleep()
205
-        );
206
-    }
191
+	/**
192
+	 * Don't serialize the models. Just record their names on some dynamic properties.
193
+	 * @since $VID:$
194
+	 */
195
+	public function __sleep()
196
+	{
197
+		$this->m = $this->model->get_this_model_name();
198
+		return array_merge(
199
+			[
200
+				'm',
201
+				'id',
202
+				'nodes',
203
+			],
204
+			parent::__sleep()
205
+		);
206
+	}
207 207
 
208
-    /**
209
-     * Use the dynamic properties to instantiate the models we use.
210
-     * @since $VID:$
211
-     * @throws EE_Error
212
-     * @throws InvalidArgumentException
213
-     * @throws InvalidDataTypeException
214
-     * @throws InvalidInterfaceException
215
-     * @throws ReflectionException
216
-     */
217
-    public function __wakeup()
218
-    {
219
-        $this->model = EE_Registry::instance()->load_model($this->m);
220
-        parent::__wakeup();
221
-    }
208
+	/**
209
+	 * Use the dynamic properties to instantiate the models we use.
210
+	 * @since $VID:$
211
+	 * @throws EE_Error
212
+	 * @throws InvalidArgumentException
213
+	 * @throws InvalidDataTypeException
214
+	 * @throws InvalidInterfaceException
215
+	 * @throws ReflectionException
216
+	 */
217
+	public function __wakeup()
218
+	{
219
+		$this->model = EE_Registry::instance()->load_model($this->m);
220
+		parent::__wakeup();
221
+	}
222 222
 }
223 223
 // End of file Visitor.php
224 224
 // Location: EventEspresso\core\services\orm\tree_traversal/Visitor.php
Please login to merge, or discard this patch.
core/services/orm/tree_traversal/RelationNode.php 1 patch
Indentation   +253 added lines, -253 removed lines patch added patch discarded remove patch
@@ -28,278 +28,278 @@
 block discarded – undo
28 28
 class RelationNode extends BaseNode
29 29
 {
30 30
 
31
-    /**
32
-     * @var string|int
33
-     */
34
-    protected $id;
31
+	/**
32
+	 * @var string|int
33
+	 */
34
+	protected $id;
35 35
 
36
-    /**
37
-     * @var EEM_Base
38
-     */
39
-    protected $main_model;
36
+	/**
37
+	 * @var EEM_Base
38
+	 */
39
+	protected $main_model;
40 40
 
41
-    /**
42
-     * @var int
43
-     */
44
-    protected $count;
41
+	/**
42
+	 * @var int
43
+	 */
44
+	protected $count;
45 45
 
46
-    /**
47
-     * @var EEM_Base
48
-     */
49
-    protected $related_model;
46
+	/**
47
+	 * @var EEM_Base
48
+	 */
49
+	protected $related_model;
50 50
 
51
-    /**
52
-     * @var ModelObjNode[]
53
-     */
54
-    protected $nodes;
51
+	/**
52
+	 * @var ModelObjNode[]
53
+	 */
54
+	protected $nodes;
55 55
 
56
-    public function __construct($main_model_obj_id, EEM_Base $main_model, EEM_Base $related_model, array $dont_traverse_models = [])
57
-    {
58
-        $this->id = $main_model_obj_id;
59
-        $this->main_model = $main_model;
60
-        $this->related_model = $related_model;
61
-        $this->nodes = [];
62
-        $this->dont_traverse_models = $dont_traverse_models;
63
-    }
56
+	public function __construct($main_model_obj_id, EEM_Base $main_model, EEM_Base $related_model, array $dont_traverse_models = [])
57
+	{
58
+		$this->id = $main_model_obj_id;
59
+		$this->main_model = $main_model;
60
+		$this->related_model = $related_model;
61
+		$this->nodes = [];
62
+		$this->dont_traverse_models = $dont_traverse_models;
63
+	}
64 64
 
65 65
 
66
-    /**
67
-     * Here is where most of the work happens. We've counted how many related model objects exist, here we identify
68
-     * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc.
69
-     * @since $VID:$
70
-     * @param int $model_objects_to_identify
71
-     * @return int
72
-     * @throws EE_Error
73
-     * @throws InvalidArgumentException
74
-     * @throws InvalidDataTypeException
75
-     * @throws InvalidInterfaceException
76
-     * @throws ReflectionException
77
-     */
78
-    protected function work($model_objects_to_identify)
79
-    {
80
-        $num_identified = $this->visitAlreadyDiscoveredNodes($this->nodes, $model_objects_to_identify);
81
-        if ($num_identified < $model_objects_to_identify) {
82
-            $related_model_objs = $this->related_model->get_all(
83
-                [
84
-                    $this->whereQueryParams(),
85
-                    'limit' => [
86
-                        count($this->nodes),
87
-                        $model_objects_to_identify - $num_identified
88
-                    ]
89
-                ]
90
-            );
91
-            $new_item_nodes = [];
66
+	/**
67
+	 * Here is where most of the work happens. We've counted how many related model objects exist, here we identify
68
+	 * them (ie, learn their IDs). But its recursive, so we'll also find their related dependent model objects etc.
69
+	 * @since $VID:$
70
+	 * @param int $model_objects_to_identify
71
+	 * @return int
72
+	 * @throws EE_Error
73
+	 * @throws InvalidArgumentException
74
+	 * @throws InvalidDataTypeException
75
+	 * @throws InvalidInterfaceException
76
+	 * @throws ReflectionException
77
+	 */
78
+	protected function work($model_objects_to_identify)
79
+	{
80
+		$num_identified = $this->visitAlreadyDiscoveredNodes($this->nodes, $model_objects_to_identify);
81
+		if ($num_identified < $model_objects_to_identify) {
82
+			$related_model_objs = $this->related_model->get_all(
83
+				[
84
+					$this->whereQueryParams(),
85
+					'limit' => [
86
+						count($this->nodes),
87
+						$model_objects_to_identify - $num_identified
88
+					]
89
+				]
90
+			);
91
+			$new_item_nodes = [];
92 92
 
93
-            // Add entity nodes for each of the model objects we fetched.
94
-            foreach ($related_model_objs as $related_model_obj) {
95
-                $entity_node = new ModelObjNode($related_model_obj->ID(), $related_model_obj->get_model(), $this->dont_traverse_models);
96
-                $this->nodes[ $related_model_obj->ID() ] = $entity_node;
97
-                $new_item_nodes[ $related_model_obj->ID() ] = $entity_node;
98
-            }
99
-            $num_identified += count($new_item_nodes);
100
-            if ($num_identified < $model_objects_to_identify) {
101
-                // And lastly do the work.
102
-                $num_identified += $this->visitAlreadyDiscoveredNodes(
103
-                    $new_item_nodes,
104
-                    $model_objects_to_identify - $num_identified
105
-                );
106
-            }
107
-        }
93
+			// Add entity nodes for each of the model objects we fetched.
94
+			foreach ($related_model_objs as $related_model_obj) {
95
+				$entity_node = new ModelObjNode($related_model_obj->ID(), $related_model_obj->get_model(), $this->dont_traverse_models);
96
+				$this->nodes[ $related_model_obj->ID() ] = $entity_node;
97
+				$new_item_nodes[ $related_model_obj->ID() ] = $entity_node;
98
+			}
99
+			$num_identified += count($new_item_nodes);
100
+			if ($num_identified < $model_objects_to_identify) {
101
+				// And lastly do the work.
102
+				$num_identified += $this->visitAlreadyDiscoveredNodes(
103
+					$new_item_nodes,
104
+					$model_objects_to_identify - $num_identified
105
+				);
106
+			}
107
+		}
108 108
 
109
-        if (count($this->nodes) >= $this->count && $this->allChildrenComplete()) {
110
-            $this->complete = true;
111
-        }
112
-        return $num_identified;
113
-    }
109
+		if (count($this->nodes) >= $this->count && $this->allChildrenComplete()) {
110
+			$this->complete = true;
111
+		}
112
+		return $num_identified;
113
+	}
114 114
 
115
-    /**
116
-     * Checks if all the identified child nodes are complete or not.
117
-     * @since $VID:$
118
-     * @return bool
119
-     */
120
-    protected function allChildrenComplete()
121
-    {
122
-        foreach ($this->nodes as $model_obj_node) {
123
-            if (! $model_obj_node->isComplete()) {
124
-                return false;
125
-            }
126
-        }
127
-        return true;
128
-    }
115
+	/**
116
+	 * Checks if all the identified child nodes are complete or not.
117
+	 * @since $VID:$
118
+	 * @return bool
119
+	 */
120
+	protected function allChildrenComplete()
121
+	{
122
+		foreach ($this->nodes as $model_obj_node) {
123
+			if (! $model_obj_node->isComplete()) {
124
+				return false;
125
+			}
126
+		}
127
+		return true;
128
+	}
129 129
 
130
-    /**
131
-     * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget.
132
-     * @since $VID:$
133
-     * @param ModelObjNode[] $model_obj_nodes
134
-     * @param $work_budget
135
-     * @return int
136
-     */
137
-    protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
138
-    {
139
-        $work_done = 0;
140
-        if (! $model_obj_nodes) {
141
-            return 0;
142
-        }
143
-        foreach ($model_obj_nodes as $model_obj_node) {
144
-            if ($work_done >= $work_budget) {
145
-                break;
146
-            }
147
-            $work_done += $model_obj_node->visit($work_budget - $work_done);
148
-        }
149
-        return $work_done;
150
-    }
130
+	/**
131
+	 * Visits the provided nodes and keeps track of how much work was done, making sure to not go over budget.
132
+	 * @since $VID:$
133
+	 * @param ModelObjNode[] $model_obj_nodes
134
+	 * @param $work_budget
135
+	 * @return int
136
+	 */
137
+	protected function visitAlreadyDiscoveredNodes($model_obj_nodes, $work_budget)
138
+	{
139
+		$work_done = 0;
140
+		if (! $model_obj_nodes) {
141
+			return 0;
142
+		}
143
+		foreach ($model_obj_nodes as $model_obj_node) {
144
+			if ($work_done >= $work_budget) {
145
+				break;
146
+			}
147
+			$work_done += $model_obj_node->visit($work_budget - $work_done);
148
+		}
149
+		return $work_done;
150
+	}
151 151
 
152
-    /**
153
-     * Whether this item has already been initialized
154
-     */
155
-    protected function isDiscovered()
156
-    {
157
-        return $this->count !== null;
158
-    }
152
+	/**
153
+	 * Whether this item has already been initialized
154
+	 */
155
+	protected function isDiscovered()
156
+	{
157
+		return $this->count !== null;
158
+	}
159 159
 
160
-    /**
161
-     * @since $VID:$
162
-     * @return boolean
163
-     */
164
-    public function isComplete()
165
-    {
166
-        if ($this->complete === null) {
167
-            if (count($this->nodes) === $this->count) {
168
-                $this->complete = true;
169
-            } else {
170
-                $this->complete = false;
171
-            }
172
-        }
173
-        return $this->complete;
174
-    }
160
+	/**
161
+	 * @since $VID:$
162
+	 * @return boolean
163
+	 */
164
+	public function isComplete()
165
+	{
166
+		if ($this->complete === null) {
167
+			if (count($this->nodes) === $this->count) {
168
+				$this->complete = true;
169
+			} else {
170
+				$this->complete = false;
171
+			}
172
+		}
173
+		return $this->complete;
174
+	}
175 175
 
176
-    /**
177
-     * Discovers how many related model objects exist.
178
-     * @since $VID:$
179
-     * @return mixed|void
180
-     * @throws EE_Error
181
-     * @throws InvalidArgumentException
182
-     * @throws InvalidDataTypeException
183
-     * @throws InvalidInterfaceException
184
-     * @throws ReflectionException
185
-     */
186
-    protected function discover()
187
-    {
188
-        $this->count = $this->related_model->count([$this->whereQueryParams()]);
189
-    }
176
+	/**
177
+	 * Discovers how many related model objects exist.
178
+	 * @since $VID:$
179
+	 * @return mixed|void
180
+	 * @throws EE_Error
181
+	 * @throws InvalidArgumentException
182
+	 * @throws InvalidDataTypeException
183
+	 * @throws InvalidInterfaceException
184
+	 * @throws ReflectionException
185
+	 */
186
+	protected function discover()
187
+	{
188
+		$this->count = $this->related_model->count([$this->whereQueryParams()]);
189
+	}
190 190
 
191
-    /**
192
-     * @since $VID:$
193
-     * @return array
194
-     * @throws EE_Error
195
-     * @throws InvalidDataTypeException
196
-     * @throws InvalidInterfaceException
197
-     * @throws InvalidArgumentException
198
-     * @throws ReflectionException
199
-     */
200
-    protected function whereQueryParams()
201
-    {
202
-        $where_params =  [
203
-            $this->related_model->get_foreign_key_to(
204
-                $this->main_model->get_this_model_name()
205
-            )->get_name() => $this->id
206
-        ];
207
-        try {
208
-            $relation_settings = $this->main_model->related_settings_for($this->related_model->get_this_model_name());
209
-        } catch (EE_Error $e) {
210
-            // This will happen for has-and-belongs-to-many relations, when this node's related model is that join table
211
-            // which hasn't been explicitly declared in the main model object's model's relations.
212
-            $relation_settings = null;
213
-        }
214
-        if ($relation_settings instanceof EE_Has_Many_Any_Relation) {
215
-            $where_params[ $this->related_model->get_field_containing_related_model_name()->get_name() ] = $this->main_model->get_this_model_name();
216
-        }
217
-        return $where_params;
218
-    }
219
-    /**
220
-     * @since $VID:$
221
-     * @return array
222
-     */
223
-    public function toArray()
224
-    {
225
-        $tree = [
226
-            'count' => $this->count,
227
-            'complete' => $this->isComplete(),
228
-            'objs' => []
229
-        ];
230
-        foreach ($this->nodes as $id => $model_obj_node) {
231
-            $tree['objs'][ $id ] = $model_obj_node->toArray();
232
-        }
233
-        return $tree;
234
-    }
191
+	/**
192
+	 * @since $VID:$
193
+	 * @return array
194
+	 * @throws EE_Error
195
+	 * @throws InvalidDataTypeException
196
+	 * @throws InvalidInterfaceException
197
+	 * @throws InvalidArgumentException
198
+	 * @throws ReflectionException
199
+	 */
200
+	protected function whereQueryParams()
201
+	{
202
+		$where_params =  [
203
+			$this->related_model->get_foreign_key_to(
204
+				$this->main_model->get_this_model_name()
205
+			)->get_name() => $this->id
206
+		];
207
+		try {
208
+			$relation_settings = $this->main_model->related_settings_for($this->related_model->get_this_model_name());
209
+		} catch (EE_Error $e) {
210
+			// This will happen for has-and-belongs-to-many relations, when this node's related model is that join table
211
+			// which hasn't been explicitly declared in the main model object's model's relations.
212
+			$relation_settings = null;
213
+		}
214
+		if ($relation_settings instanceof EE_Has_Many_Any_Relation) {
215
+			$where_params[ $this->related_model->get_field_containing_related_model_name()->get_name() ] = $this->main_model->get_this_model_name();
216
+		}
217
+		return $where_params;
218
+	}
219
+	/**
220
+	 * @since $VID:$
221
+	 * @return array
222
+	 */
223
+	public function toArray()
224
+	{
225
+		$tree = [
226
+			'count' => $this->count,
227
+			'complete' => $this->isComplete(),
228
+			'objs' => []
229
+		];
230
+		foreach ($this->nodes as $id => $model_obj_node) {
231
+			$tree['objs'][ $id ] = $model_obj_node->toArray();
232
+		}
233
+		return $tree;
234
+	}
235 235
 
236
-    /**
237
-     * Gets the IDs of all the model objects to delete; indexed first by model object name.
238
-     * @since $VID:$
239
-     * @return array
240
-     */
241
-    public function getIds()
242
-    {
243
-        if (empty($this->nodes)) {
244
-            return [];
245
-        }
246
-        $ids = [
247
-            $this->related_model->get_this_model_name() => array_combine(
248
-                array_keys($this->nodes),
249
-                array_keys($this->nodes)
250
-            )
251
-        ];
252
-        foreach ($this->nodes as $model_obj_node) {
253
-            $ids = array_replace_recursive($ids, $model_obj_node->getIds());
254
-        }
255
-        return $ids;
256
-    }
236
+	/**
237
+	 * Gets the IDs of all the model objects to delete; indexed first by model object name.
238
+	 * @since $VID:$
239
+	 * @return array
240
+	 */
241
+	public function getIds()
242
+	{
243
+		if (empty($this->nodes)) {
244
+			return [];
245
+		}
246
+		$ids = [
247
+			$this->related_model->get_this_model_name() => array_combine(
248
+				array_keys($this->nodes),
249
+				array_keys($this->nodes)
250
+			)
251
+		];
252
+		foreach ($this->nodes as $model_obj_node) {
253
+			$ids = array_replace_recursive($ids, $model_obj_node->getIds());
254
+		}
255
+		return $ids;
256
+	}
257 257
 
258
-    /**
259
-     * Returns the number of sub-nodes found (ie, related model objects across this relation.)
260
-     * @since $VID:$
261
-     * @return int
262
-     */
263
-    public function countSubNodes()
264
-    {
265
-        return count($this->nodes);
266
-    }
258
+	/**
259
+	 * Returns the number of sub-nodes found (ie, related model objects across this relation.)
260
+	 * @since $VID:$
261
+	 * @return int
262
+	 */
263
+	public function countSubNodes()
264
+	{
265
+		return count($this->nodes);
266
+	}
267 267
 
268
-    /**
269
-     * Don't serialize the models. Just record their names on some dynamic properties.
270
-     * @since $VID:$
271
-     */
272
-    public function __sleep()
273
-    {
274
-        $this->m = $this->main_model->get_this_model_name();
275
-        $this->rm = $this->related_model->get_this_model_name();
276
-        return array_merge(
277
-            [
278
-                'm',
279
-                'rm',
280
-                'id',
281
-                'count',
282
-                'nodes',
283
-            ],
284
-            parent::__sleep()
285
-        );
286
-    }
268
+	/**
269
+	 * Don't serialize the models. Just record their names on some dynamic properties.
270
+	 * @since $VID:$
271
+	 */
272
+	public function __sleep()
273
+	{
274
+		$this->m = $this->main_model->get_this_model_name();
275
+		$this->rm = $this->related_model->get_this_model_name();
276
+		return array_merge(
277
+			[
278
+				'm',
279
+				'rm',
280
+				'id',
281
+				'count',
282
+				'nodes',
283
+			],
284
+			parent::__sleep()
285
+		);
286
+	}
287 287
 
288
-    /**
289
-     * Use the dynamic properties to instantiate the models we use.
290
-     * @since $VID:$
291
-     * @throws EE_Error
292
-     * @throws InvalidArgumentException
293
-     * @throws InvalidDataTypeException
294
-     * @throws InvalidInterfaceException
295
-     * @throws ReflectionException
296
-     */
297
-    public function __wakeup()
298
-    {
299
-        $this->main_model = EE_Registry::instance()->load_model($this->m);
300
-        $this->related_model = EE_Registry::instance()->load_model($this->rm);
301
-        parent::__wakeup();
302
-    }
288
+	/**
289
+	 * Use the dynamic properties to instantiate the models we use.
290
+	 * @since $VID:$
291
+	 * @throws EE_Error
292
+	 * @throws InvalidArgumentException
293
+	 * @throws InvalidDataTypeException
294
+	 * @throws InvalidInterfaceException
295
+	 * @throws ReflectionException
296
+	 */
297
+	public function __wakeup()
298
+	{
299
+		$this->main_model = EE_Registry::instance()->load_model($this->m);
300
+		$this->related_model = EE_Registry::instance()->load_model($this->rm);
301
+		parent::__wakeup();
302
+	}
303 303
 }
304 304
 // End of file RelationNode.php
305 305
 // Location: EventEspresso\core\services\orm\tree_traversal/RelationNode.php
Please login to merge, or discard this patch.