Completed
Branch FET-11146-improve-messages-dat... (6a5c34)
by
unknown
137:17 queued 126:02
created
messages/data_class/EE_Messages_Registrations_incoming_data.class.php 1 patch
Indentation   +169 added lines, -169 removed lines patch added patch discarded remove patch
@@ -5,7 +5,7 @@  discard block
 block discarded – undo
5 5
 use EventEspresso\core\exceptions\InvalidInterfaceException;
6 6
 
7 7
 if (! defined('EVENT_ESPRESSO_VERSION')) {
8
-    exit('No direct script access allowed');
8
+	exit('No direct script access allowed');
9 9
 }
10 10
 
11 11
 /**
@@ -21,172 +21,172 @@  discard block
 block discarded – undo
21 21
 {
22 22
 
23 23
 
24
-    /**
25
-     * Constructor.
26
-     *
27
-     * @param  EE_Registration[] $data expecting an array of EE_Registration objects.
28
-     * @throws EE_Error
29
-     * @access protected
30
-     */
31
-    public function __construct($data = array())
32
-    {
33
-
34
-        //validate that the first element in the array is an EE_Registration object.
35
-        if (! reset($data) instanceof EE_Registration) {
36
-            throw new EE_Error(
37
-                esc_html__(
38
-                    'The EE_Message_Registrations_incoming_data class expects an array of EE_Registration objects.',
39
-                    'event_espresso'
40
-                )
41
-            );
42
-        }
43
-        parent::__construct($data);
44
-    }
45
-
46
-
47
-    /**
48
-     * setup the data.
49
-     * Sets up the expected data object for the messages prep using incoming registration objects.
50
-     *
51
-     * @return void
52
-     * @throws EE_Error
53
-     * @throws EntityNotFoundException
54
-     * @access protected
55
-     */
56
-    protected function _setup_data()
57
-    {
58
-        //we'll loop through each contact and setup the data needed.  Note that many properties will just be set as
59
-        // empty because this data handler is for a very specific set of data (i.e. just what's related to the
60
-        // registration).
61
-
62
-        $this->reg_objs = $this->data();
63
-        $this->txn      = $this->_maybe_get_transaction();
64
-        $this->_assemble_data();
65
-    }
66
-
67
-
68
-    /**
69
-     * If the incoming registrations all share the same transaction then this will return the transaction object shared
70
-     * among the registrations. Otherwise the transaction object is set to null because its intended to only represent
71
-     * one transaction.
72
-     *
73
-     * @return EE_Transaction|null
74
-     * @throws EE_Error
75
-     * @throws EntityNotFoundException
76
-     */
77
-    protected function _maybe_get_transaction()
78
-    {
79
-        $transactions = array();
80
-        foreach ($this->reg_objs as $registration) {
81
-            if ($registration instanceof EE_Registration) {
82
-                $transaction = $registration->transaction();
83
-                if ($transaction instanceof EE_Transaction) {
84
-                    $transactions[$transaction->ID()] = $transaction;
85
-                }
86
-            }
87
-        }
88
-        return count($transactions) === 1 ? reset($transactions) : null;
89
-    }
90
-
91
-
92
-    /**
93
-     * Returns database safe representation of the data later used to when instantiating this object.
94
-     *
95
-     * @param array $registrations The incoming data to be prepped.
96
-     * @return EE_Registration[] The data being prepared for the db
97
-     * @throws EE_Error
98
-     * @throws InvalidArgumentException
99
-     * @throws InvalidDataTypeException
100
-     * @throws InvalidInterfaceException
101
-     */
102
-    public static  function convert_data_for_persistent_storage($registrations)
103
-    {
104
-        if (! self::validateRegistrationsForConversion($registrations)) {
105
-            return array();
106
-        }
107
-
108
-        //is this an array of ints?
109
-        $first_item = reset($registrations);
110
-        if (is_int($first_item)) {
111
-            return $registrations;
112
-        }
113
-
114
-        //k nope so let's pull from the registrations
115
-        $registration_ids = array_filter(
116
-            array_map(
117
-                function ($registration) {
118
-                    if ($registration instanceof EE_Registration) {
119
-                        return $registration->ID();
120
-                    }
121
-                    return false;
122
-                },
123
-                $registrations
124
-            )
125
-        );
126
-
127
-        return $registration_ids;
128
-    }
129
-
130
-
131
-    /**
132
-     * This validates incoming registrations (considers whether they are ids or EE_Registration objects.
133
-     *
134
-     * @param array $registrations Could be EE_Registration[] or int[]
135
-     * @return bool
136
-     * @throws EE_Error
137
-     * @throws InvalidArgumentException
138
-     * @throws InvalidDataTypeException
139
-     * @throws InvalidInterfaceException
140
-     */
141
-    protected static function validateRegistrationsForConversion($registrations)
142
-    {
143
-        if (is_array($registrations)) {
144
-            $first_item = reset($registrations);
145
-            if ($first_item instanceof EE_Registration) {
146
-                return true;
147
-            }
148
-            if (is_int($first_item)) {
149
-                //k let's some basic validation here.  This isn't foolproof but better than nothing.
150
-                //the purpose of this validation is to verify that the ids sent in match valid registrations existing
151
-                //in the db.  If the count is different, then we know they aren't valid.
152
-                $count_for_ids = EEM_Registration::instance()->count(
153
-                    array(
154
-                        array(
155
-                            'REG_ID' => array('IN', $registrations)
156
-                        )
157
-                    )
158
-                );
159
-                return $count_for_ids === count($registrations);
160
-            }
161
-        }
162
-        return false;
163
-    }
164
-
165
-
166
-    /**
167
-     * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage
168
-     * can be sent into this method and converted back into the format used for instantiating with this data handler.
169
-     *
170
-     * @param array $data
171
-     * @return EE_Registration[]
172
-     * @throws EE_Error
173
-     * @throws InvalidArgumentException
174
-     * @throws InvalidDataTypeException
175
-     * @throws InvalidInterfaceException
176
-     */
177
-    public static function convert_data_from_persistent_storage($data)
178
-    {
179
-        //since this was added later, we need to account of possible back compat issues where data already queued for
180
-        // generation is in the old format, which is an array of EE_Registration objects.  So if that's the case, then
181
-        // let's just return them
182
-        //@see https://events.codebasehq.com/projects/event-espresso/tickets/10127
183
-        if (is_array($data) && reset($data) instanceof EE_Registration) {
184
-            return $data;
185
-        }
186
-
187
-        $registrations = is_array($data)
188
-            ? EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $data))))
189
-            : array();
190
-        return $registrations;
191
-    }
24
+	/**
25
+	 * Constructor.
26
+	 *
27
+	 * @param  EE_Registration[] $data expecting an array of EE_Registration objects.
28
+	 * @throws EE_Error
29
+	 * @access protected
30
+	 */
31
+	public function __construct($data = array())
32
+	{
33
+
34
+		//validate that the first element in the array is an EE_Registration object.
35
+		if (! reset($data) instanceof EE_Registration) {
36
+			throw new EE_Error(
37
+				esc_html__(
38
+					'The EE_Message_Registrations_incoming_data class expects an array of EE_Registration objects.',
39
+					'event_espresso'
40
+				)
41
+			);
42
+		}
43
+		parent::__construct($data);
44
+	}
45
+
46
+
47
+	/**
48
+	 * setup the data.
49
+	 * Sets up the expected data object for the messages prep using incoming registration objects.
50
+	 *
51
+	 * @return void
52
+	 * @throws EE_Error
53
+	 * @throws EntityNotFoundException
54
+	 * @access protected
55
+	 */
56
+	protected function _setup_data()
57
+	{
58
+		//we'll loop through each contact and setup the data needed.  Note that many properties will just be set as
59
+		// empty because this data handler is for a very specific set of data (i.e. just what's related to the
60
+		// registration).
61
+
62
+		$this->reg_objs = $this->data();
63
+		$this->txn      = $this->_maybe_get_transaction();
64
+		$this->_assemble_data();
65
+	}
66
+
67
+
68
+	/**
69
+	 * If the incoming registrations all share the same transaction then this will return the transaction object shared
70
+	 * among the registrations. Otherwise the transaction object is set to null because its intended to only represent
71
+	 * one transaction.
72
+	 *
73
+	 * @return EE_Transaction|null
74
+	 * @throws EE_Error
75
+	 * @throws EntityNotFoundException
76
+	 */
77
+	protected function _maybe_get_transaction()
78
+	{
79
+		$transactions = array();
80
+		foreach ($this->reg_objs as $registration) {
81
+			if ($registration instanceof EE_Registration) {
82
+				$transaction = $registration->transaction();
83
+				if ($transaction instanceof EE_Transaction) {
84
+					$transactions[$transaction->ID()] = $transaction;
85
+				}
86
+			}
87
+		}
88
+		return count($transactions) === 1 ? reset($transactions) : null;
89
+	}
90
+
91
+
92
+	/**
93
+	 * Returns database safe representation of the data later used to when instantiating this object.
94
+	 *
95
+	 * @param array $registrations The incoming data to be prepped.
96
+	 * @return EE_Registration[] The data being prepared for the db
97
+	 * @throws EE_Error
98
+	 * @throws InvalidArgumentException
99
+	 * @throws InvalidDataTypeException
100
+	 * @throws InvalidInterfaceException
101
+	 */
102
+	public static  function convert_data_for_persistent_storage($registrations)
103
+	{
104
+		if (! self::validateRegistrationsForConversion($registrations)) {
105
+			return array();
106
+		}
107
+
108
+		//is this an array of ints?
109
+		$first_item = reset($registrations);
110
+		if (is_int($first_item)) {
111
+			return $registrations;
112
+		}
113
+
114
+		//k nope so let's pull from the registrations
115
+		$registration_ids = array_filter(
116
+			array_map(
117
+				function ($registration) {
118
+					if ($registration instanceof EE_Registration) {
119
+						return $registration->ID();
120
+					}
121
+					return false;
122
+				},
123
+				$registrations
124
+			)
125
+		);
126
+
127
+		return $registration_ids;
128
+	}
129
+
130
+
131
+	/**
132
+	 * This validates incoming registrations (considers whether they are ids or EE_Registration objects.
133
+	 *
134
+	 * @param array $registrations Could be EE_Registration[] or int[]
135
+	 * @return bool
136
+	 * @throws EE_Error
137
+	 * @throws InvalidArgumentException
138
+	 * @throws InvalidDataTypeException
139
+	 * @throws InvalidInterfaceException
140
+	 */
141
+	protected static function validateRegistrationsForConversion($registrations)
142
+	{
143
+		if (is_array($registrations)) {
144
+			$first_item = reset($registrations);
145
+			if ($first_item instanceof EE_Registration) {
146
+				return true;
147
+			}
148
+			if (is_int($first_item)) {
149
+				//k let's some basic validation here.  This isn't foolproof but better than nothing.
150
+				//the purpose of this validation is to verify that the ids sent in match valid registrations existing
151
+				//in the db.  If the count is different, then we know they aren't valid.
152
+				$count_for_ids = EEM_Registration::instance()->count(
153
+					array(
154
+						array(
155
+							'REG_ID' => array('IN', $registrations)
156
+						)
157
+					)
158
+				);
159
+				return $count_for_ids === count($registrations);
160
+			}
161
+		}
162
+		return false;
163
+	}
164
+
165
+
166
+	/**
167
+	 * Data that has been stored in persistent storage that was prepped by _convert_data_for_persistent_storage
168
+	 * can be sent into this method and converted back into the format used for instantiating with this data handler.
169
+	 *
170
+	 * @param array $data
171
+	 * @return EE_Registration[]
172
+	 * @throws EE_Error
173
+	 * @throws InvalidArgumentException
174
+	 * @throws InvalidDataTypeException
175
+	 * @throws InvalidInterfaceException
176
+	 */
177
+	public static function convert_data_from_persistent_storage($data)
178
+	{
179
+		//since this was added later, we need to account of possible back compat issues where data already queued for
180
+		// generation is in the old format, which is an array of EE_Registration objects.  So if that's the case, then
181
+		// let's just return them
182
+		//@see https://events.codebasehq.com/projects/event-espresso/tickets/10127
183
+		if (is_array($data) && reset($data) instanceof EE_Registration) {
184
+			return $data;
185
+		}
186
+
187
+		$registrations = is_array($data)
188
+			? EEM_Registration::instance()->get_all(array(array('REG_ID' => array('IN', $data))))
189
+			: array();
190
+		return $registrations;
191
+	}
192 192
 }
Please login to merge, or discard this patch.