GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b5dfb6...a568a9 )
by Mario
40:05
created

EmailDataFactory::resolveEmail()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 35

Duplication

Lines 35
Ratio 100 %

Importance

Changes 0
Metric Value
dl 35
loc 35
rs 8.4266
c 0
b 0
f 0
cc 7
nc 8
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Factory;
6
7
use function array_key_exists;
8
use eZ\Publish\API\Repository\ContentService;
9
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFile;
10
use eZ\Publish\Core\Helper\FieldHelper;
11
use eZ\Publish\Core\Helper\TranslationHelper;
12
use Netgen\InformationCollection\API\Value\Event\InformationCollected;
13
use Netgen\InformationCollection\API\Exception\MissingEmailBlockException;
14
use Netgen\InformationCollection\API\Exception\MissingValueException;
15
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent;
16
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent;
17
use Netgen\InformationCollection\API\Constants;
18
use function trim;
19
use Twig_Environment;
20
21
class EmailDataFactory
22
{
23
    /**
24
     * @var array
25
     */
26
    protected $config;
27
28
    /**
29
     * @var \eZ\Publish\Core\Helper\TranslationHelper
30
     */
31
    protected $translationHelper;
32
33
    /**
34
     * @var \eZ\Publish\Core\Helper\FieldHelper
35
     */
36
    protected $fieldHelper;
37
38
    /**
39
     * @var \eZ\Publish\API\Repository\ContentService
40
     */
41
    protected $contentService;
42
43
    /**
44
     * @var \Twig_Environment
45
     */
46
    protected $twig;
47
48
    /**
49
     * EmailDataFactory constructor.
50
     *
51
     * @param array $config
52
     * @param \eZ\Publish\Core\Helper\TranslationHelper $translationHelper
53
     * @param \eZ\Publish\Core\Helper\FieldHelper $fieldHelper
54
     * @param \eZ\Publish\API\Repository\ContentService $contentService
55
     * @param \Twig_Environment $twig
56
     */
57
    public function __construct(
58
        array $config,
59
        TranslationHelper $translationHelper,
60
        FieldHelper $fieldHelper,
61
        ContentService $contentService,
62
        Twig_Environment $twig
63
    ) {
64
        $this->config = $config;
65
        $this->translationHelper = $translationHelper;
66
        $this->fieldHelper = $fieldHelper;
67
        $this->contentService = $contentService;
68
        $this->twig = $twig;
69
    }
70
71
    /**
72
     * Factory method.
73
     *
74
     * @param InformationCollected $value
75
     *
76
     * @return EmailData
77
     */
78
    public function build(InformationCollected $value)
79
    {
80
        $location = $value->getLocation();
81
        $contentType = $value->getContentType();
82
        $content = $this->contentService->loadContent($location->contentId);
83
84
        $template = $this->resolveTemplate($contentType->identifier);
85
86
        $templateWrapper = $this->twig->load($template);
87
        $data = new TemplateData($value, $content, $templateWrapper);
88
89
        $body = $this->resolveBody($data);
90
91
        return new EmailData(
92
            $this->resolveEmail($data, Constants::FIELD_RECIPIENT),
93
            $this->resolveEmail($data, Constants::FIELD_SENDER),
94
            $this->resolve($data, Constants::FIELD_SUBJECT),
95
            $body,
96
            $this->resolveAttachments($contentType->identifier, $value->getInformationCollectionStruct()->getCollectedFields())
97
        );
98
    }
99
100
    /**
101
     * Returns resolved parameter.
102
     *
103
     * @param TemplateData $data
104
     * @param string $field
105
     * @param string $property
106
     *
107
     * @return string
108
     */
109 View Code Duplication
    protected function resolve(TemplateData $data, $field, $property = Constants::FIELD_TYPE_TEXT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        $rendered = '';
112
        if ($data->getTemplateWrapper()->hasBlock($field)) {
113
            $rendered = $data->getTemplateWrapper()->renderBlock(
114
                $field,
115
                [
116
                    'event' => $data->getEvent(),
117
                    'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
118
                    'content' => $data->getContent(),
119
                ]
120
            );
121
122
            $rendered = trim($rendered);
123
        }
124
125
        if (!empty($rendered)) {
126
            return $rendered;
127
        }
128
129
        $content = $data->getContent();
130
        if (array_key_exists($field, $content->fields) &&
131
            !$this->fieldHelper->isFieldEmpty($content, $field)
132
        ) {
133
            $fieldValue = $this->translationHelper->getTranslatedField($content, $field);
134
135
            return $fieldValue->value->{$property};
136
        }
137
138
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) {
139
            return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field];
140
        }
141
142
        throw new MissingValueException($field);
143
    }
144
145
    /**
146
     * Returns resolved email parameter.
147
     *
148
     * @param TemplateData $data
149
     * @param string $field
150
     *
151
     * @return string
152
     */
153 View Code Duplication
    protected function resolveEmail(TemplateData $data, $field)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
154
    {
155
        $rendered = '';
156
        if ($data->getTemplateWrapper()->hasBlock($field)) {
157
            $rendered = $data->getTemplateWrapper()->renderBlock(
158
                $field,
159
                [
160
                    'event' => $data->getEvent(),
161
                    'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
162
                    'content' => $data->getContent(),
163
                ]
164
            );
165
166
            $rendered = trim($rendered);
167
        }
168
169
        if (!empty($rendered) && filter_var($rendered, FILTER_VALIDATE_EMAIL)) {
170
            return $rendered;
171
        }
172
173
        $content = $data->getContent();
174
        if (array_key_exists($field, $content->fields) &&
175
            !$this->fieldHelper->isFieldEmpty($content, $field)
176
        ) {
177
            $fieldValue = $this->translationHelper->getTranslatedField($content, $field);
178
179
            return $fieldValue->value->email;
180
        }
181
182
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) {
183
            return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field];
184
        }
185
186
        throw new MissingValueException($field);
187
    }
188
189
    /**
190
     * Returns resolved template name.
191
     *
192
     * @param string $contentTypeIdentifier
193
     *
194
     * @return string
195
     */
196
    protected function resolveTemplate($contentTypeIdentifier)
197
    {
198
        if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES])) {
199
            return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier];
200
        }
201
202
        return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::SETTINGS_DEFAULT];
203
    }
204
205
    /**
206
     * Renders email template.
207
     *
208
     * @param TemplateData $data
209
     *
210
     * @throws MissingEmailBlockException
211
     *
212
     * @return string
213
     */
214
    protected function resolveBody(TemplateData $data)
215
    {
216
        if ($data->getTemplateWrapper()->hasBlock(Constants::BLOCK_EMAIL)) {
217
            return $data->getTemplateWrapper()
218
                ->renderBlock(
219
                    Constants::BLOCK_EMAIL,
220
                    [
221
                        'event' => $data->getEvent(),
222
                        'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
223
                        'content' => $data->getContent(),
224
                        'default_variables' => !empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES])
225
                            ? $this->config[ConfigurationConstants::DEFAULT_VARIABLES] : null,
226
                    ]
227
                );
228
        }
229
230
        throw new MissingEmailBlockException(
231
            $data->getTemplateWrapper()->getSourceContext()->getName(),
232
            $data->getTemplateWrapper()->getBlockNames()
233
        );
234
    }
235
236
    /**
237
     * @param $contentTypeIdentifier
238
     * @param array $collectedFields
239
     *
240
     * @return BinaryFile[]|null
241
     */
242
    protected function resolveAttachments($contentTypeIdentifier, array $collectedFields)
243
    {
244
        if (empty($this->config[ConfigurationConstants::ATTACHMENTS])) {
245
            return null;
246
        }
247
248
        if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES])) {
249
            $send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier];
250
        } else {
251
            $send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::SETTINGS_DEFAULT];
252
        }
253
254
        if (!$send) {
255
            return null;
256
        }
257
258
        return $this->getBinaryFileFields($collectedFields);
259
    }
260
261
    /**
262
     * @param array $collectedFields
263
     *
264
     * @return BinaryFile[]|null
265
     */
266
    protected function getBinaryFileFields(array $collectedFields)
267
    {
268
        $filtered = [];
269
        foreach ($collectedFields as $identifier => $value) {
270
            if ($value instanceof BinaryFile) {
271
                $filtered[] = $value;
272
            }
273
        }
274
275
        return empty($filtered) ? null : $filtered;
276
    }
277
}
278