Test Failed
Push — master ( 9ff364...742ef2 )
by Sebastian
03:58
created

getNestedMailcode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 19
rs 10
1
<?php
2
/**
3
 * File containing the trait {@see \Mailcode\Mailcode_Traits_Commands_ProtectedContent}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see \Mailcode\Mailcode_Traits_Commands_ProtectedContent
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Parser\PreParser;
15
16
/**
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 *
21
 * @see Mailcode_Interfaces_Commands_ProtectedContent
22
 */
23
trait Mailcode_Traits_Commands_ProtectedContent
24
{
25
    protected string $content = '';
26
    private ?Mailcode_Collection $nestedMailcode = null;
27
    protected ?Mailcode_Parser_Statement_Tokenizer_Token_Number $contentIDToken = null;
28
29
    public function getContent() : string
30
    {
31
        return $this->content;
32
    }
33
34
    public function getContentTrimmed() : string
35
    {
36
        return trim($this->content);
37
    }
38
39
    public function getContentIDToken() : ?Mailcode_Parser_Statement_Tokenizer_Token_Number
40
    {
41
        return $this->contentIDToken;
42
    }
43
44
    protected function validateSyntax_content_id() : void
45
    {
46
        $contentIDToken = $this->requireParams()
0 ignored issues
show
Bug introduced by
It seems like requireParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $contentIDToken = $this->/** @scrutinizer ignore-call */ requireParams()
Loading history...
47
            ->getInfo()
48
            ->getTokenByIndex(0);
49
50
        if($contentIDToken instanceof Mailcode_Parser_Statement_Tokenizer_Token_Number)
51
        {
52
            $this->contentIDToken = $contentIDToken;
53
            $this->loadContent();
54
            return;
55
        }
56
57
        $this->validationResult->makeError(
58
            t('The content ID parameter is missing.'),
59
            Mailcode_Interfaces_Commands_ProtectedContent::VALIDATION_ERROR_CONTENT_ID_MISSING
60
        );
61
    }
62
63
    public function getNestedMailcode() : Mailcode_Collection
64
    {
65
         if(isset($this->nestedMailcode))
66
         {
67
             return $this->nestedMailcode;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->nestedMailcode could return the type null which is incompatible with the type-hinted return Mailcode\Mailcode_Collection. Consider adding an additional type-check to rule them out.
Loading history...
68
         }
69
70
         if($this->isMailcodeEnabled())
0 ignored issues
show
Bug introduced by
It seems like isMailcodeEnabled() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
         if($this->/** @scrutinizer ignore-call */ isMailcodeEnabled())
Loading history...
71
         {
72
             $collection = Mailcode::create()->parseString($this->getContent());
73
         }
74
         else
75
         {
76
             $collection = new Mailcode_Collection();
77
         }
78
79
         $this->nestedMailcode = $collection;
80
81
         return $collection;
82
    }
83
84
    protected function validateSyntax_nested_mailcode() : void
85
    {
86
        $collection = $this->getNestedMailcode();
87
88
        if($collection->isValid())
89
        {
90
            return;
91
        }
92
93
        $errors = $collection->getErrors();
94
95
        foreach($errors as $error)
96
        {
97
            $this->getValidationResult()->makeError(
0 ignored issues
show
Bug introduced by
It seems like getValidationResult() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
            $this->/** @scrutinizer ignore-call */ 
98
                   getValidationResult()->makeError(
Loading history...
98
                $error->getMessage(),
99
                $error->getCode()
100
            );
101
        }
102
    }
103
104
    public function getContentID() : int
105
    {
106
        if(isset($this->contentIDToken))
107
        {
108
            return (int)$this->contentIDToken->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

108
            return (int)$this->contentIDToken->/** @scrutinizer ignore-call */ getValue();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
        }
110
111
        throw new Mailcode_Exception(
112
            'No content ID set',
113
            '',
114
            Mailcode_Interfaces_Commands_ProtectedContent::ERROR_NO_CONTENT_ID_TOKEN
115
        );
116
    }
117
118
    private function loadContent() : void
119
    {
120
        $contentID = $this->getContentID();
121
122
        $this->content = PreParser::getContent($contentID);
123
124
        PreParser::clearContent($contentID);
125
    }
126
127
    public function getNormalized() : string
128
    {
129
        return (new Mailcode_Commands_Normalizer_ProtectedContent($this))->normalize();
0 ignored issues
show
Bug introduced by
$this of type Mailcode\Mailcode_Traits_Commands_ProtectedContent is incompatible with the type Mailcode\Mailcode_Commands_Command expected by parameter $command of Mailcode\Mailcode_Comman...dContent::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
        return (new Mailcode_Commands_Normalizer_ProtectedContent(/** @scrutinizer ignore-type */ $this))->normalize();
Loading history...
130
    }
131
132
    public function getVariables() : Mailcode_Variables_Collection_Regular
133
    {
134
        $variables = parent::getVariables();
135
136
        if($this->isMailcodeEnabled())
137
        {
138
            $nested = $this->getNestedMailcode()
139
                ->getVariables()
140
                ->getAll();
141
142
            foreach($nested as $variable)
143
            {
144
                $variables->add($variable);
145
            }
146
        }
147
148
        return $variables;
149
    }
150
}
151