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 ( 6a4c44...6f222d )
by Mario
02:28
created

EmailDataFactory::resolveBody()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 1
1
<?php
2
3
namespace Netgen\Bundle\InformationCollectionBundle\Factory;
4
5
use eZ\Publish\API\Repository\ContentService;
6
use eZ\Publish\Core\Helper\FieldHelper;
7
use eZ\Publish\Core\Helper\TranslationHelper;
8
use Netgen\Bundle\InformationCollectionBundle\Constants;
9
use Netgen\Bundle\InformationCollectionBundle\DependencyInjection\ConfigurationConstants;
10
use Netgen\Bundle\InformationCollectionBundle\Event\InformationCollected;
11
use Netgen\Bundle\InformationCollectionBundle\Exception\MissingEmailBlockException;
12
use Netgen\Bundle\InformationCollectionBundle\Value\EmailData;
13
use Netgen\Bundle\InformationCollectionBundle\Value\TemplateData;
14
use Twig_Environment;
15
16
class EmailDataFactory
17
{
18
    /**
19
     * @var array
20
     */
21
    protected $config;
22
23
    /**
24
     * @var \eZ\Publish\Core\Helper\TranslationHelper
25
     */
26
    protected $translationHelper;
27
28
    /**
29
     * @var \eZ\Publish\Core\Helper\FieldHelper
30
     */
31
    protected $fieldHelper;
32
33
    /**
34
     * @var \eZ\Publish\API\Repository\ContentService
35
     */
36
    protected $contentService;
37
38
    /**
39
     * @var \Twig_Environment
40
     */
41
    protected $twig;
42
43
    /**
44
     * EmailDataFactory constructor.
45
     *
46
     * @param array $config
47
     * @param \eZ\Publish\Core\Helper\TranslationHelper $translationHelper
48
     * @param \eZ\Publish\Core\Helper\FieldHelper $fieldHelper
49
     * @param \eZ\Publish\API\Repository\ContentService $contentService
50
     * @param \Twig_Environment $twig
51
     */
52
    public function __construct(
53
        array $config,
54
        TranslationHelper $translationHelper,
55
        FieldHelper $fieldHelper,
56
        ContentService $contentService,
57
        Twig_Environment $twig
58
    ) {
59
        $this->config = $config;
60
        $this->translationHelper = $translationHelper;
61
        $this->fieldHelper = $fieldHelper;
62
        $this->contentService = $contentService;
63
        $this->twig = $twig;
64
    }
65
66
    /**
67
     * Factory method.
68
     *
69
     * @param InformationCollected $value
70
     *
71
     * @return EmailData
72
     */
73
    public function build(InformationCollected $value)
74
    {
75
        $location = $value->getLocation();
76
        $contentType = $value->getContentType();
77
        $content = $this->contentService->loadContent($location->contentId);
78
79
        $template = $this->resolveTemplate($contentType->identifier);
80
81
        $templateWrapper = $this->twig->load($template);
82
        $data = new TemplateData(array(
83
            'event' => $value,
84
            'content' => $content,
85
            'templateWrapper' => $templateWrapper,
86
        ));
87
88
        $body = $this->resolveBody($data);
89
90
        return new EmailData(
91
            array(
92
                'recipient' => $this->resolve($data, Constants::FIELD_RECIPIENT, Constants::FIELD_TYPE_EMAIL),
93
                'sender' => $this->resolve($data, Constants::FIELD_SENDER, Constants::FIELD_TYPE_EMAIL),
94
                'subject' => $this->resolve($data, Constants::FIELD_SUBJECT),
95
                'body' => $body,
96
            )
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
    protected function resolve(TemplateData $data, $field, $property = Constants::FIELD_TYPE_TEXT)
110
    {
111
        if ($data->templateWrapper->hasBlock($field)) {
112
            $rendered = $data->templateWrapper->renderBlock(
113
                $field,
114
                array(
115
                    'event' => $data->event,
116
                    'collected_fields' => $data->event->getInformationCollectionStruct()->getCollectedFields(),
117
                    'content' => $data->content,
118
                )
119
            );
120
121
            return trim($rendered);
122
        }
123
124
        $content = $data->content;
125
        if (array_key_exists($field, $content->fields) &&
126
            !$this->fieldHelper->isFieldEmpty($content, $field)
127
        ) {
128
            $fieldValue = $this->translationHelper->getTranslatedField($content, $field);
129
130
            return $fieldValue->value->$property;
131
        }
132
133
        return $this->config[ConfigurationConstants::DEFAULT_VARIABLES][$field];
134
    }
135
136
    /**
137
     * Returns resolved template name.
138
     *
139
     * @param string $contentTypeIdentifier
140
     *
141
     * @return string
142
     */
143
    protected function resolveTemplate($contentTypeIdentifier)
144
    {
145
        if (array_key_exists(
146
            $contentTypeIdentifier,
147
            $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES]
148
        )) {
149
            return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::CONTENT_TYPES][$contentTypeIdentifier];
150
        }
151
152
        return $this->config[ConfigurationConstants::TEMPLATES][ConfigurationConstants::SETTINGS_DEFAULT];
153
    }
154
155
    /**
156
     * Renders email template.
157
     *
158
     * @param TemplateData $data
159
     *
160
     * @throws MissingEmailBlockException
161
     *
162
     * @return string
163
     */
164
    protected function resolveBody(TemplateData $data)
165
    {
166
        $templateWrapper = $data->templateWrapper;
167
        if ($templateWrapper->hasBlock(Constants::BLOCK_EMAIL)) {
168
            return $templateWrapper
169
                ->renderBlock(
170
                    Constants::BLOCK_EMAIL,
171
                    array(
172
                        'event' => $data->event,
173
                        'collected_fields' => $data->event->getInformationCollectionStruct()->getCollectedFields(),
174
                        'content' => $data->content,
175
                        'default_variables' => $this->config[ConfigurationConstants::DEFAULT_VARIABLES],
176
                    )
177
                );
178
        }
179
180
        throw new MissingEmailBlockException(
181
            $templateWrapper->getSourceContext()->getName(),
182
            $templateWrapper->getBlockNames()
183
        );
184
    }
185
}
186