Passed
Push — master ( cd350b...3a08d5 )
by Thierry
03:10
created

AssetManager::getJsOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Plugin\Code;
4
5
use Jaxon\Plugin\Plugin;
6
use Jaxon\Utils\Config\Config;
7
use Jaxon\Utils\File\Minifier;
8
use Jaxon\Utils\Http\UriDetector;
9
use Jaxon\Utils\Http\UriException;
10
11
use function rtrim;
12
use function is_file;
13
use function file_put_contents;
14
15
class AssetManager
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class AssetManager
Loading history...
16
{
17
    /**
18
     * @var Config
19
     */
20
    protected $xConfig;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
21
22
    /**
23
     * @var UriDetector
24
     */
25
    private $xUriDetector;
26
27
    /**
28
     * @var Minifier
29
     */
30
    private $xMinifier;
31
32
    /**
33
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
34
     */
35
    protected $bIncludeAllAssets;
36
37
    /**
38
     * Default library URL
39
     *
40
     * @var string
41
     */
42
    const JS_LIB_URL = 'https://cdn.jsdelivr.net/gh/jaxon-php/[email protected]/dist';
43
44
    /**
45
     * The constructor
46
     *
47
     * @param Config $xConfig
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 6 spaces after parameter type; 1 found
Loading history...
48
     * @param UriDetector $xUriDetector
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     * @param Minifier $xMinifier
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
50
     */
51
    public function __construct(Config $xConfig, UriDetector $xUriDetector, Minifier $xMinifier)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
52
    {
53
        $this->xConfig = $xConfig;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
        $this->xUriDetector = $xUriDetector;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
55
        $this->xMinifier = $xMinifier;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 9 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
56
        $this->bIncludeAllAssets = $xConfig->getOption('assets.include.all', true);
57
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
58
59
    /**
60
     * Get js options
61
     *
62
     * @return string
63
     */
64
    public function getJsOptions(): string
65
    {
66
        return $this->xConfig->getOption('js.app.options', '');
67
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
68
69
    /**
70
     * Check if the assets of this plugin shall be included in Jaxon generated code.
71
     *
72
     * @param Plugin $xPlugin
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
73
     *
74
     * @return bool
75
     */
76
    public function shallIncludeAssets(Plugin $xPlugin): bool
77
    {
78
        if($this->bIncludeAllAssets)
79
        {
80
            return true;
81
        }
82
        $sPluginOptionName = 'assets.include.' . $xPlugin->getName();
83
        return $this->xConfig->getOption($sPluginOptionName, true);
84
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
85
86
    /**
87
     * Get the HTML tags to include Jaxon javascript files into the page
88
     *
89
     * @return array
90
     */
91
    public function getJsLibFiles(): array
92
    {
93
        $sJsExtension = $this->xConfig->getOption('js.app.minify') ? '.min.js' : '.js';
94
        // The URI for the javascript library files
95
        $sJsLibUri = rtrim($this->xConfig->getOption('js.lib.uri', self::JS_LIB_URL), '/') . '/';
96
        // Add component files to the javascript file array;
97
        $aJsFiles = [$sJsLibUri . 'jaxon.core' . $sJsExtension];
98
        if($this->xConfig->getOption('core.debug.on'))
99
        {
100
            $sLanguage = $this->xConfig->getOption('core.language');
101
            $aJsFiles[] = $sJsLibUri . 'jaxon.debug' . $sJsExtension;
102
            $aJsFiles[] = $sJsLibUri . 'lang/jaxon.' . $sLanguage . $sJsExtension;
103
        }
104
        return $aJsFiles;
105
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
106
107
    /**
108
     * Get the mappings between previous and current config options
109
     *
110
     * @return array
111
     * @throws UriException
112
     */
113
    public function getOptionVars(): array
114
    {
115
        if(!$this->xConfig->hasOption('core.request.uri'))
116
        {
117
            $this->xConfig->setOption('core.request.uri', $this->xUriDetector->detect($_SERVER));
118
        }
119
        return [
120
            'sResponseType'         => 'JSON',
121
            'sVersion'              => $this->xConfig->getOption('core.version'),
122
            'sLanguage'             => $this->xConfig->getOption('core.language'),
123
            'bLanguage'             => $this->xConfig->hasOption('core.language'),
124
            'sRequestURI'           => $this->xConfig->getOption('core.request.uri'),
125
            'sDefaultMode'          => $this->xConfig->getOption('core.request.mode'),
126
            'sDefaultMethod'        => $this->xConfig->getOption('core.request.method'),
127
            'sCsrfMetaName'         => $this->xConfig->getOption('core.request.csrf_meta'),
128
            'bDebug'                => $this->xConfig->getOption('core.debug.on'),
129
            'bVerboseDebug'         => $this->xConfig->getOption('core.debug.verbose'),
130
            'sDebugOutputID'        => $this->xConfig->getOption('core.debug.output_id'),
131
            'nResponseQueueSize'    => $this->xConfig->getOption('js.lib.queue_size'),
132
            'sStatusMessages'       => $this->xConfig->getOption('js.lib.show_status') ? 'true' : 'false',
133
            'sWaitCursor'           => $this->xConfig->getOption('js.lib.show_cursor') ? 'true' : 'false',
134
            'sDefer'                => $this->xConfig->getOption('js.app.options', ''),
135
        ];
136
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
137
138
    /**
139
     * Get the javascript file name
140
     *
141
     * @return bool
142
     */
143
    public function shallCreateJsFiles(): bool
144
    {
145
        // Check config options
146
        // - The js.app.export option must be set to true
147
        // - The js.app.uri and js.app.dir options must be set to non null values
148
        if(!$this->xConfig->getOption('js.app.export', false) ||
149
            !$this->xConfig->getOption('js.app.uri', false) ||
150
            !$this->xConfig->getOption('js.app.dir', false))
151
        {
152
            return false;
153
        }
154
        return true;
155
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
156
157
    /**
158
     * Write javascript files and return the corresponding URI
159
     *
160
     * @param string $sHash
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
161
     * @param string $sJsCode
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
162
     *
163
     * @return string
164
     */
165
    public function createJsFiles(string $sHash, string $sJsCode): string
166
    {
167
        // Check dir access
168
        $sJsFileName = $this->xConfig->getOption('js.app.file', $sHash);
169
        $sJsDirectory = rtrim($this->xConfig->getOption('js.app.dir'), '/') . '/';
0 ignored issues
show
Bug introduced by
It seems like $this->xConfig->getOption('js.app.dir') can also be of type null; however, parameter $string of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        $sJsDirectory = rtrim(/** @scrutinizer ignore-type */ $this->xConfig->getOption('js.app.dir'), '/') . '/';
Loading history...
170
        // - The js.app.dir must be writable
171
        if(!$sJsFileName || !is_dir($sJsDirectory) || !is_writable($sJsDirectory))
172
        {
173
            return '';
174
        }
175
176
        $sOutFile = $sJsFileName . '.js';
177
        $sMinFile = $sJsFileName . '.min.js';
178
        if(!is_file($sJsDirectory . $sOutFile))
179
        {
180
            if(!file_put_contents($sJsDirectory . $sOutFile, $sJsCode))
181
            {
182
                return '';
183
            }
184
        }
185
        if(($this->xConfig->getOption('js.app.minify')) && !is_file($sJsDirectory . $sMinFile))
186
        {
187
            if(!$this->xMinifier->minify($sJsDirectory . $sOutFile, $sJsDirectory . $sMinFile))
188
            {
189
                return '';
190
            }
191
        }
192
193
        $sJsAppUri = rtrim($this->xConfig->getOption('js.app.uri'), '/') . '/';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
194
        $sJsExtension = $this->xConfig->getOption('js.app.minify') ? '.min.js' : '.js';
195
        return $sJsAppUri . $sJsFileName . $sJsExtension;
196
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
197
}
198