Passed
Push — master ( 9bab94...77b992 )
by Sebastian
13:50
created

Mailcode_Commands_Highlighter::appendCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 11
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Highlighter} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Highlighter
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Base command class with the common functionality for all commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Highlighter
22
{
23
   /**
24
    * @var Mailcode_Commands_Command
25
    */
26
    protected $command;
27
    
28
   /**
29
    * @var string[]
30
    */
31
    protected $parts = array();
32
    
33
    public function __construct(Mailcode_Commands_Command $command)
34
    {
35
       $this->command = $command;
36
    }
37
    
38
    public function highlight() : string
39
    {
40
        $this->parts = array();
41
        
42
        $this->appendBracket('{');
43
        $this->appendCommand();
44
        $this->appendParams();
45
        $this->appendBracket('}');
46
        
47
        return implode('', $this->parts);
48
    }
49
    
50
    protected function appendCommand() : void
51
    {
52
        $this->parts[] = $this->renderTag(array('command-name'), $this->command->getName());
53
        
54
        if($this->command->hasType())
55
        {
56
            $this->parts[] = ' '.$this->renderTag(array('command-type'), $this->command->getType());
57
        }
58
        
59
        $this->parts[] = $this->renderTag(array('hyphen'), ':');
60
        $this->parts[] = '<wbr>';
61
    }
62
    
63
    protected function appendParams()
64
    {
65
        if($this->command->hasParameters())
66
        {
67
            $this->parts[] = ' '.$this->renderTag(array('params'), $this->command->getParamsString());
68
        }
69
    }
70
    
71
    protected function renderTag(array $classes, string $content) : string
72
    {
73
        $parts = array();
74
        
75
        foreach($classes as $class)
76
        {
77
            $parts[] = 'mailcode-'.$class;
78
        }
79
        
80
        return sprintf(
81
            '<span class="%s">%s</span>',
82
            implode(' ', $parts),
83
            $content
84
        );
85
    }
86
    
87
    protected function appendBracket(string $bracket) : void
88
    {
89
        $this->parts[] = $this->renderTag(
90
            array('bracket'),
91
            $bracket
92
        );
93
    }
94
}
95