Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| Location_Payload_Factory | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
9.18 | |
0.00% |
0 / 1 |
| create_payload | |
86.96% |
20 / 23 |
|
0.00% |
0 / 1 |
9.18 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class Location Payload Factory |
| 4 | * |
| 5 | * @package ChoctawNation\CNHSA_Federation |
| 6 | */ |
| 7 | |
| 8 | namespace ChoctawNation\CNHSA_Federation\WP\Payload; |
| 9 | |
| 10 | use WP_Post; |
| 11 | use WP_Error; |
| 12 | |
| 13 | /** |
| 14 | * Location Payload Factory Class |
| 15 | * Responsible for constructing the payload for location posts to be sent to the external API. |
| 16 | */ |
| 17 | class Location_Payload_Factory extends Payload_Factory { |
| 18 | /** |
| 19 | * Builds the payload for location posts. |
| 20 | * |
| 21 | * @param WP_Post $post The location post objects. |
| 22 | * @return array|WP_Error|null The payload array, a WP_Error on failure, or null if no payload is needed. |
| 23 | */ |
| 24 | public function create_payload( WP_Post $post ): array|WP_Error|null { |
| 25 | if ( 'locations' !== $post->post_type && 'services' !== $post->post_type ) { |
| 26 | return null; |
| 27 | } |
| 28 | $locations = array(); |
| 29 | if ( 'services' === $post->post_type ) { |
| 30 | /** |
| 31 | * Array of location posts |
| 32 | * |
| 33 | * @var WP_Post[] $locations |
| 34 | */ |
| 35 | $locations = get_field( 'location', $post->ID ); |
| 36 | if ( empty( $locations ) ) { |
| 37 | return null; |
| 38 | } |
| 39 | } elseif ( 'locations' === $post->post_type ) { |
| 40 | $locations[] = $post; |
| 41 | } |
| 42 | $location_data = array(); |
| 43 | foreach ( $locations as $location ) { |
| 44 | $data = array( |
| 45 | 'location_type' => get_field( 'choctaw_or_external_location', $location->ID ), |
| 46 | 'cno_location_id' => $location->ID, |
| 47 | 'location_name' => $location->post_title, |
| 48 | 'address' => get_field( 'address', $location->ID ), |
| 49 | 'city_state_zip' => get_field( 'city_state_zip', $location->ID ), |
| 50 | 'phone_number' => get_field( 'phone_number', $location->ID ), |
| 51 | 'additional_phone_number' => empty( get_field( 'additional_phone_number', $location->ID ) ) ? null : get_field( 'additional_phone_number', $location->ID ), |
| 52 | 'fax_number' => empty( get_field( 'fax_number', $location->ID ) ) ? null : get_field( 'fax_number', $location->ID ), |
| 53 | ); |
| 54 | $location_data[] = $data; |
| 55 | } |
| 56 | return $location_data; |
| 57 | } |
| 58 | } |