Completed
Push — master ( 5fc8fc...78226d )
by James
03:03
created

WebHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 30
Bugs 4 Features 2
Metric Value
wmc 13
c 30
b 4
f 2
lcom 1
cbo 6
dl 0
loc 127
ccs 40
cts 40
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setRepository() 0 6 1
A getRepository() 0 4 1
A setServer() 0 7 1
A getServer() 0 4 1
A setProject() 0 7 1
A getProject() 0 4 1
A find() 0 19 4
A render() 0 8 2
1
<?php
2
3
/**
4
 * This file is, guess what, part of WebHelper.
5
 *
6
 * (c) James <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace JamesRezo\WebHelper;
13
14
use Composer\Semver\VersionParser;
15
use Composer\Semver\Comparator;
16
use JamesRezo\WebHelper\Factory;
17
use JamesRezo\WebHelper\WebHelperRepository;
18
19
/**
20
 * WebHelper.
21
 */
22
class WebHelper
23
{
24
    /** @var WebHelperRepository a Repository instance */
25
    private $repository;
26
27
    /** @var VersionParser a VersionParser instance */
28
    private $versionParser;
29
30
    /** @var Comparator a Comparator instance */
31
    private $comparator;
32
33
    /** @var WebServer\WebServerInterface a Web Server instance */
34
    private $server;
35
36
    /** @var WebProject\WebProjectInterface the PHP Webapp to configure */
37
    private $project;
38
39
    /**
40
     * Base constructor.
41
     */
42 5
    public function __construct()
43 1
    {
44 5
        $this->versionParser = new VersionParser();
45 5
        $this->comparator = new Comparator();
46 5
    }
47
48
    /**
49
     * Sets the Repository Instance.
50
     *
51
     * @param string $resDir a Path of a Directives Repository
52
     */
53 5
    public function setRepository($resDir = '')
54
    {
55 5
        $this->repository = new WebHelperRepository($resDir);
56
57 5
        return $this;
58
    }
59
60
    /**
61
     * Gets the Repository Instance.
62
     *
63
     * @return WebHelperRepository the Repository Instance
64
     */
65 5
    public function getRepository()
66
    {
67 5
        return $this->repository;
68
    }
69
70
    /**
71
     * Sets the web server instance.
72
     *
73
     * @param string $server  a web server name
74
     * @param string $version a semver-like version
75
     */
76 5
    public function setServer($server, $version)
77
    {
78 5
        $factory = new Factory();
79 5
        $this->server = $factory->createWebServer($server, $this->versionParser->normalize($version));
80
81 5
        return $this;
82
    }
83
84
    /**
85
     * Gets the web server instance.
86
     *
87
     * @return WebServerInterface the web server instance
88
     */
89 3
    public function getServer()
90
    {
91 3
        return $this->server;
92
    }
93
94 1
    public function setProject($projectname, $version)
95
    {
96 1
        $factory = new Factory();
97 1
        $this->project = $factory->createWebProject($projectname, $this->versionParser->normalize($version));
98
99 1
        return $this;
100
    }
101
102 1
    public function getProject()
103
    {
104 1
        return $this->project;
105
    }
106
107
    /**
108
     * Finds the best template for a web server directive according to its version.
109
     *
110
     * @param  string $directive a directive
111
     * @return string            the relative path to the template
112
     */
113 3
    public function find($directive)
114
    {
115 3
        $memoize = $this->getRepository()->getMemoize();
116 3
        $serverName = $this->getServer()->getName();
117 3
        $return = '';
118
119 3
        $versions = array_keys($memoize[$serverName]);
120 3
        sort($versions);
121
122 3
        foreach ($versions as $version) {
123 3
            if ($this->comparator->greaterThanOrEqualTo($this->getServer()->getVersion(), $version) &&
124 3
                array_key_exists($directive, $memoize[$serverName][$version])
125 3
            ) {
126 2
                $return = $memoize[$serverName][$version][$directive];
127 2
            }
128 3
        }
129
130 3
        return $return;
131
    }
132
133
    /**
134
     * Outputs a webserver directive.
135
     *
136
     * @param  string $twigFile a relative path of a template
137
     * @param  array  $params   parameters
138
     * @return string           the directive output
139
     */
140 3
    public function render($twigFile, array $params = array())
141
    {
142
        try {
143 3
            return $this->getRepository()->getTwig()->render($twigFile, $params);
144 1
        } catch (\Twig_Error_Loader $e) {
145 1
            return '';
146
        }
147
    }
148
}
149