DocumentationController::render()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of Jitamin.
5
 *
6
 * Copyright (C) Jitamin Team
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 Jitamin\Http\Controllers;
13
14
use Parsedown;
15
16
/**
17
 * Documentation Viewer.
18
 */
19
class DocumentationController extends Controller
20
{
21
    /**
22
     * Show documentation content.
23
     */
24
    public function show()
25
    {
26
        $page = $this->request->getStringParam('file', 'index');
0 ignored issues
show
Documentation introduced by
The property request does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
27
28
        if (!preg_match('/^[a-z0-9\-]+/', $page)) {
29
            $page = 'index';
30
        }
31
32
        $filename = $this->getPageFilename($page);
33
        $this->response->html($this->helper->layout->app('doc/show', $this->render($filename)));
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
Documentation introduced by
The property helper does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
34
    }
35
36
    /**
37
     * Regex callback to replace Markdown links.
38
     *
39
     * @param array $matches
40
     *
41
     * @return string
42
     */
43
    public function replaceMarkdownUrl(array $matches)
44
    {
45
        return '('.$this->helper->url->to('DocumentationController', 'show', ['file' => str_replace('.md', '', $matches[1])]).')';
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
46
    }
47
48
    /**
49
     * Regex callback to replace image links.
50
     *
51
     * @param array $matches
52
     *
53
     * @return string
54
     */
55
    public function replaceImageUrl(array $matches)
56
    {
57
        return '('.$this->getFileBaseUrl($matches[1]).')';
58
    }
59
60
    /**
61
     * Prepare Markdown file.
62
     *
63
     * @param string $filename
64
     *
65
     * @return array
66
     */
67
    protected function render($filename)
68
    {
69
        $data = file_get_contents($filename);
70
        $content = preg_replace_callback('/\((.*.md)\)/', [$this, 'replaceMarkdownUrl'], $data);
71
        $content = preg_replace_callback('/\((screenshots.*\.png)\)/', [$this, 'replaceImageUrl'], $content);
72
73
        list($title) = explode("\n", $data, 2);
74
75
        return [
76
            'content' => Parsedown::instance()->text($content),
77
            'title'   => $title !== 'Documentation' ? t('Documentation: %s', $title) : $title,
78
        ];
79
    }
80
81
    /**
82
     * Get Markdown file according to the current language.
83
     *
84
     * @param string $page
85
     *
86
     * @return string
87
     */
88
    protected function getPageFilename($page)
89
    {
90
        return $this->getFileLocation($page.'.md') ?:
91
            implode(DIRECTORY_SEPARATOR, [JITAMIN_DIR, 'doc', 'index.md']);
92
    }
93
94
    /**
95
     * Get base URL for Markdown links.
96
     *
97
     * @param string $filename
98
     *
99
     * @return string
100
     */
101
    protected function getFileBaseUrl($filename)
102
    {
103
        $language = $this->languageModel->getCurrentLanguage();
0 ignored issues
show
Documentation introduced by
The property languageModel does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
104
        $path = $this->getFileLocation($filename);
105
106
        if (strpos($path, $language) !== false) {
107
            $url = implode('/', ['doc', $language, $filename]);
108
        } else {
109
            $url = implode('/', ['doc', $filename]);
110
        }
111
112
        return $this->helper->url->base().$url;
0 ignored issues
show
Documentation introduced by
The property helper does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
113
    }
114
115
    /**
116
     * Get file location according to the current language.
117
     *
118
     * @param string $filename
119
     *
120
     * @return string
121
     */
122
    protected function getFileLocation($filename)
123
    {
124
        $files = [
125
            implode(DIRECTORY_SEPARATOR, [JITAMIN_DIR, 'doc', $this->languageModel->getCurrentLanguage(), $filename]),
0 ignored issues
show
Documentation introduced by
The property languageModel does not exist on object<Jitamin\Http\Cont...ocumentationController>. 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...
126
            implode(DIRECTORY_SEPARATOR, [JITAMIN_DIR, 'doc', $filename]),
127
        ];
128
129
        foreach ($files as $filename) {
130
            if (file_exists($filename)) {
131
                return $filename;
132
            }
133
        }
134
135
        return '';
136
    }
137
}
138