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.

EmailDataFactory::resolveEmail()   B
last analyzed

Complexity

Conditions 8
Paths 22

Size

Total Lines 47

Duplication

Lines 37
Ratio 78.72 %

Importance

Changes 0
Metric Value
dl 37
loc 47
rs 7.9119
c 0
b 0
f 0
cc 8
nc 22
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Netgen\InformationCollection\Core\Factory;
6
7
use eZ\Publish\Core\MVC\ConfigResolverInterface;
8
use Netgen\InformationCollection\Core\Action\EmailAction;
9
use function array_key_exists;
10
use eZ\Publish\API\Repository\Values\Content\Field;
11
use eZ\Publish\Core\FieldType\BinaryFile\Value as BinaryFile;
12
use eZ\Publish\Core\Helper\FieldHelper;
13
use eZ\Publish\Core\Helper\TranslationHelper;
14
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent;
15
use Netgen\InformationCollection\API\Constants;
16
use Netgen\InformationCollection\API\ConfigurationConstants;
17
use Netgen\InformationCollection\API\Exception\MissingEmailBlockException;
18
use Netgen\InformationCollection\API\Exception\MissingValueException;
19
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent;
20
use Netgen\InformationCollection\API\Value\Event\InformationCollected;
21
use function trim;
22
use Twig\Environment;
23
24
class EmailDataFactory extends BaseEmailDataFactory
25
{
26
    /**
27
     * @var array
28
     */
29
    protected $configResolver;
30
31
    /**
32
     * @var \eZ\Publish\Core\Helper\TranslationHelper
33
     */
34
    protected $translationHelper;
35
36
    /**
37
     * @var \eZ\Publish\Core\Helper\FieldHelper
38
     */
39
    protected $fieldHelper;
40
41
    /**
42
     * @var \Twig\Environment
43
     */
44
    protected $twig;
45
46
    /**
47
     * EmailDataFactory constructor.
48
     *
49
     * @param array $config
0 ignored issues
show
Documentation introduced by
There is no parameter named $config. Did you maybe mean $configResolver?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
50
     * @param \eZ\Publish\Core\Helper\TranslationHelper $translationHelper
51
     * @param \eZ\Publish\Core\Helper\FieldHelper $fieldHelper
52
     * @param \Twig\Environment $twig
53
     */
54
    public function __construct(
55
        ConfigResolverInterface $configResolver,
56
        TranslationHelper $translationHelper,
57
        FieldHelper $fieldHelper,
58
        Environment $twig
59
    ) {
60
        $this->configResolver = $configResolver;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configResolver of type object<eZ\Publish\Core\M...onfigResolverInterface> is incompatible with the declared type array of property $configResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
        $this->config = $this->configResolver->getParameter('action_config.' . EmailAction::$defaultName, 'netgen_information_collection');
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
62
        $this->translationHelper = $translationHelper;
63
        $this->fieldHelper = $fieldHelper;
64
        $this->twig = $twig;
65
    }
66
67
    /**
68
     * Factory method.
69
     *
70
     * @param InformationCollected $value
71
     *
72
     * @return EmailContent
73
     */
74
    public function build(InformationCollected $value): EmailContent
75
    {
76
        $contentType = $value->getContentType();
77
78
        $template = $this->resolveTemplate($contentType->identifier);
79
80
        $templateWrapper = $this->twig->load($template);
81
        $data = new TemplateContent($value, $templateWrapper);
82
83
        $body = $this->resolveBody($data);
84
85
        return new EmailContent(
86
            $this->resolveEmail($data, Constants::FIELD_RECIPIENT),
87
            $this->resolveEmail($data, Constants::FIELD_SENDER),
88
            $this->resolve($data, Constants::FIELD_SUBJECT),
89
            $body,
90
            $this->resolveAttachments($contentType->identifier, $value->getInformationCollectionStruct()->getFieldsData())
91
        );
92
    }
93
94
    /**
95
     * Returns resolved parameter.
96
     *
97
     * @param TemplateContent $data
98
     * @param string $field
99
     * @param string $property
100
     *
101
     * @return string
102
     */
103
    protected function resolve(TemplateContent $data, $field, $property = Constants::FIELD_TYPE_TEXT)
104
    {
105
        $rendered = '';
106 View Code Duplication
        if ($data->getTemplateWrapper()->hasBlock($field)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
107
            $rendered = $data->getTemplateWrapper()->renderBlock(
108
                $field,
109
                [
110
                    'event' => $data->getEvent(),
111
                    'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
112
                    'content' => $data->getContent(),
113
                ]
114
            );
115
116
            $rendered = trim($rendered);
117
        }
118
119
        if (!empty($rendered)) {
120
            return $rendered;
121
        }
122
123
        $content = $data->getContent();
124 View Code Duplication
        if (array_key_exists($field, $content->fields) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
125
            !$this->fieldHelper->isFieldEmpty($content, $field)
126
        ) {
127
            $fieldValue = $this->translationHelper->getTranslatedField($content, $field);
128
129
            if ($fieldValue instanceof Field) {
130
                return $fieldValue->value->{$property};
131
            }
132
        }
133
134 View Code Duplication
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) {
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
135
            return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
136
        }
137
138
        throw new MissingValueException($field);
139
    }
140
141
    /**
142
     * Returns resolved email parameter.
143
     *
144
     * @param TemplateContent $data
145
     * @param string $field
146
     *
147
     * @return array
148
     */
149
    protected function resolveEmail(TemplateContent $data, $field)
150
    {
151
        $rendered = '';
152 View Code Duplication
        if ($data->getTemplateWrapper()->hasBlock($field)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
153
            $rendered = $data->getTemplateWrapper()->renderBlock(
154
                $field,
155
                [
156
                    'event' => $data->getEvent(),
157
                    'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getCollectedFields(),
158
                    'content' => $data->getContent(),
159
                ]
160
            );
161
162
            $rendered = trim($rendered);
163
        }
164
165 View Code Duplication
        if (!empty($rendered)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
166
167
            $emails = explode(',', $rendered);
168
169
            $emails = array_filter($emails, function($var) {
170
                return filter_var($var, FILTER_VALIDATE_EMAIL);
171
            });
172
173
            if (!empty($emails)) {
174
                return $emails;
175
            }
176
        }
177
178
        $content = $data->getContent();
179 View Code Duplication
        if (array_key_exists($field, $content->fields) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
180
            !$this->fieldHelper->isFieldEmpty($content, $field)
181
        ) {
182
            $fieldValue = $this->translationHelper->getTranslatedField($content, $field);
183
184
            if ($fieldValue instanceof Field) {
185
                return [$fieldValue->value->email];
186
            }
187
188
        }
189
190 View Code Duplication
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field])) {
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
191
            return [$this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field]];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
192
        }
193
194
        throw new MissingValueException($field);
195
    }
196
197
    /**
198
     * Returns resolved template name.
199
     *
200
     * @param string $contentTypeIdentifier
201
     *
202
     * @return string
203
     */
204
    protected function resolveTemplate($contentTypeIdentifier)
205
    {
206
        if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES])) {
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
207
            return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
208
        }
209
210
        return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::SETTINGS_DEFAULT];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
211
    }
212
213
    /**
214
     * Renders email template.
215
     *
216
     * @param TemplateContent $data
217
     *
218
     * @throws MissingEmailBlockException
219
     *
220
     * @return string
221
     */
222
    protected function resolveBody(TemplateContent $data)
223
    {
224
        if ($data->getTemplateWrapper()->hasBlock(Constants::BLOCK_EMAIL)) {
225
            return $data->getTemplateWrapper()
226
                ->renderBlock(
227
                    Constants::BLOCK_EMAIL,
228
                    [
229
                        'event' => $data->getEvent(),
230
                        'collected_fields' => $data->getEvent()->getInformationCollectionStruct()->getFieldsData(),
231
                        'content' => $data->getContent(),
232
                        'default_variables' => !empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES])
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
233
                            ? $this->config[ConfigurationConstants::DEFAULT_VARIABLES] : null,
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
234
                    ]
235
                );
236
        }
237
238
        throw new MissingEmailBlockException(
239
            $data->getTemplateWrapper()->getSourceContext()->getName(),
240
            $data->getTemplateWrapper()->getBlockNames()
241
        );
242
    }
243
244
    /**
245
     * @param string $contentTypeIdentifier
246
     * @param array $collectedFields
247
     *
248
     * @return BinaryFile[]
249
     */
250
    protected function resolveAttachments(string $contentTypeIdentifier, array $collectedFields)
251
    {
252
        if (empty($this->config[ConfigurationConstants::ATTACHMENTS])) {
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
253
            return [];
254
        }
255
256
        if (array_key_exists($contentTypeIdentifier, $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES])) {
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
257
            $send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
258
        } else {
259
            $send = $this->config[ConfigurationConstants::ATTACHMENTS][ConfigurationConstants::SETTINGS_DEFAULT];
0 ignored issues
show
Bug introduced by
The property config does not seem to exist. Did you mean configResolver?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
260
        }
261
262
        if (!$send) {
263
            return [];
264
        }
265
266
        return $this->getBinaryFileFields($collectedFields);
267
    }
268
269
    /**
270
     * @param array $collectedFields
271
     *
272
     * @return BinaryFile[]
273
     */
274
    protected function getBinaryFileFields(array $collectedFields)
275
    {
276
        $filtered = [];
277
        foreach ($collectedFields as $identifier => $value) {
278
            if ($value instanceof BinaryFile) {
279
                $filtered[] = $value;
280
            }
281
        }
282
283
        return empty($filtered) ? [] : $filtered;
284
    }
285
}
286