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.

AutoResponderDataFactory::resolveRecipient()   B
last analyzed

Complexity

Conditions 6
Paths 18

Size

Total Lines 40

Duplication

Lines 27
Ratio 67.5 %

Importance

Changes 0
Metric Value
dl 27
loc 40
rs 8.6577
c 0
b 0
f 0
cc 6
nc 18
nop 1
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\Values\Content\Field;
9
use Netgen\InformationCollection\API\Constants;
10
use Netgen\InformationCollection\API\ConfigurationConstants;
11
use Netgen\InformationCollection\API\Exception\MissingValueException;
12
use Netgen\InformationCollection\API\Value\DataTransfer\EmailContent;
13
use Netgen\InformationCollection\API\Value\DataTransfer\TemplateContent;
14
use Netgen\InformationCollection\API\Value\Event\InformationCollected;
15
use function trim;
16
17
class AutoResponderDataFactory extends EmailDataFactory
18
{
19
    /**
20
     * Factory method.
21
     *
22
     * @param InformationCollected $value
23
     *
24
     * @return EmailContent
25
     */
26
    public function build(InformationCollected $value): EmailContent
27
    {
28
        $location = $value->getLocation();
0 ignored issues
show
Unused Code introduced by
$location is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
        $contentType = $value->getContentType();
30
31
        $template = $this->resolveTemplate($contentType->identifier);
32
33
        $templateWrapper = $this->twig->load($template);
34
        $data = new TemplateContent($value, $templateWrapper);
35
36
        $body = $this->resolveBody($data);
37
38
        return new EmailContent(
39
            $this->resolveRecipient($data),
40
            [$this->resolve($data, Constants::FIELD_SENDER, Constants::FIELD_TYPE_EMAIL)],
41
            $this->resolveSubject($data),
42
            $body
43
        );
44
    }
45
46
    /**
47
     * Returns resolved parameter.
48
     *
49
     * @param TemplateContent $data
50
     *
51
     * @return array
52
     */
53
    protected function resolveRecipient(TemplateContent $data)
54
    {
55
        $fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields();
56 View Code Duplication
        if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_RECIPIENT)) {
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...
57
            $rendered = $data->getTemplateWrapper()->renderBlock(
58
                Constants::FIELD_RECIPIENT,
59
                [
60
                    'event' => $data->getEvent(),
61
                    'collected_fields' => $fields,
62
                    'content' => $data->getContent(),
63
                ]
64
            );
65
66
            $rendered = trim($rendered);
67
        }
68
69 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...
70
71
            $emails = explode(',', $rendered);
72
73
            $emails = array_filter($emails, function($var) {
74
                return filter_var($var, FILTER_VALIDATE_EMAIL);
75
            });
76
77
            if (!empty($emails)) {
78
                return $emails;
79
            }
80
        }
81
82
        $field = 'email';
83 View Code Duplication
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER])) {
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...
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...
84
            $field = $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_FIELD_IDENTIFIER];
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...
85
        }
86
87
        if (array_key_exists($field, $fields)) {
88
            return [$fields[$field]->email];
89
        }
90
91
        throw new MissingValueException($field);
92
    }
93
94
    /**
95
     * Returns resolved parameter.
96
     *
97
     * @param TemplateContent $data
98
     *
99
     * @return string
100
     */
101
    protected function resolveSubject(TemplateContent $data)
102
    {
103
        $fields = $data->getEvent()->getInformationCollectionStruct()->getCollectedFields();
104 View Code Duplication
        if ($data->getTemplateWrapper()->hasBlock(Constants::FIELD_AUTO_RESPONDER_SUBJECT)) {
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...
105
            $rendered = $data->getTemplateWrapper()->renderBlock(
106
                Constants::FIELD_AUTO_RESPONDER_SUBJECT,
107
                [
108
                    'event' => $data->getEvent(),
109
                    'collected_fields' => $fields,
110
                    'content' => $data->getContent(),
111
                ]
112
            );
113
114
            return trim($rendered);
115
        }
116
117
        $content = $data->getContent();
118
        if (array_key_exists(Constants::FIELD_AUTO_RESPONDER_SUBJECT, $content->fields) &&
119
            !$this->fieldHelper->isFieldEmpty($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT)
120
        ) {
121
            $fieldValue = $this->translationHelper->getTranslatedField($content, Constants::FIELD_AUTO_RESPONDER_SUBJECT);
122
123
            if ($fieldValue instanceof Field) {
124
                return $fieldValue->value->text;
125
            }
126
        }
127
128 View Code Duplication
        if (!empty($this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT])) {
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...
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...
129
            return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][ConfigurationConstants::EMAIL_SUBJECT];
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...
130
        }
131
132
        $message = Constants::FIELD_AUTO_RESPONDER_SUBJECT . '|' . ConfigurationConstants::EMAIL_SUBJECT;
133
134
        throw new MissingValueException($message);
135
    }
136
}
137