Completed
Push — develop ( 927119...79656c )
by Julien
05:10 queued 02:36
created

HtmlOutput::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 4
b 0
f 0
nc 1
nop 1
dl 0
loc 21
ccs 0
cts 18
cp 0
crap 2
rs 9.3142
1
<?php
2
3
namespace SSpkS\Output;
4
5
use \Mustache_Engine;
6
use \Mustache_Loader_FilesystemLoader;
7
use \Mustache_Logger_StreamLogger;
8
9
class HtmlOutput
10
{
11
    private $config;
12
    private $mustache;
13
    private $tplVars;
14
    private $template;
15
16
    public function __construct(\SSpkS\Config $config)
17
    {
18
        $this->config = $config;
19
        $tplBase  = $this->config->basePath . DIRECTORY_SEPARATOR . $this->config->paths['themes'];
20
        $tplBase .= $this->config->site['theme'] . DIRECTORY_SEPARATOR . 'templates';
21
22
        $this->mustache = new Mustache_Engine(array(
23
            'loader'          => new Mustache_Loader_FilesystemLoader($tplBase),
24
            'partials_loader' => new Mustache_Loader_FilesystemLoader($tplBase . '/partials'),
25
            'charset'         => 'utf-8',
26
            'logger'          => new Mustache_Logger_StreamLogger('php://stderr'),
27
        ));
28
29
        $this->setVariable('siteName', $this->config->site['name']);
30
        $this->setVariable('baseUrl', $this->config->baseUrl);
31
        $this->setVariable('baseUrlRelative', $this->config->baseUrlRelative);
32
        $this->setVariable('themeUrl', $this->config->baseUrlRelative . $this->config->paths['themes'] . $this->config->site['theme'] . '/');
33
        $this->setVariable('requestUri', $_SERVER['REQUEST_URI']);
34
        $this->setVariable('commitHash', $this->config->SSPKS_COMMIT);
0 ignored issues
show
Documentation introduced by
The property SSPKS_COMMIT does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
35
        $this->setVariable('branch', $this->config->SSPKS_BRANCH);
0 ignored issues
show
Documentation introduced by
The property SSPKS_BRANCH does not exist on object<SSpkS\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
36
    }
37
38
    public function setVariable($name, $value)
39
    {
40
        $this->tplVars[$name] = $value;
41
    }
42
43
    public function setTemplate($tplName)
44
    {
45
        $this->template = $tplName;
46
    }
47
48
    public function output()
49
    {
50
        $tpl = $this->mustache->loadTemplate($this->template);
51
        echo $tpl->render($this->tplVars);
52
    }
53
}
54