Passed
Push — main ( f332df...379075 )
by Thierry
02:57
created

Package::getReadyScript()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 2
b 0
f 0
nc 4
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Lagdo\DbAdmin;
4
5
use Jaxon\Plugin\AbstractPackage;
6
use Lagdo\DbAdmin\Ajax\App\Admin;
7
8
use function realpath;
9
use function Jaxon\cl;
10
use function Jaxon\rq;
11
12
/**
13
 * Jaxon DbAdmin package
14
 */
15
class Package extends AbstractPackage
16
{
17
    /**
18
     * Get the path to the config file
19
     *
20
     * @return string|array
21
     */
22
    public static function config(): string
23
    {
24
        return realpath(__DIR__ . '/../config/config.php');
25
    }
26
27
    /**
28
     * Get the name of a given server
29
     *
30
     * @param string $server    The server name in the configuration
31
     *
32
     * @return string
33
     */
34
    public function getServerName(string $server): string
35
    {
36
        return $this->getOption("servers.$server.name", '');
37
    }
38
39
    /**
40
     * Get a given server options
41
     *
42
     * @param string $server    The server name in the configuration
43
     *
44
     * @return array
45
     */
46
    public function getServerOptions(string $server): array
47
    {
48
        return $this->getOption("servers.$server", []);
49
    }
50
51
    /**
52
     * Get the driver of a given server
53
     *
54
     * @param string $server    The server name in the configuration
55
     *
56
     * @return string
57
     */
58
    public function getServerDriver(string $server): string
59
    {
60
        return $this->getOption("servers.$server.driver", '');
61
    }
62
63
    /**
64
     * Check if the user has access to a server
65
     *
66
     * @param string $server      The database server
67
     *
68
     * return bool
69
     */
70
    public function getServerAccess(string $server): bool
71
    {
72
        // Check in server options
73
        $serverAccess = $this->getOption("servers.$server.access.server", null);
74
        if($serverAccess === true || $serverAccess === false)
75
        {
76
            return $serverAccess;
77
        }
78
        // Check in global options
79
        return $this->getOption('access.server', true) === true;
80
    }
81
82
    /**
83
     * Get the HTML tags to include CSS code and files into the page
84
     *
85
     * The code must be enclosed in the appropriate HTML tags.
86
     *
87
     * @return string
88
     */
89
    public function getCss(): string
90
    {
91
        return $this->view()->render('dbadmin::codes::css.html') . "\n<style>\n" .
92
            $this->view()->render('dbadmin::codes::styles.css') . "\n</style>\n";
93
    }
94
95
    /**
96
     * Get the HTML tags to include javascript code and files into the page
97
     *
98
     * The code must be enclosed in the appropriate HTML tags.
99
     *
100
     * @return string
101
     */
102
    public function getJs(): string
103
    {
104
        return $this->view()->render('dbadmin::codes::js.html');
105
    }
106
107
    /**
108
     * Get the javascript code to include into the page
109
     *
110
     * The code must NOT be enclosed in HTML tags.
111
     *
112
     * @return string
113
     */
114
    public function getScript(): string
115
    {
116
        return $this->view()->render('dbadmin::codes::script.js');
117
    }
118
119
    /**
120
     * Get the javascript code to include into the page
121
     *
122
     * The code must NOT be enclosed in HTML tags.
123
     *
124
     * @return string
125
     */
126
    public function getReadyScript(): string
127
    {
128
        $defaultServer = $this->getOption('default');
129
        return !$defaultServer ||
130
            !$this->getOption("servers.$defaultServer") ? '' :
131
                rq(Admin::class)->server($defaultServer);
132
    }
133
134
    /**
135
     * Get the HTML code of the package home page
136
     *
137
     * @return string
138
     */
139
    public function getHtml(): string
140
    {
141
        return cl(Admin::class)->html();
142
    }
143
}
144