Completed
Pull Request — master (#278)
by Pascale
03:06
created

EventImport::injectEventRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Import default events.
5
 */
6
declare(strict_types=1);
7
8
namespace HDNET\Calendarize\Slots;
9
10
use HDNET\Calendarize\Command\ImportCommandController;
11
use HDNET\Calendarize\Domain\Model\Configuration;
12
use HDNET\Calendarize\Domain\Model\Event;
13
use HDNET\Calendarize\Domain\Repository\EventRepository;
14
use HDNET\Calendarize\Utility\DateTimeUtility;
15
use HDNET\Calendarize\Utility\HelperUtility;
16
use TYPO3\CMS\Extbase\Persistence\Generic\PersistenceManager;
17
18
/**
19
 * Import default events.
20
 */
21
class EventImport
22
{
23
    /**
24
     * Event repository.
25
     *
26
     * @var \HDNET\Calendarize\Domain\Repository\EventRepository
27
     */
28
    protected $eventRepository;
29
30
    public function injectEventRepository(EventRepository $eventRepository)
31
    {
32
        $this->eventRepository = $eventRepository;
33
    }
34
35
    /**
36
     * Run the import.
37
     *
38
     * @param array                   $event
39
     * @param ImportCommandController $commandController
40
     * @param int                     $pid
41
     * @param bool                    $handled
42
     *
43
     * @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
44
     *
45
     * @return array
46
     */
47
    public function importCommand(array $event, $commandController, $pid, $handled)
0 ignored issues
show
Unused Code introduced by
The parameter $handled is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
48
    {
49
        $commandController->enqueueMessage('Handle via Default Event Import Slot');
50
51
        $eventObject = $this->getEvent($event['uid']);
52
        $eventObject->setPid($pid);
53
        $eventObject->setTitle($event['title']);
54
        $eventObject->setDescription($this->nl2br($event['description']));
55
56
        $configuration = $this->getConfiguration($pid, $event['start'], $event['end']);
57
        $eventObject->addCalendarize($configuration);
58
59
        if (null === $eventObject->getUid()) {
60
            $this->eventRepository->update($eventObject);
61
            $commandController->enqueueMessage('Update Event Meta data: ' . $eventObject->getTitle(), 'Update');
62
        } else {
63
            $this->eventRepository->add($eventObject);
64
            $commandController->enqueueMessage('Add Event: ' . $eventObject->getTitle(), 'Add');
65
        }
66
67
        $this->persist();
68
        $handled = true;
69
70
        return [
71
            'event' => $event,
72
            'commandController' => $commandController,
73
            'pid' => $pid,
74
            'handled' => $handled,
75
        ];
76
    }
77
78
    /**
79
     * Get the configuration.
80
     *
81
     * @param int       $pid
82
     * @param \DateTime $startDate
83
     * @param \DateTime $endDate
84
     *
85
     * @return Configuration
86
     */
87
    protected function getConfiguration($pid, \DateTime $startDate, \DateTime $endDate)
88
    {
89
        $configuration = new Configuration();
90
        $configuration->setPid($pid);
91
        $configuration->setType(Configuration::TYPE_TIME);
92
        $configuration->setFrequency(Configuration::FREQUENCY_NONE);
93
        $configuration->setAllDay(true);
94
95
        $startTime = clone $startDate;
96
        $configuration->setStartDate(DateTimeUtility::resetTime($startDate));
97
        $endTime = clone $endDate;
98
        $configuration->setEndDate(DateTimeUtility::resetTime($endDate));
99
100
        $startTime = DateTimeUtility::getDaySecondsOfDateTime($startTime);
101
        if ($startTime > 0) {
102
            $configuration->setStartTime($startTime);
103
            $configuration->setEndTime(DateTimeUtility::getDaySecondsOfDateTime($endTime));
104
            $configuration->setAllDay(false);
105
        }
106
107
        return $configuration;
108
    }
109
110
    /**
111
     * Get the right event object (or a new one).
112
     *
113
     * @param string $importId
114
     *
115
     * @return Event
116
     */
117
    protected function getEvent($importId)
118
    {
119
        $eventObject = $this->eventRepository->findOneByImportId($importId);
0 ignored issues
show
Documentation Bug introduced by
The method findOneByImportId does not exist on object<HDNET\Calendarize...sitory\EventRepository>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
120
        if (!($eventObject instanceof Event)) {
121
            $eventObject = new Event();
122
        }
123
        $eventObject->setImportId($importId);
124
125
        return $eventObject;
126
    }
127
128
    /**
129
     * Store to the DB.
130
     */
131
    protected function persist()
132
    {
133
        $persist = HelperUtility::create(PersistenceManager::class);
134
        $persist->persistAll();
135
    }
136
137
    /**
138
     * Replace new lines.
139
     *
140
     * @param string $string
141
     *
142
     * @return string
143
     */
144
    protected function nl2br($string)
145
    {
146
        $string = \nl2br((string) $string);
147
148
        return \str_replace('\\n', '<br />', $string);
149
    }
150
}
151