Completed
Branch master (3d25b6)
by De Cramer
02:34
created

ManiaScript   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 0
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 24
cts 24
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getVarN() 0 4 1
A __toString() 0 17 1
A render() 0 10 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: olive
5
 * Date: 14/05/2017
6
 * Time: 12:04
7
 */
8
9
namespace eXpansion\Framework\Core\Model\Gui;
10
11
use FML\Types\Renderable;
12
13
/**
14
 * Class ManiaScript
15
 *
16
 * @TODO add other helper methods to clean escape script sutff.
17
 *
18
 * @package eXpansion\Framework\Core\Model\Gui;
19
 * @author  oliver de Cramer <[email protected]>
20
 */
21
class ManiaScript implements Renderable
22
{
23
    /** @var string  */
24
    protected $filePath;
25
26
    /** @var string  */
27
    protected $id;
28
29
    /** @var array */
30
    public $params;
31
32
    /**
33
     * ManiaScript constructor.
34
     *
35
     * @param string $filePath
36
     */
37 4
    public function __construct($filePath, $params)
38
    {
39 4
        $this->filePath = $filePath;
40 4
        $this->id = spl_object_hash($this);
41 4
        $this->params = $params;
42 4
    }
43
44
    /**
45
     * Get unique variable name.
46
     *
47
     * @param string $name
48
     *
49
     * @return string
50
     */
51 1
    public function getVarN($name)
52
    {
53 1
        return "exp_" . $this->id . "_$name";
54
    }
55
56
    /**
57
     * Generate script content
58
     *
59
     * @return string
60
     */
61 2
    public function __toString()
62
    {
63 2
        ob_start();
64
65 2
        echo "\n";
66 2
        echo "/**************************************************\n";
67 2
        echo " *     eXpansion : 2.*.*\n";
68 2
        echo "**************************************************/\n";
69 2
        echo "\n";
70
71 2
        include $this->filePath;
72
73 2
        $script = ob_get_contents();
74 2
        ob_end_clean();
75
76 2
        return $script;
77
    }
78
79
    /**
80
     * Render the XML element
81
     *
82
     * @param \DOMDocument $domDocument DOMDocument for which the XML element should be rendered
83
     *
84
     * @return \DOMElement
85
     */
86 2
    public function render(\DOMDocument $domDocument)
87
    {
88 2
        $scriptXml  = $domDocument->createElement("script");
89 2
        $scriptText = $this->__toString();
90
91 2
        $scriptComment = $domDocument->createComment($scriptText);
92 2
        $scriptXml->appendChild($scriptComment);
93
94 2
        return $scriptXml;
95
    }
96
}