Passed
Push — master ( af9933...6d2375 )
by Torben
04:41 queued 01:21
created

AttachmentService::getObjectAttachments()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 0
cp 0
crap 20
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 16
     * @param string $messageRecipient
65
     *
66 16
     * @return array Array with absolute filenames to attachments
67 16
     */
68
    public function getAttachments(
69
        array $settings,
70 16
        Registration $registration,
71 10
        int $messageType,
72 10
        string $messageRecipient
73 6
    ): array {
74 2
        $attachments = [];
75 2
        $settingPath = $this->getSettingsPath($messageType);
76 4
77 2
        if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient])) {
78 2
            // Attachments globally from TypoScript
79 2
            $config = $settings['notification'][$settingPath]['attachments'][$messageRecipient];
80 2
            $attachments = $this->getFileAttachments($config);
81 2
82
            // Attachments from Event properties
83
            $eventAttachments = $this->getObjectAttachments(
84 16
                $config['fromEventProperty'] ?? [],
85
                $registration->getEvent()
0 ignored issues
show
Bug introduced by
It seems like $registration->getEvent() can also be of type null; however, parameter $object of DERHANSEN\SfEventMgt\Ser...:getObjectAttachments() does only seem to accept TYPO3\CMS\Extbase\DomainObject\AbstractEntity, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
                /** @scrutinizer ignore-type */ $registration->getEvent()
Loading history...
86 14
            );
87 14
            $attachments = array_merge($attachments, $eventAttachments);
88
89
            // Attachments from Registration properties
90 14
            $registrationAttachments = $this->getObjectAttachments(
91 14
                $config['fromRegistrationProperty'] ?? [],
92
                $registration
93
            );
94 14
            $attachments = array_merge($attachments, $registrationAttachments);
95 14
        }
96 14
97 16
        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 14
     *  registrationNew {
107
     *    attachments {
108 14
     *      user {
109 14
     *        iCalFile = 1
110 10
     *      }
111 10
     *   }
112 10
     * }
113 10
     *
114 14
     *
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 14
        int $messageType,
125
        string $messageRecipient
126 14
    ): string {
127 14
        $file = '';
128 4
        $settingPath = $this->getSettingsPath($messageType);
129 4
130 4
        if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) &&
131 4
            (bool)$settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile']) {
132 4
            $file = GeneralUtility::tempnam(
133 4
                'event-' . $registration->getEvent()->getUid() . '-',
134 14
                '.ics'
135
            );
136
            $content = $this->iCalendarService->getiCalendarContent($registration->getEvent());
0 ignored issues
show
Bug introduced by
It seems like $registration->getEvent() can also be of type null; however, parameter $event of DERHANSEN\SfEventMgt\Ser...::getiCalendarContent() does only seem to accept DERHANSEN\SfEventMgt\Domain\Model\Event, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
            $content = $this->iCalendarService->getiCalendarContent(/** @scrutinizer ignore-type */ $registration->getEvent());
Loading history...
137
            GeneralUtility::writeFile($file, $content);
138
        }
139
140
        return $file;
141
    }
142
143
    /**
144 4
     * Returns the settingspath for the given messagetype
145
     *
146 4
     * @param int $messageType
147 4
     * @return string
148
     */
149 4
    protected function getSettingsPath(int $messageType): string
150
    {
151 2
        $settingPath = '';
152 2
        switch ($messageType) {
153 2
            case MessageType::REGISTRATION_NEW:
154 2
                $settingPath = 'registrationNew';
155 2
                break;
156 2
            case MessageType::REGISTRATION_WAITLIST_NEW:
157
                $settingPath = 'registrationWaitlistNew';
158 4
                break;
159
            case MessageType::REGISTRATION_CONFIRMED:
160 2
                $settingPath = 'registrationConfirmed';
161 2
                break;
162 4
            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