Passed
Push — master ( 89d9c2...d06d8f )
by Sebastian
02:25
created

Mailcode_Collection::getFirstCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 10
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Collection} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Collection
7
 * @see Mailcode_Collection
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\OperationResult;
15
16
/**
17
 * Commands collection: container for commands.
18
 *
19
 * @package Mailcode
20
 * @subpackage Collection
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Collection
24
{
25
   /**
26
    * @var Mailcode_Commands_Command[]
27
    */
28
    protected $commands = array();
29
    
30
    /**
31
     * @var Mailcode_Collection_Error[]
32
     */
33
    protected $errors = array();
34
    
35
   /**
36
    * @var OperationResult|NULL
37
    */
38
    protected $validationResult;
39
    
40
   /**
41
    * Adds a command to the collection.
42
    * 
43
    * @param Mailcode_Commands_Command $command
44
    * @return Mailcode_Collection
45
    */
46
    public function addCommand(Mailcode_Commands_Command $command) : Mailcode_Collection
47
    {
48
        $this->commands[] = $command;
49
        
50
        // reset the collection's validation result, since it 
51
        // depends on the commands.
52
        $this->validationResult = null;
53
        
54
        return $this;
55
    }
56
    
57
   /**
58
    * Whether there are any commands in the collection.
59
    * 
60
    * @return bool
61
    */
62
    public function hasCommands() : bool
63
    {
64
        return !empty($this->commands);
65
    }
66
    
67
   /**
68
    * Counts the amount of commands in the collection.
69
    * 
70
    * @return int
71
    */
72
    public function countCommands() : int
73
    {
74
        return count($this->commands);
75
    }
76
77
    public function addErrorMessage(string $matchedText, string $message, int $code) : void
78
    {
79
        $this->errors[] = new Mailcode_Collection_Error_Message(
80
            $matchedText,
81
            $code,
82
            $message
83
        );
84
    }
85
    
86
    public function addInvalidCommand(Mailcode_Commands_Command $command) : void
87
    {
88
        $this->errors[] = new Mailcode_Collection_Error_Command($command);
89
    }
90
    
91
   /**
92
    * @return Mailcode_Collection_Error[]
93
    */
94
    public function getErrors()
95
    {
96
        $result = $this->getValidationResult();
97
        
98
        $errors = $this->errors;
99
        
100
        if(!$result->isValid())
101
        {
102
            $errors[] = new Mailcode_Collection_Error_Message(
103
                '',
104
                $result->getCode(),
105
                $result->getErrorMessage()
106
            );
107
        }
108
        
109
        return $errors;
110
    }
111
    
112
    public function isValid() : bool
113
    {
114
        $errors = $this->getErrors();
115
        
116
        return empty($errors);
117
    }
118
    
119
   /**
120
    * Retrieves all commands that were detected, in the exact order
121
    * they were found.
122
    * 
123
    * @return \Mailcode\Mailcode_Commands_Command[]
124
    */
125
    public function getCommands()
126
    {
127
       return $this->commands;
128
    }
129
    
130
   /**
131
    * Retrieves all unique commands by their matched
132
    * string hash: this ensures only commands that were
133
    * written the exact same way (including spacing)
134
    * are returned.
135
    * 
136
    * @return \Mailcode\Mailcode_Commands_Command[]
137
    */
138
    public function getGroupedByHash()
139
    {
140
        $hashes = array();
141
        
142
        foreach($this->commands as $command)
143
        {
144
            $hash = $command->getHash();
145
            
146
            if(!isset($hashes[$hash]))
147
            {
148
                $hashes[$hash] = $command;
149
            }
150
        }
151
            
152
        return array_values($hashes);
153
    }
154
155
   /**
156
    * Adds several commands at once.
157
    * 
158
    * @param Mailcode_Commands_Command[] $commands
159
    * @return Mailcode_Collection
160
    */
161
    public function addCommands(array $commands) : Mailcode_Collection
162
    {
163
        foreach($commands as $command)
164
        {
165
            $this->addCommand($command);
166
        }
167
        
168
        return $this;
169
    }
170
    
171
    public function mergeWith(Mailcode_Collection $collection) : Mailcode_Collection
172
    {
173
        $this->addCommands($collection->getCommands());
174
        
175
        return $this;
176
    }
177
    
178
    public function getVariables() : Mailcode_Variables_Collection
179
    {
180
        $collection = new Mailcode_Variables_Collection_Regular();
181
        
182
        foreach($this->commands as $command)
183
        {
184
            $collection->mergeWith($command->getVariables());
185
        }
186
        
187
        return $collection;
188
    }
189
    
190
    public function getValidationResult() : OperationResult
191
    {
192
        if(isset($this->validationResult))
193
        {
194
            return $this->validationResult;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->validationResult could return the type null which is incompatible with the type-hinted return AppUtils\OperationResult. Consider adding an additional type-check to rule them out.
Loading history...
195
        }
196
        
197
        $nesting = new Mailcode_Collection_NestingValidator($this);
198
        
199
        $this->validationResult = $nesting->validate(); 
200
        
201
        return $this->validationResult;
202
    }
203
    
204
    public function hasErrorCode(int $code) : bool
205
    {
206
        $errors = $this->getErrors();
207
        
208
        foreach($errors as $error)
209
        {
210
            if($error->getCode() === $code)
211
            {
212
                return true;
213
            }
214
        }
215
        
216
        return false;
217
    }
218
    
219
   /**
220
    * Retrieves only show variable commands in the collection, if any.
221
    * 
222
    * @return \Mailcode\Mailcode_Commands_Command_ShowVariable[]
223
    */
224
    public function getShowVariableCommands()
225
    {
226
        $result = array();
227
        
228
        foreach($this->commands as $command)
229
        {
230
            if($command instanceof Mailcode_Commands_Command_ShowVariable)
231
            {
232
                $result[] = $command;
233
            }
234
        }
235
        
236
        return $result;
237
    }
238
    
239
    public function getFirstCommand() : ?Mailcode_Commands_Command
240
    {
241
        $commands = $this->getCommands();
242
        
243
        if(!empty($commands))
244
        {
245
            return array_shift($commands);
246
        }
247
        
248
        return null;
249
    }
250
}
251