Completed
Push — develop ( 2993ce...48a3ec )
by Mike
09:32
created

Factory::createTemplateFromXml()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 5.525

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 1
dl 0
loc 37
ccs 21
cts 29
cp 0.7241
crap 5.525
rs 9.0168
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Transformer\Template;
17
18
use phpDocumentor\Transformer\Template;
19
use phpDocumentor\Transformer\Transformation;
20
21
class Factory
22
{
23
    const TEMPLATE_DEFINITION_FILENAME = 'template.xml';
24
25
    /** @var PathResolver */
26
    private $pathResolver;
27
28
    /**
29
     * Constructs a new template factory with its dependencies.
30
     */
31 1
    public function __construct(PathResolver $pathResolver)
32
    {
33 1
        $this->pathResolver = $pathResolver;
34 1
    }
35
36
    /**
37
     * Attempts to find, construct and return a template object with the given template name or (relative/absolute)
38
     * path.
39
     */
40 1
    public function get(string $nameOrPath): Template
41
    {
42 1
        return $this->createTemplateFromXml(
43 1
            $this->fetchTemplateXmlFromPath(
44 1
                $this->pathResolver->resolve($nameOrPath)
45
            )
46
        );
47
    }
48
49
    /**
50
     * Returns a list of all template names.
51
     *
52
     * @return string[]
53
     */
54 1
    public function getAllNames(): array
55
    {
56
        /** @var \RecursiveDirectoryIterator $files */
57 1
        $files = new \DirectoryIterator($this->getTemplatePath());
58
59 1
        $template_names = [];
60 1
        while ($files->valid()) {
61 1
            $name = $files->getBasename();
62
63
            // skip abstract files
64 1
            if (!$files->isDir() || in_array($name, ['.', '..'], true)) {
65 1
                $files->next();
66 1
                continue;
67
            }
68
69 1
            $template_names[] = $name;
70 1
            $files->next();
71
        }
72
73 1
        return $template_names;
74
    }
75
76
    /**
77
     * Returns the path where all templates are stored.
78
     */
79 1
    public function getTemplatePath(): string
80
    {
81 1
        return $this->pathResolver->getTemplatePath();
82
    }
83
84
    /**
85
     * Loads the template definition file from the given path and returns it's contents.
86
     */
87 1
    protected function fetchTemplateXmlFromPath(string $path): string
88
    {
89 1
        return file_get_contents($path . DIRECTORY_SEPARATOR . self::TEMPLATE_DEFINITION_FILENAME);
90
    }
91
92
    /**
93
     * Creates and returns a template object based on the provided template definition.
94
     */
95 1
    protected function createTemplateFromXml(string $xml): Template
96
    {
97 1
        $xml = new \SimpleXMLElement($xml);
98 1
        $template = new Template((string) $xml->name);
99 1
        $template->setAuthor((string) $xml->author . ((string)$xml->email ? ' <' . $xml->email . '>' : ''));
100 1
        $template->setVersion((string) $xml->version);
101 1
        $template->setCopyright((string) $xml->copyright);
102 1
        $template->setDescription((string) $xml->description);
103 1
        foreach ($xml->parameter as $parameter) {
104
            $parameterObject = new Parameter();
105
            $parameterObject->setKey((string) $parameter->attributes()->key);
106
            $parameterObject->setValue((string) $parameter);
107
            $template->setParameter($parameterObject->getKey(), $parameterObject);
108
        }
109 1
        $i = 0;
110 1
        foreach ($xml->transformations->transformation as $transformation) {
111 1
            $transformationObject = new Transformation(
112 1
                (string) $transformation->attributes()->query,
113 1
                (string) $transformation->attributes()->writer,
114 1
                (string) $transformation->attributes()->source,
115 1
                (string) $transformation->attributes()->artifact
116
            );
117 1
            $parameters = [];
118 1
            foreach ($transformation->parameter as $parameter) {
119
                $parameterObject = new Parameter();
120
                $parameterObject->setKey((string) $parameter->attributes()->key);
121
                $parameterObject->setValue((string) $parameter);
122
                $parameters[$parameterObject->getKey()] = $parameterObject;
123
            }
124 1
            $transformationObject->setParameters($parameters);
125
126 1
            $template[$i++] = $transformationObject;
127
        }
128 1
        $template->propagateParameters();
129
130 1
        return $template;
131
    }
132
}
133