Completed
Push — master ( ec29fe...2e5711 )
by Torben
09:02 queued 07:11
created

AttachmentService::getAttachments()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 35
ccs 24
cts 24
cp 1
rs 8.439
c 1
b 0
f 1
cc 6
eloc 24
nc 10
nop 4
crap 6
1
<?php
2
namespace DERHANSEN\SfEventMgt\Service\Notification;
3
4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use DERHANSEN\SfEventMgt\Domain\Model\Registration;
18
use DERHANSEN\SfEventMgt\Utility\MessageType;
19
use \TYPO3\CMS\Core\Utility\GeneralUtility;
20
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
21
22
/**
23
 * AttachmentService
24
 *
25
 * @author Torben Hansen <[email protected]>
26
 */
27
class AttachmentService
28
{
29
30
    /**
31
     * Returns an array of 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
     * @param array $settings
58
     * @param Registration $registration
59
     * @param int $messageType
60
     * @param string $messageRecipient
61
     *
62
     * @return array Array with absolute filenames to attachments
63
     */
64 8
    public function getAttachments($settings, $registration, $messageType, $messageRecipient)
65
    {
66 8
        $attachments = [];
67 8
        $settingPath = '';
68
69
        switch ($messageType) {
70 8
            case MessageType::REGISTRATION_NEW:
71 5
                $settingPath = 'registrationNew';
72 5
                break;
73 3
            case MessageType::REGISTRATION_WAITLIST_NEW:
74 1
                $settingPath = 'registrationWaitlistNew';
75 1
                break;
76 2
            case MessageType::REGISTRATION_CONFIRMED:
77 1
                $settingPath = 'registrationConfirmed';
78 1
                break;
79 1
            case MessageType::REGISTRATION_WAITLIST_CONFIRMED:
80 1
                $settingPath = 'registrationWaitlistConfirmed';
81 1
                break;
82
        }
83
84 8
        if (isset($settings['notification'][$settingPath]['attachments'][$messageRecipient])) {
85
            // Attachments globally from TypoScript
86 7
            $config = $settings['notification'][$settingPath]['attachments'][$messageRecipient];
87 7
            $attachments = $this->getFileAttachments($config);
88
89
            // Attachments from Event properties
90 7
            $eventAttachments = $this->getObjectAttachments($config['fromEventProperty'], $registration->getEvent());
91 7
            $attachments = array_merge($attachments, $eventAttachments);
92
93
            // Attachments from Registration properties
94 7
            $registrationAttachments = $this->getObjectAttachments($config['fromRegistrationProperty'], $registration);
95 7
            $attachments = array_merge($attachments, $registrationAttachments);
96 7
        }
97 8
        return $attachments;
98
    }
99
100
    /**
101
     * Returns configured fromFiles attachments from TypoScript settings
102
     *
103
     * @param array $settings
104
     * @return array
105
     */
106 7
    protected function getFileAttachments($settings)
107
    {
108 7
        $attachments = [];
109 7
        if (isset($settings['fromFiles']) && $settings['fromFiles'] !== '' && count($settings['fromFiles']) > 0) {
110 5
            foreach ($settings['fromFiles'] as $file) {
111 5
                $attachments[] = GeneralUtility::getFileAbsFileName($file);
112 5
            }
113 5
        }
114 7
        return $attachments;
115
    }
116
117
    /**
118
     * Returns the attachments from an object of all configured properties
119
     *
120
     * @param array $propertyNames
121
     * @param AbstractEntity $object
122
     * @return array
123
     */
124 7
    protected function getObjectAttachments($propertyNames, $object)
125
    {
126 7
        $attachments = [];
127 7
        if ($object && $propertyNames !== '' && count($propertyNames) > 0) {
128 2
            foreach ($propertyNames as $propertyName) {
129 2
                if ($object->_hasProperty($propertyName)) {
130 2
                    $attachments = array_merge($attachments, $this->getAttachmentsFromProperty($object, $propertyName));
131 2
                }
132 2
            }
133 2
        }
134 7
        return $attachments;
135
    }
136
137
    /**
138
     * Returns an array wih the absolute path to all FAL files in the given object-property
139
     *
140
     * @param AbstractEntity $object
141
     * @param string $propertyName
142
     * @return array
143
     */
144 2
    protected function getAttachmentsFromProperty($object, $propertyName)
145
    {
146 2
        $attachments = [];
147 2
        $property = $object->_getProperty($propertyName);
148
149 2
        if ($property instanceof \TYPO3\CMS\Extbase\Persistence\ObjectStorage) {
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Extbase\Persistence\ObjectStorage does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
150
            /** @var $property \TYPO3\CMS\Extbase\Persistence\ObjectStorage */
151 1
            foreach ($property as $object) {
152 1
                if ($object instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Extbase\Domain\Model\FileReference does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
153 1
                    $attachments[] = $object->getOriginalResource()->getForLocalProcessing(false);
154 1
                }
155 1
            }
156 1
        }
157
158 2
        if ($property instanceof \TYPO3\CMS\Extbase\Domain\Model\FileReference) {
0 ignored issues
show
Bug introduced by
The class TYPO3\CMS\Extbase\Domain\Model\FileReference does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
159
            /** @var $property \TYPO3\CMS\Extbase\Domain\Model\FileReference */
160 1
            $attachments[] = $property->getOriginalResource()->getForLocalProcessing(false);
161 1
        }
162 2
        return $attachments;
163
    }
164
}
165