Completed
Push — development ( 6b83c1...4f1043 )
by Torben
11:42
created

AttachmentService::getAttachmentsFromProperty()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
ccs 12
cts 12
cp 1
cc 5
nc 4
nop 2
crap 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
     * Returns an array of filenames to attach to notifications
25
     *
26
     * Attachments must be configured as following (example for "registrationNew"):
27
     *
28
     *  registrationNew {
29
     *    attachments {
30
     *      user {
31
     *        fromFiles {
32
     *          1 = fileadmin/path-to-attachment.pdf
33
     *        }
34
     *        fromEventProperty {
35
     *          1 = files
36
     *          2 = image
37
     *        }
38
     *        fromRegistrationProperty {
39
     *          1 = propertyOfRegistration
40
     *        }
41
     *      }
42
     *      admin {
43
     *        fromFiles =
44
     *        fromEventProperty =
45
     *        fromRegistrationProperty =
46
     *      }
47
     *   }
48
     * }
49
     *
50
     * @param array $settings
51
     * @param Registration $registration
52
     * @param int $messageType
53
     * @param string $messageRecipient
54
     *
55
     * @return array Array with absolute filenames to attachments
56
     */
57
    public function getAttachments($settings, $registration, $messageType, $messageRecipient)
58
    {
59
        $attachments = [];
60
        $settingPath = '';
61
62
        switch ($messageType) {
63
            case MessageType::REGISTRATION_NEW:
64 8
                $settingPath = 'registrationNew';
65
                break;
66 8
            case MessageType::REGISTRATION_WAITLIST_NEW:
67 8
                $settingPath = 'registrationWaitlistNew';
68
                break;
69
            case MessageType::REGISTRATION_CONFIRMED:
70 8
                $settingPath = 'registrationConfirmed';
71 5
                break;
72 5
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
73 3
                $settingPath = 'registrationWaitlistConfirmed';
74 1
                break;
75 1
        }
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 configured fromFiles attachments from TypoScript settings
96 7
     *
97 8
     * @param array $settings
98
     * @return array
99
     */
100
    protected function getFileAttachments($settings)
101
    {
102
        $attachments = [];
103
        if (isset($settings['fromFiles']) && $settings['fromFiles'] !== '' && count($settings['fromFiles']) > 0) {
104
            foreach ($settings['fromFiles'] as $file) {
105
                $attachments[] = GeneralUtility::getFileAbsFileName($file);
106 7
            }
107
        }
108 7
109 7
        return $attachments;
110 5
    }
111 5
112 5
    /**
113 5
     * Returns the attachments from an object of all configured properties
114 7
     *
115
     * @param array $propertyNames
116
     * @param AbstractEntity $object
117
     * @return array
118
     */
119
    protected function getObjectAttachments($propertyNames, $object)
120
    {
121
        $attachments = [];
122
        if ($object && $propertyNames !== '' && is_array($propertyNames) && count($propertyNames) > 0) {
123
            foreach ($propertyNames as $propertyName) {
124 7
                if ($object->_hasProperty($propertyName)) {
125
                    $attachments = array_merge($attachments, $this->getAttachmentsFromProperty($object, $propertyName));
126 7
                }
127 7
            }
128 2
        }
129 2
130 2
        return $attachments;
131 2
    }
132 2
133 2
    /**
134 7
     * Returns an array wih the absolute path to all FAL files in the given object-property
135
     *
136
     * @param AbstractEntity $object
137
     * @param string $propertyName
138
     * @return array
139
     */
140
    protected function getAttachmentsFromProperty($object, $propertyName)
141
    {
142
        $attachments = [];
143
        $property = $object->_getProperty($propertyName);
144 2
145
        if ($property instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
146 2
            /** @var $property \TYPO3\CMS\Extbase\Persistence\ObjectStorage */
147 2
            foreach ($property as $object) {
148
                if ($object instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
149 2
                    $attachments[] = $object->getOriginalResource()->getForLocalProcessing(false);
150
                }
151 1
            }
152 1
        }
153 1
154 1
        if ($property instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
155 1
            /** @var $property \TYPO3\CMS\Extbase\Domain\Model\FileReference */
156 1
            $attachments[] = $property->getOriginalResource()->getForLocalProcessing(false);
157
        }
158 2
159
        return $attachments;
160 1
    }
161
}
162