AttachmentService   A
last analyzed

Complexity

Total Complexity 27

Size/Duplication

Total Lines 194
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
dl 0
loc 194
rs 10
c 2
b 0
f 0
wmc 27

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttachments() 0 35 3
A __construct() 0 2 1
A getSettingsPath() 0 19 5
A getFileAttachments() 0 10 5
A getObjectAttachments() 0 12 4
A getAttachmentsFromProperty() 0 22 6
A getICalAttachment() 0 27 3
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\Dto\CustomNotification;
15
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
16
use DERHANSEN\SfEventMgt\Service\ICalendarService;
17
use DERHANSEN\SfEventMgt\Utility\MessageType;
18
use TYPO3\CMS\Core\Utility\GeneralUtility;
19
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Domain\Model\FileReference was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\DomainObject\AbstractEntity was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Mvc\RequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Persistence\ObjectStorage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
24
class AttachmentService
25
{
26
    public function __construct(protected readonly ICalendarService $iCalendarService)
27
    {
28
    }
29
30
    /**
31
     * Returns an array of absolute filenames to attach to notifications
32
     *
33
     * Attachments must be configured as following (example for "registrationNew"):
34
     *
35
     * registrationNew {
36
     *   attachments {
37
     *     user {
38
     *       fromFiles {
39
     *         1 = fileadmin/path-to-attachment.pdf
40
     *       }
41
     *       fromEventProperty {
42
     *         1 = files
43
     *         2 = image
44
     *       }
45
     *       fromRegistrationProperty {
46
     *         1 = propertyOfRegistration
47
     *       }
48
     *     }
49
     *     admin {
50
     *       fromFiles {}
51
     *       fromEventProperty {}
52
     *       fromRegistrationProperty {}
53
     *     }
54
     *   }
55
     * }
56
     */
57
    public function getAttachments(
58
        array $settings,
59
        Registration $registration,
60
        int $messageType,
61
        string $messageRecipient,
62
        ?CustomNotification $customNotification = null
63
    ): array {
64
        $attachments = [];
65
        $settingPath = $this->getSettingsPath($messageType);
66
        $attachmentSettings = $settings['notification'][$settingPath]['attachments'][$messageRecipient] ?? [];
67
68
        if ($customNotification) {
69
            $attachmentSettings = $settings['notification']['customNotifications'][$customNotification->getTemplate()]['attachments'][$messageRecipient] ?? [];
70
        }
71
72
        if (!empty($attachmentSettings)) {
73
            // Attachments globally from TypoScript
74
            $attachments = $this->getFileAttachments($attachmentSettings);
75
76
            // Attachments from Event properties
77
            $eventAttachments = $this->getObjectAttachments(
78
                $attachmentSettings['fromEventProperty'] ?? [],
79
                $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

79
                /** @scrutinizer ignore-type */ $registration->getEvent()
Loading history...
80
            );
81
            $attachments = array_merge($attachments, $eventAttachments);
82
83
            // Attachments from Registration properties
84
            $registrationAttachments = $this->getObjectAttachments(
85
                $attachmentSettings['fromRegistrationProperty'] ?? [],
86
                $registration
87
            );
88
            $attachments = array_merge($attachments, $registrationAttachments);
89
        }
90
91
        return $attachments;
92
    }
93
94
    /**
95
     * Returns the absolute filename for to an iCal File of the event, if the iCalFile setting is set for
96
     * the given messageType
97
     *
98
     * Example:
99
     *
100
     *  registrationNew {
101
     *    attachments {
102
     *      user {
103
     *        iCalFile = 1
104
     *      }
105
     *   }
106
     * }
107
     */
108
    public function getICalAttachment(
109
        RequestInterface $request,
110
        array $settings,
111
        Registration $registration,
112
        int $messageType,
113
        string $messageRecipient,
114
        ?CustomNotification $customNotification = null
115
    ): string {
116
        $file = '';
117
        $settingPath = $this->getSettingsPath($messageType);
118
119
        $attachICalFile = (bool)($settings['notification'][$settingPath]['attachments'][$messageRecipient]['iCalFile'] ?? false);
120
121
        if ($customNotification) {
122
            $attachICalFile = (bool)($settings['notification']['customNotifications'][$customNotification->getTemplate()]['attachments'][$messageRecipient]['iCalFile'] ?? false);
123
        }
124
125
        if ($attachICalFile) {
126
            $file = GeneralUtility::tempnam(
127
                'event-' . $registration->getEvent()->getUid() . '-',
128
                '.ics'
129
            );
130
            $content = $this->iCalendarService->getiCalendarContent($request, $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

130
            $content = $this->iCalendarService->getiCalendarContent($request, /** @scrutinizer ignore-type */ $registration->getEvent());
Loading history...
131
            GeneralUtility::writeFile($file, $content);
132
        }
133
134
        return $file;
135
    }
136
137
    /**
138
     * Returns the settingspath for the given messagetype
139
     */
140
    protected function getSettingsPath(int $messageType): string
141
    {
142
        $settingPath = '';
143
        switch ($messageType) {
144
            case MessageType::REGISTRATION_NEW:
145
                $settingPath = 'registrationNew';
146
                break;
147
            case MessageType::REGISTRATION_WAITLIST_NEW:
148
                $settingPath = 'registrationWaitlistNew';
149
                break;
150
            case MessageType::REGISTRATION_CONFIRMED:
151
                $settingPath = 'registrationConfirmed';
152
                break;
153
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
154
                $settingPath = 'registrationWaitlistConfirmed';
155
                break;
156
        }
157
158
        return $settingPath;
159
    }
160
161
    /**
162
     * Returns configured fromFiles attachments from TypoScript settings
163
     */
164
    protected function getFileAttachments(array $settings): array
165
    {
166
        $attachments = [];
167
        if (isset($settings['fromFiles']) && is_array($settings['fromFiles']) && count($settings['fromFiles']) > 0) {
168
            foreach ($settings['fromFiles'] as $file) {
169
                $attachments[] = GeneralUtility::getFileAbsFileName($file);
170
            }
171
        }
172
173
        return $attachments;
174
    }
175
176
    /**
177
     * Returns the attachments from an object of all configured properties
178
     */
179
    protected function getObjectAttachments(array $propertyNames, AbstractEntity $object): array
180
    {
181
        $attachments = [];
182
        if (count($propertyNames) > 0) {
183
            foreach ($propertyNames as $propertyName) {
184
                if ($object->_hasProperty($propertyName)) {
185
                    $attachments = array_merge($attachments, $this->getAttachmentsFromProperty($object, $propertyName));
186
                }
187
            }
188
        }
189
190
        return $attachments;
191
    }
192
193
    /**
194
     * Returns an array wih the absolute path to all FAL files in the given object-property
195
     */
196
    protected function getAttachmentsFromProperty(AbstractEntity $object, string $propertyName): array
197
    {
198
        $attachments = [];
199
        if ($propertyName === '') {
200
            return $attachments;
201
        }
202
203
        $property = $object->_getProperty($propertyName);
204
205
        if ($property instanceof ObjectStorage) {
206
            foreach ($property as $fileRefetrenceObject) {
207
                if ($fileRefetrenceObject instanceof FileReference) {
208
                    $attachments[] = $fileRefetrenceObject->getOriginalResource()->getForLocalProcessing(false);
209
                }
210
            }
211
        }
212
213
        if ($property instanceof FileReference) {
214
            $attachments[] = $property->getOriginalResource()->getForLocalProcessing(false);
215
        }
216
217
        return $attachments;
218
    }
219
}
220