Completed
Branch BUG-10738-inconsistency-in-ses... (860590)
by
unknown
57:39 queued 45:30
created

CreateAttendeeCommandHandler::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 24
rs 8.9713
c 0
b 0
f 0
1
<?php
2
namespace EventEspresso\core\services\commands\attendee;
3
4
use EE_Attendee;
5
use EE_Error;
6
use EE_Registration;
7
use EEM_Attendee;
8
use EventEspresso\core\exceptions\InvalidEntityException;
9
use EventEspresso\core\services\commands\CommandHandler;
10
use EventEspresso\core\services\commands\CommandInterface;
11
12
defined( 'EVENT_ESPRESSO_VERSION' ) || exit;
13
14
15
16
/**
17
 * Class CreateAttendeeCommandHandler
18
 * generates and validates an Attendee
19
 *
20
 * @package       Event Espresso
21
 * @author        Brent Christensen
22
 * @since         $VID:$
23
 */
24
class CreateAttendeeCommandHandler extends CommandHandler {
25
26
27
	/**
28
	 * @var EEM_Attendee $attendee_model
29
	 */
30
	protected $attendee_model;
31
32
33
34
	/**
35
	 * @param EEM_Attendee $attendee_model
36
	 */
37
	public function __construct(EEM_Attendee $attendee_model ) {
38
        $this->attendee_model = $attendee_model;
39
	}
40
41
42
43
	/**
44
	 * @param CommandInterface $command
45
	 * @return EE_Attendee
46
	 * @throws EE_Error
47
	 * @throws InvalidEntityException
48
	 */
49
	public function handle( CommandInterface $command ) {
50
		/** @var CreateAttendeeCommand $command */
51
		if ( ! $command instanceof CreateAttendeeCommand ) {
52
			throw new InvalidEntityException( get_class( $command ), 'CreateAttendeeCommand' );
53
		}
54
		// have we met before?
55
		$attendee = $this->findExistingAttendee(
56
			$command->registration(),
57
			$command->attendeeDetails()
58
		);
59
		// did we find an already existing record for this attendee ?
60
		if ( $attendee instanceof EE_Attendee ) {
61
			$attendee = $this->updateExistingAttendeeData(
62
				$attendee,
63
				$command->attendeeDetails()
64
			);
65
		} else {
66
			$attendee = $this->createNewAttendee(
67
				$command->registration(),
68
				$command->attendeeDetails()
69
			);
70
		}
71
		return $attendee;
72
	}
73
74
75
76
	/**
77
	 * find_existing_attendee
78
	 *
79
	 * @param EE_Registration $registration
80
	 * @param  array           $attendee_data
81
	 * @return EE_Attendee
82
	 */
83
	private function findExistingAttendee( EE_Registration $registration, array $attendee_data ) {
84
		$existing_attendee = null;
85
		// does this attendee already exist in the db ?
86
        // we're searching using a combination of first name, last name, AND email address
87
		$ATT_fname = ! empty( $attendee_data['ATT_fname'] )
88
			? $attendee_data['ATT_fname']
89
			: '';
90
		$ATT_lname = ! empty( $attendee_data['ATT_lname'] )
91
			? $attendee_data['ATT_lname']
92
			: '';
93
		$ATT_email = ! empty( $attendee_data['ATT_email'] )
94
			? $attendee_data['ATT_email']
95
			: '';
96
		// but only if those have values
97
		if ( $ATT_fname && $ATT_lname && $ATT_email ) {
98
			$existing_attendee = $this->attendee_model->find_existing_attendee(
99
				array(
100
					'ATT_fname' => $ATT_fname,
101
					'ATT_lname' => $ATT_lname,
102
					'ATT_email' => $ATT_email
103
				)
104
			);
105
		}
106
		return apply_filters(
107
			'FHEE_EventEspresso_core_services_commands_attendee_CreateAttendeeCommandHandler__findExistingAttendee__existing_attendee',
108
			$existing_attendee,
109
			$registration,
110
			$attendee_data
111
		);
112
	}
113
114
115
116
	/**
117
	 * _update_existing_attendee_data
118
	 * in case it has changed since last time they registered for an event
119
	 *
120
	 * @param EE_Attendee $existing_attendee
121
	 * @param  array       $attendee_data
122
	 * @return EE_Attendee
123
	 * @throws EE_Error
124
	 */
125
	private function updateExistingAttendeeData( EE_Attendee $existing_attendee, array $attendee_data ) {
126
		// first remove fname, lname, and email from attendee data
127
        // because these properties will be exactly the same as the returned attendee object,
128
        // since they were used in the query to get the attendee object in the first place
129
		$dont_set = array( 'ATT_fname', 'ATT_lname', 'ATT_email' );
130
		// now loop thru what's left and add to attendee CPT
131
		foreach ( $attendee_data as $property_name => $property_value ) {
132
			if (
133
				! in_array( $property_name, $dont_set, true )
134
				&& $this->attendee_model->has_field( $property_name )
135
			) {
136
				$existing_attendee->set( $property_name, $property_value );
137
			}
138
		}
139
		// better save that now
140
		$existing_attendee->save();
141
		return $existing_attendee;
142
	}
143
144
145
146
	/**
147
	 * create_new_attendee
148
	 *
149
	 * @param EE_Registration $registration
150
	 * @param  array           $attendee_data
151
	 * @return EE_Attendee
152
	 * @throws EE_Error
153
	 */
154
	private function createNewAttendee( EE_Registration $registration, array $attendee_data ) {
155
		// create new attendee object
156
		$new_attendee = EE_Attendee::new_instance( $attendee_data );
157
		// set author to event creator
158
		$new_attendee->set( 'ATT_author', $registration->event()->wp_user() );
159
		$new_attendee->save();
160
		return $new_attendee;
161
	}
162
163
164
165
}
166
// End of file CreateAttendeeCommandHandler.php
167
// Location: EventEspresso\core\services\commands\attendee/CreateAttendeeCommandHandler.php