Completed
Branch dev (81ae86)
by
unknown
27:37 queued 17:44
created

DefaultTickets::create()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 4
nop 1
dl 0
loc 32
rs 8.4746
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\admin\entities;
4
5
use EE_Base_Class;
6
use EE_Datetime;
7
use EE_Error;
8
use EE_Price;
9
use EE_Ticket;
10
use EEM_Ticket;
11
use EventEspresso\core\exceptions\InvalidEntityException;
12
use ReflectionException;
13
14
/**
15
 * Class DefaultTickets
16
 * Description
17
 *
18
 * @package EventEspresso\core\domain\services\admin\events\editor
19
 * @author  Brent Christensen
20
 * @since   $VID:$
21
 */
22
class DefaultTickets implements DefaultEntityGeneratorInterface
23
{
24
25
    /**
26
     * @var DefaultPrices $default_prices
27
     */
28
    protected $default_prices;
29
30
    /**
31
     * @var EEM_Ticket $ticket_model
32
     */
33
    protected $ticket_model;
34
35
36
    /**
37
     * @param DefaultPrices $default_prices
38
     * @param EEM_Ticket    $ticket_model
39
     */
40
    public function __construct(DefaultPrices $default_prices, EEM_Ticket $ticket_model)
41
    {
42
        $this->default_prices = $default_prices;
43
        $this->ticket_model   = $ticket_model;
44
    }
45
46
47
    /**
48
     * @param EE_Datetime|EE_Base_Class $entity
49
     * @return EE_Ticket[]
50
     * @throws EE_Error
51
     * @throws InvalidEntityException
52
     * @throws ReflectionException
53
     * @since $VID:$
54
     */
55
    public function create(EE_Base_Class $entity): array
56
    {
57
        if (! $entity instanceof EE_Datetime) {
58
            throw new InvalidEntityException($entity, 'EE_Datetime');
59
        }
60
        $new_tickets     = [];
61
        $default_tickets = $this->ticket_model->get_all_default_tickets();
62
        if (is_array($default_tickets)) {
63
            foreach ($default_tickets as $default_ticket) {
64
                if (! $default_ticket instanceof EE_Ticket) {
65
                    throw new InvalidEntityException($default_ticket, 'EE_Ticket');
66
                }
67
                $existing_default_prices = $default_ticket->prices();
68
                // clone ticket, strip out ID, then save to get a new ID
69
                $default_ticket_clone = clone $default_ticket;
70
                $default_ticket_clone->set('TKT_ID', null);
71
                $default_ticket_clone->set('TKT_is_default', false);
72
                $default_ticket_clone->save();
73
                $default_ticket_clone->_add_relation_to($entity, 'Datetime');
74
                // temporarily adding relations to existing prices, but these will be cloned and removed
75
                // when passed to DefaultPrices::create() below so that they the clones can be freely mutated
76
                foreach ($existing_default_prices as $existing_default_price) {
77
                    if ($existing_default_price instanceof EE_Price) {
78
                        $existing_default_price->_add_relation_to($default_ticket_clone, 'Ticket');
79
                    }
80
                }
81
                $this->default_prices->create($default_ticket_clone);
82
                $new_tickets[ $default_ticket_clone->ID() ] = $default_ticket_clone;
83
            }
84
        }
85
        return $new_tickets;
86
    }
87
}
88