Completed
Branch EDTR/master (5c103b)
by
unknown
10:13 queued 38s
created

NewEventDefaultEntities::getData()   B

Complexity

Conditions 6
Paths 2

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 2
nop 1
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\domain\services\admin\events\editor;
4
5
use DomainException;
6
use EE_Datetime;
7
use EE_Error;
8
use EventEspresso\core\exceptions\InvalidDataTypeException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use EventEspresso\core\exceptions\ModelConfigurationException;
11
use EventEspresso\core\exceptions\UnexpectedEntityException;
12
use InvalidArgumentException;
13
use ReflectionException;
14
15
/**
16
 * Class NewEventDefaultEntities
17
 * Description
18
 *
19
 * @package EventEspresso\core\domain\services\admin\events\editor
20
 * @author  Brent Christensen
21
 * @since   $VID:$
22
 */
23
class NewEventDefaultEntities extends EventEditorData
24
{
25
26
    /**
27
     * @param int $eventId
28
     * @return EE_Datetime[]
29
     * @throws DomainException
30
     * @throws EE_Error
31
     * @throws InvalidArgumentException
32
     * @throws InvalidDataTypeException
33
     * @throws InvalidInterfaceException
34
     * @throws ModelConfigurationException
35
     * @throws ReflectionException
36
     * @throws UnexpectedEntityException
37
     * @since $VID:$
38
     */
39
    public function getData($eventId)
40
    {
41
        $default_dates = $this->datetime_model->create_new_blank_datetime();
42
        if (is_array($default_dates) && isset($default_dates[0]) && $default_dates[0] instanceof EE_Datetime) {
43
            // clone date, strip out ID, then save to get a new ID
44
            $default_date = clone $default_dates[0];
45
            $default_date->set('DTT_ID', null);
46
            $default_date->save();
47
            $default_date->_add_relation_to($eventId, 'Event');
48
            $default_tickets = $this->ticket_model->get_all_default_tickets();
49
            $default_prices = $this->price_model->get_all_default_prices();
50
            foreach ($default_tickets as $default_ticket) {
51
                // clone ticket, strip out ID, then save to get a new ID
52
                $default_ticket_clone = clone $default_ticket;
53
                $default_ticket_clone->set('TKT_ID', null);
54
                $default_ticket_clone->save();
55
                $default_ticket_clone->_add_relation_to($default_date, 'Datetime');
56
                foreach ($default_prices as $default_price) {
0 ignored issues
show
Bug introduced by
The expression $default_prices of type integer|array<integer,object<EE_Base_Class>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
57
                    // clone price, strip out ID, then save to get a new ID
58
                    $default_price_clone = clone $default_price;
59
                    $default_price_clone->set('PRC_ID', null);
60
                    $default_price_clone->save();
61
                    $default_price_clone->_add_relation_to($default_ticket_clone, 'Ticket');
62
                }
63
            }
64
        }
65
        return $default_dates;
66
    }
67
}
68