Completed
Push — development ( 782837...fed765 )
by Torben
03:25
created

AttachmentService   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 28
lcom 1
cbo 6
dl 0
loc 205
ccs 56
cts 56
cp 1
rs 10
c 0
b 0
f 0

7 Methods

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