/var/www/html_uk/wp-content/plugins/automatewoo/includes/Triggers/Utilities/BookingDataLayer.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace AutomateWoo\Triggers\Utilities;

use 
AutomateWoo\Customer_Factory;
use 
AutomateWoo\Data_Layer;
use 
AutomateWoo\DataTypes\DataTypes;
use 
AutomateWoo\Exceptions\InvalidValue;
use 
WC_Booking;
use 
WC_Order;

/**
 * Trait BookingDataLayer
 *
 * @since 5.3.0
 */
trait BookingDataLayer {

    
/**
     * Get the supplied data items for a booking.
     *
     * @return string[]
     */
    
protected function get_supplied_data_items_for_booking(): array {
        return [ 
DataTypes::BOOKINGDataTypes::CUSTOMERDataTypes::PRODUCTDataTypes::ORDER ];
    }

    
/**
     * Generate a booking data layer from a booking object.
     *
     * Includes booking, customer, booking product and order data types.
     *
     * @param WC_Booking $booking
     *
     * @return Data_Layer
     *
     * @throws InvalidValue If the booking's customer or booking is not found.
     */
    
protected function generate_booking_data_layerWC_Booking $booking ): Data_Layer {
        
// First try to retrieve customer from order.
        
$order $booking->get_order();
        
// Bookings can be made without an order, so there's no need to log if the customer isn't found through the order.
        
$log_error false;
        
$customer  Customer_Factory::get_by_order$ordertrue$log_error );
        if ( ! 
$customer ) {
            
// If that fails, retrieve customer from booking.
            
$customer Customer_Factory::get_by_user_id$booking->get_customer_id() );
        }

        if ( ! 
$customer ) {
            throw 
InvalidValue::item_not_foundesc_htmlDataTypes::CUSTOMER ) );
        }

        
$product $booking->get_product();
        if ( ! 
$product ) {
            throw 
InvalidValue::item_not_foundesc_htmlDataTypes::PRODUCT ) );
        }

        return new 
Data_Layer(
            [
                
DataTypes::BOOKING  => $booking,
                
DataTypes::CUSTOMER => $customer,
                
DataTypes::PRODUCT  => $product,
                
DataTypes::ORDER    => $order ?: new WC_Order(),
            ]
        );
    }
}