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.

MailDefinitionParserXml::parseMailDefinition()   B
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 32
nop 2
1
<?php
2
3
namespace Hautzi\SystemMailBundle\SystemMailer\MailDefinition;
4
5
use Hautzi\SystemMailBundle\SystemMailer\ParsedMessage;
6
use Hautzi\SystemMailBundle\SystemMailer\XML\XsdValidator;
7
8
class MailDefinitionParserXml implements ParserInterface
9
{
10
    /**
11
     * @var XsdValidator
12
     */
13
    private $validator;
14
15
    /**
16
     * @var string
17
     */
18
    private $xsdFile;
19
20
    /**
21
     * @param XsdValidator $validator
22
     * @param              $xsdFile
23
     */
24
    public function __construct(XsdValidator $validator, $xsdFile)
25
    {
26
        $this->validator = $validator;
27
        $this->xsdFile = $xsdFile;
28
    }
29
30
    /**
31
     * Parse XML Mail Definition string
32
     *
33
     * which looks something like this
34
     * <code>
35
     *   <email>
36
     *      <to name="yourname">[email protected]</email>
37
     *      <subject locale="de">Your subject</subject>
38
     *      <messageText locale="de">Your Message</messageText>
39
     *   </email>
40
     * </code>
41
     *
42
     * @param string $xml
43
     * @param string $locale
44
     *
45
     * @return ParsedMessage
46
     *
47
     * @throws \Exception
48
     */
49
    public function parseMailDefinition($xml, $locale = null)
50
    {
51
        $this->validator->validate($xml, $this->xsdFile);
52
53
        $parsed = simplexml_load_string($xml);
54
55
        $message = new ParsedMessage();
56
57
        $from = $parsed->from;
0 ignored issues
show
Bug introduced by
The property from does not seem to exist in SimpleXMLElement.

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...
58
        if ($from->count() > 0) {
59
            $message->setFrom((string)$from, (string)$from->attributes()['name']);
60
        }
61
62
        $replyTo = $parsed->replyTo;
0 ignored issues
show
Bug introduced by
The property replyTo does not seem to exist in SimpleXMLElement.

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...
63
        if ($replyTo->count() > 0) {
64
            $message->setReplyTo((string)$replyTo);
65
        }
66
67
        foreach ($parsed->to as $to) {
68
            $message->addTo((string)$to, (string)$to->attributes()['name']);
69
        }
70
        foreach ($parsed->cc as $cc) {
71
            $message->addCc((string)$cc, (string)$cc->attributes()['name']);
72
        }
73
        foreach ($parsed->bcc as $bcc) {
74
            $message->addBcc((string)$bcc, (string)$bcc->attributes()['name']);
75
        }
76
77
        $message->setSubject($this->getLocalizedTagContent($parsed->subject, $locale));
78
79
        $message->setMessageHtml($this->getLocalizedTagContent($parsed->messageHtml, $locale));
80
        $message->setMessageText($this->getLocalizedTagContent($parsed->messageText, $locale));
81
82
        return $message;
83
    }
84
85
    /**
86
     * @param \SimpleXMLElement $element
87
     * @param string            $locale
88
     *
89
     * @return string
90
     *
91
     * @throws \Exception
92
     */
93
    protected function getLocalizedTagContent(\SimpleXMLElement $element, $locale = null)
94
    {
95
        if (0 === $element->count()) {
96
            return '';
97
        }
98
99 View Code Duplication
        if (null === $locale) {
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...
100
            return $this->multilineRemoveIndent((string)$element, 'true' === (string)$element->attributes()['removeIndent']);
101
        }
102
103
        foreach ($element as $node) {
104 View Code Duplication
            if ($locale == (string)$node->attributes()['locale']) {
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
                return $this->multilineRemoveIndent((string)$node, 'true' === (string)$node->attributes()['removeIndent']);
106
            }
107
        }
108
109
        $message = sprintf('Locale "%s" not available in node "%s".', $locale, $element->getName());
110
        throw new \Exception($message);
111
    }
112
113
    /**
114
     * @param string $string
115
     * @param bool   $removeThatShit
116
     *
117
     * @return mixed
118
     */
119
    protected function multilineRemoveIndent($string, $removeThatShit = false)
120
    {
121
        if (!$removeThatShit) {
122
            return $string;
123
        }
124
125
        return trim(preg_replace('@^ *@m', '', $string));
126
    }
127
}
128