Passed
Push — typo3_11 ( 427061...02287e )
by Torben
04:24
created

AttachmentService::injectICalService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Service\Notification;
13
14
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
15
use DERHANSEN\SfEventMgt\Service\ICalendarService;
16
use DERHANSEN\SfEventMgt\Utility\MessageType;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
19
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
20
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
21
22
/**
23
 * AttachmentService
24
 */
25
class AttachmentService
26
{
27
    protected ICalendarService $iCalendarService;
28
29
    public function injectICalService(ICalendarService $iCalService)
30
    {
31
        $this->iCalendarService = $iCalService;
32
    }
33
34
    /**
35
     * Returns an array of filenames to attach to notifications
36
     *
37
     * Attachments must be configured as following (example for "registrationNew"):
38
     *
39
     * registrationNew {
40
     *   attachments {
41
     *     user {
42
     *       fromFiles {
43
     *         1 = fileadmin/path-to-attachment.pdf
44
     *       }
45
     *       fromEventProperty {
46
     *         1 = files
47
     *         2 = image
48
     *       }
49
     *       fromRegistrationProperty {
50
     *         1 = propertyOfRegistration
51
     *       }
52
     *     }
53
     *     admin {
54
     *       fromFiles =
55
     *       fromEventProperty =
56
     *       fromRegistrationProperty =
57
     *     }
58
     *   }
59
     * }
60
     *
61
     * @param array $settings
62
     * @param Registration $registration
63
     * @param int $messageType
64
     * @param string $messageRecipient
65
     *
66
     * @return array Array with absolute filenames to attachments
67
     */
68
    public function getAttachments(
69
        array $settings,
70
        Registration $registration,
71
        int $messageType,
72
        string $messageRecipient
73
    ): array {
74
        $attachments = [];
75
        $settingPath = $this->getSettingsPath($messageType);
76
77
        if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient])) {
78
            // Attachments globally from TypoScript
79
            $config = $settings['notification'][$settingPath]['attachments'][$messageRecipient];
80
            $attachments = $this->getFileAttachments($config);
81
82
            // Attachments from Event properties
83
            $eventAttachments = $this->getObjectAttachments(
84
                $config['fromEventProperty'] ?? [],
85
                $registration->getEvent()
86
            );
87
            $attachments = array_merge($attachments, $eventAttachments);
88
89
            // Attachments from Registration properties
90
            $registrationAttachments = $this->getObjectAttachments(
91
                $config['fromRegistrationProperty'] ?? [],
92
                $registration
93
            );
94
            $attachments = array_merge($attachments, $registrationAttachments);
95
        }
96
97
        return $attachments;
98
    }
99
100
    /**
101
     * Returns the absolute filename for to an iCal File of the event, if the iCalFile setting is set for
102
     * the given messageType
103
     *
104
     * Example:
105
     *
106
     *  registrationNew {
107
     *    attachments {
108
     *      user {
109
     *        iCalFile = 1
110
     *      }
111
     *   }
112
     * }
113
     *
114
     *
115
     * @param array $settings
116
     * @param Registration $registration
117
     * @param int $messageType
118
     * @param string $messageRecipient
119
     * @return string
120
     */
121
    public function getICalAttachment(
122
        array $settings,
123
        Registration $registration,
124
        int $messageType,
125
        string $messageRecipient
126
    ): string {
127
        $file = '';
128
        $settingPath = $this->getSettingsPath($messageType);
129
130
        if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) &&
131
            (bool)$settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) {
132
            $file = GeneralUtility::tempnam(
133
                'event-' . $registration->getEvent()->getUid() . '-',
134
                '.ics'
135
            );
136
            $content = $this->iCalendarService->getiCalendarContent($registration->getEvent());
137
            GeneralUtility::writeFile($file, $content);
138
        }
139
140
        return $file;
141
    }
142
143
    /**
144
     * Returns the settingspath for the given messagetype
145
     *
146
     * @param int $messageType
147
     * @return string
148
     */
149
    protected function getSettingsPath(int $messageType): string
150
    {
151
        $settingPath = '';
152
        switch ($messageType) {
153
            case MessageType::REGISTRATION_NEW:
154
                $settingPath = 'registrationNew';
155
                break;
156
            case MessageType::REGISTRATION_WAITLIST_NEW:
157
                $settingPath = 'registrationWaitlistNew';
158
                break;
159
            case MessageType::REGISTRATION_CONFIRMED:
160
                $settingPath = 'registrationConfirmed';
161
                break;
162
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
163
                $settingPath = 'registrationWaitlistConfirmed';
164
                break;
165
        }
166
167
        return $settingPath;
168
    }
169
170
    /**
171
     * Returns configured fromFiles attachments from TypoScript settings
172
     *
173
     * @param array $settings
174
     * @return array
175
     */
176
    protected function getFileAttachments(array $settings): array
177
    {
178
        $attachments = [];
179
        if (isset($settings['fromFiles']) && is_array($settings['fromFiles']) && count($settings['fromFiles']) > 0) {
180
            foreach ($settings['fromFiles'] as $file) {
181
                $attachments[] = GeneralUtility::getFileAbsFileName($file);
182
            }
183
        }
184
185
        return $attachments;
186
    }
187
188
    /**
189
     * Returns the attachments from an object of all configured properties
190
     *
191
     * @param array $propertyNames
192
     * @param AbstractEntity $object
193
     * @return array
194
     */
195
    protected function getObjectAttachments(array $propertyNames, AbstractEntity $object): array
196
    {
197
        $attachments = [];
198
        if (count($propertyNames) > 0) {
199
            foreach ($propertyNames as $propertyName) {
200
                if ($object->_hasProperty($propertyName)) {
201
                    $attachments = array_merge($attachments, $this->getAttachmentsFromProperty($object, $propertyName));
202
                }
203
            }
204
        }
205
206
        return $attachments;
207
    }
208
209
    /**
210
     * Returns an array wih the absolute path to all FAL files in the given object-property
211
     *
212
     * @param AbstractEntity $object
213
     * @param string $propertyName
214
     * @return array
215
     */
216
    protected function getAttachmentsFromProperty(AbstractEntity $object, string $propertyName): array
217
    {
218
        $attachments = [];
219
        $property = $object->_getProperty($propertyName);
220
221
        if ($property instanceof ObjectStorage) {
222
            foreach ($property as $object) {
223
                if ($object instanceof FileReference) {
224
                    $attachments[] = $object->getOriginalResource()->getForLocalProcessing(false);
225
                }
226
            }
227
        }
228
229
        if ($property instanceof FileReference) {
230
            $attachments[] = $property->getOriginalResource()->getForLocalProcessing(false);
231
        }
232
233
        return $attachments;
234
    }
235
}
236