Passed
Push — master ( 472134...e9e97f )
by
unknown
07:15 queued 03:21
created

Snippet::compileParams()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 16
rs 10
cc 3
nc 4
nop 1
1
<?php
2
/**
3
 * File containing the class {@see \Mailcode\Factory\CommandSets\Set\Show\Snippet}.
4
 *
5
 * @package Mailcode
6
 * @subpackage Factory
7
 * @see \Mailcode\Factory\CommandSets\Set\Show\Snippet
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode\Factory\CommandSets\Set\Show;
13
14
use Mailcode\Mailcode_Commands_Command_ShowSnippet;
15
use Mailcode\Mailcode_Factory_CommandSets_Set;
16
use Mailcode\NamespaceInterface;
17
18
/**
19
 * Factory class for the `showsnippet` command.
20
 *
21
 * @package Mailcode
22
 * @subpackage Factory
23
 * @author Sebastian Mordziol <[email protected]>
24
 */
25
class Snippet extends Mailcode_Factory_CommandSets_Set
26
{
27
    public function create(string $snippetName, string $namespace = null): Mailcode_Commands_Command_ShowSnippet
28
    {
29
        $snippetName = $this->instantiator->filterVariableName($snippetName);
30
        $paramsString = $this->compileParams($namespace);
31
32
        $cmd = $this->commands->createCommand(
33
            'ShowSnippet',
34
            '',
35
            $snippetName . $paramsString,
36
            sprintf('{showsnippet: %s%s',
37
                $snippetName,
38
                $paramsString)
39
        );
40
41
        $this->instantiator->checkCommand($cmd);
42
43
        if ($cmd instanceof Mailcode_Commands_Command_ShowSnippet) {
44
            return $cmd;
45
        }
46
47
        throw $this->instantiator->exceptionUnexpectedType('ShowSnippet', $cmd);
48
    }
49
50
    private function compileParams(string $namespace = null): string
51
    {
52
        $params = array();
53
54
        if ($namespace) {
55
            $params[] = sprintf(
56
                ' ' . NamespaceInterface::PARAMETER_NAMESPACE_NAME . '=%s',
57
                $this->quoteString($namespace)
58
            );
59
        }
60
61
        if (!empty($params)) {
62
            return ' ' . implode(' ', $params);
63
        }
64
65
        return '';
66
    }
67
}
68