Passed
Push — main ( ee6fb2...96b9cc )
by Thierry
05:15
created

LibraryHelper::config()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Jaxon\Dialogs\Dialog;
4
5
use Jaxon\Dialogs\DialogPlugin;
6
7
use function is_array;
8
use function rtrim;
9
10
class LibraryHelper
11
{
12
    /**
13
     * @param AbstractLibrary $xDialogLibrary
14
     * @param DialogPlugin $xDialogPlugin
15
     */
16
    public function __construct(private AbstractLibrary $xDialogLibrary,
17
        private DialogPlugin $xDialogPlugin)
18
    {}
19
20
    /**
21
     * Get the value of a config option
22
     *
23
     * @param string $sOptionName The option name
24
     * @param mixed $xDefault The default value, to be returned if the option is not defined
25
     *
26
     * @return mixed
27
     */
28
    private function getOption(string $sOptionName, $xDefault = null): mixed
29
    {
30
        return $this->xDialogPlugin->config()
31
            ->getOption($this->xDialogLibrary->getName() . ".$sOptionName", $xDefault);
32
    }
33
34
    /**
35
     * Check the presence of a config option
36
     *
37
     * @param string $sOptionName The option name
38
     *
39
     * @return bool
40
     */
41
    private function hasOption(string $sOptionName): bool
42
    {
43
        return $this->xDialogPlugin->config()
44
            ->hasOption($this->xDialogLibrary->getName() . ".$sOptionName");
45
    }
46
47
    /**
48
     * Get the library base URL
49
     *
50
     * @return string
51
     */
52
    public function getBaseUrl(): string
53
    {
54
        $sBaseUrl = $this->hasOption('uri') ?
55
            $this->getOption('uri') :
56
            $this->xDialogPlugin->config()->getOption('lib.uri',
57
                $this->xDialogLibrary->getBaseUrl());
58
        if($this->hasOption('subdir'))
59
        {
60
            $sBaseUrl = rtrim($sBaseUrl, '/') . '/' . $this->getOption('subdir');
61
        }
62
        if($this->hasOption('version'))
63
        {
64
            $sBaseUrl = rtrim($sBaseUrl, '/') . '/' . $this->getOption('version');
65
        }
66
        return $sBaseUrl;
67
    }
68
69
    /**
70
     * Get the options of the js library
71
     *
72
     * @return array
73
     */
74
    public function getJsOptions(): array
75
    {
76
        $xOptions = $this->getOption('options', []);
77
        return is_array($xOptions) ? $xOptions : [];
78
    }
79
}
80