XmlLegacyRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 2
cbo 1
dl 0
loc 129
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A initXsltProcessor() 0 15 1
A render() 0 19 1
A imageUrl() 0 8 2
A linkUrl() 0 8 2
A highlight() 0 19 1
1
<?php
2
3
namespace Kaloa\Renderer;
4
5
use DOMDocument;
6
use XSLTProcessor;
7
8
/**
9
 *
10
 */
11
final class XmlLegacyRenderer implements RendererInterface
12
{
13
    /**
14
     *
15
     * @var XmlLegacyRenderer
16
     */
17
    private static $myself;
18
19
    /**
20
     *
21
     * @var XSLTProcessor
22
     */
23
    private $xsltProcessor = null;
24
25
    /**
26
     *
27
     * @var Config
28
     */
29
    private $config;
30
31 1
    public function __construct(Config $config)
32
    {
33 1
        $this->config = $config;
34
35 1
        self::$myself = $this;
36
37 1
        $this->initXsltProcessor();
38 1
    }
39
40
    /**
41
     *
42
     */
43 1
    private function initXsltProcessor()
44
    {
45 1
        $xsl = file_get_contents(__DIR__ . '/xmllegacy.xsl');
46
47 1
        $xsl = str_replace('__CLASS__', __CLASS__, $xsl);
48
49 1
        $xslDoc = new DOMDocument();
50 1
        $xslDoc->loadXML($xsl);
51
52 1
        $xsltProcessor = new XSLTProcessor();
53 1
        $xsltProcessor->registerPHPFunctions();
54 1
        $xsltProcessor->importStyleSheet($xslDoc);
55
56 1
        $this->xsltProcessor = $xsltProcessor;
57 1
    }
58
59
    /**
60
     *
61
     * @param  string $input
62
     * @return string
63
     */
64 1
    public function render($input)
65
    {
66
        $xmlCode = '<?xml version="1.0" encoding="utf-8"?>
67
<!DOCTYPE root [
68
<!ENTITY nbsp "&#160;">
69 1
]>';
70
71 1
        $xmlCode .= '<root>' . $input . '</root>';
72
73 1
        $xmldoc = new DOMDocument();
74 1
        $xmldoc->loadXML($xmlCode);
75
76 1
        $tmp = $this->xsltProcessor->transformToDoc($xmldoc);
77
78 1
        $html = $tmp->saveHTML($tmp->documentElement);
79 1
        $html = substr($html, 6, -7);
80
81 1
        return $html;
82
    }
83
84
    /**
85
     *
86
     * @param  string $path
87
     * @return string
88
     */
89 1
    public static function imageUrl($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
    {
91 1
        if (preg_match('/^https?:\/\//', $path)) {
92 1
            return $path;
93
        }
94
95 1
        return self::$myself->config->getResourceBasePath() . '/' . $path;
96
    }
97
98
    /**
99
     *
100
     * @param  string $path
101
     * @return string
102
     */
103 1
    public static function linkUrl($path)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105 1
        if (preg_match('/^(https?:\/\/|mailto:)/', $path)) {
106 1
            return $path;
107
        }
108
109 1
        return self::$myself->config->getResourceBasePath() . '/' . $path;
110
    }
111
112
    /**
113
     *
114
     * @todo OMG this method can't be efficient
115
     *
116
     * @param  string $source
117
     * @param  string $language
118
     * @return DOMElement?
0 ignored issues
show
Documentation introduced by
The doc-type DOMElement? could not be parsed: Unknown type name "DOMElement?" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
119
     */
120 1
    public static function highlight($source, $language)
121
    {
122
        // Smart trim code
123
        /*$source = preg_replace('/(?:\s*\n)?(.*)$/s', '$1', $source);
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
124
        $source = rtrim($source);*/
125
126 1
        $parsed_code = self::$myself->config->getSyntaxHighlighter()->highlight($source, $language);
127
128
        $parsed_code = '<?xml version="1.0" encoding="utf-8"?>
129
<!DOCTYPE root [
130
<!ENTITY nbsp "&#160;">
131 1
]>' . $parsed_code;
132
133 1
        $tmp = new DOMDocument();
134
135 1
        $tmp->loadXML($parsed_code);
136
137 1
        return $tmp->documentElement;
138
    }
139
}
140