Passed
Push — master ( c28222...9253d7 )
by Sebastian
02:41
created

Mailcode_Translator::syntaxExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\FileHelper;
15
16
/**
17
 * Used to translate mailcode syntax to other syntaxes.
18
 *
19
 * @package Mailcode
20
 * @subpackage Translator
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Translator
24
{
25
    const ERROR_INVALID_SYNTAX_NAME = 73001;
26
27
    /**
28
     * Creates an instance of the specified syntax.
29
     *
30
     * @param string $name The name of the syntax, e.g. "ApacheVelocity"
31
     * @return Mailcode_Translator_Syntax
32
     */
33
    public function createSyntax(string $name) : Mailcode_Translator_Syntax
34
    {
35
        if($this->syntaxExists($name))
36
        {
37
            return new Mailcode_Translator_Syntax($name);
38
        }
39
40
        throw new Mailcode_Exception(
41
            'Invalid translation syntax',
42
            sprintf(
43
                'The syntax [%s] does not exist. Possible values are: [%s].',
44
                $name,
45
                implode(', ', $this->getSyntaxNames())
46
            ),
47
            self::ERROR_INVALID_SYNTAX_NAME
48
        );
49
    }
50
51
    /**
52
     * Retrieves an instance for each syntax available
53
     * in the system.
54
     *
55
     * @return Mailcode_Translator_Syntax[]
56
     */
57
    public function getSyntaxes() : array
58
    {
59
        $names = $this->getSyntaxNames();
60
        $result = array();
61
62
        foreach($names as $name)
63
        {
64
            $result[] = $this->createSyntax($name);
65
        }
66
67
        return $result;
68
    }
69
70
    /**
71
     * Retrieves a list of the names of all syntaxes supported
72
     * by the system.
73
     *
74
     * @return string[]
75
     */
76
    public function getSyntaxNames() : array
77
    {
78
        return FileHelper::createFileFinder(__DIR__.'/Translator/Syntax')
79
            ->getPHPClassNames();
80
    }
81
82
    /**
83
     * Checks whether the specified syntax exists.
84
     *
85
     * @param string $name The syntax name, e.g. "ApacheVelocity"
86
     * @return bool
87
     */
88
    public function syntaxExists(string $name) : bool
89
    {
90
        $names = $this->getSyntaxNames();
91
92
        return in_array($name, $names);
93
    }
94
}
95