Database::getAccessHosts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * DirectAdmin API Client
5
 * (c) Omines Internetbureau B.V. - https://omines.nl/
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Omines\DirectAdmin\Objects;
12
13
use Omines\DirectAdmin\Context\UserContext;
14
use Omines\DirectAdmin\Objects\Users\User;
15
16
/**
17
 * Database.
18
 *
19
 * @author Niels Keurentjes <[email protected]>
20
 */
21
class Database extends BaseObject
22
{
23
    const CACHE_ACCESS_HOSTS = 'access_hosts';
24
25
    /** @var User */
26
    private $owner;
27
28
    /** @var string */
29
    private $databaseName;
30
31
    /**
32
     * Database constructor.
33
     *
34
     * @param string $name Name of the database
35
     * @param User $owner Database owner
36
     * @param UserContext $context Context within which the object is valid
37
     */
38
    public function __construct($name, User $owner, UserContext $context)
39
    {
40
        parent::__construct($name, $context);
41
        $this->owner = $owner;
42
        $this->databaseName = $this->owner->getUsername() . '_' . $this->getName();
43
    }
44
45
    /**
46
     * Creates a new database under the specified user.
47
     *
48
     * @param User $user Owner of the database
49
     * @param string $name Database name, without <user>_ prefix
50
     * @param string $username Username to access the database with, without <user>_ prefix
51
     * @param string|null $password Password, or null if database user already exists
52
     * @return Database Newly created database
53
     */
54
    public static function create(User $user, $name, $username, $password)
55
    {
56
        $options = [
57
            'action' => 'create',
58
            'name' => $name,
59
        ];
60
        if (!empty($password)) {
61
            $options += ['user' => $username, 'passwd' => $password, 'passwd2' => $password];
62
        } else {
63
            $options += ['userlist' => $username];
64
        }
65
        $user->getContext()->invokeApiPost('DATABASES', $options);
66
        return new self($name, $user, $user->getContext());
67
    }
68
69
    /**
70
     * Deletes this database from the user.
71
     */
72
    public function delete()
73
    {
74
        $this->getContext()->invokeApiPost('DATABASES', [
75
            'action' => 'delete',
76
            'select0' => $this->getDatabaseName(),
77
        ]);
78
        $this->getContext()->getContextUser()->clearCache();
79
    }
80
81
    /**
82
     * @return Database\AccessHost[]
83
     */
84
    public function getAccessHosts()
85
    {
86
        return $this->getCache(self::CACHE_ACCESS_HOSTS, function () {
87
            $accessHosts = $this->getContext()->invokeApiGet('DATABASES', [
88
                'action' => 'accesshosts',
89
                'db' => $this->getDatabaseName(),
90
            ]);
91
92
            return array_map(function ($name) {
93
                return new Database\AccessHost($name, $this);
94
            }, $accessHosts);
95
        });
96
    }
97
98
    /**
99
     * @param string $name
100
     * @return Database\AccessHost
101
     */
102
    public function createAccessHost($name)
103
    {
104
        $accessHost = Database\AccessHost::create($this, $name);
105
        $this->getContext()->getContextUser()->clearCache();
106
        return $accessHost;
107
    }
108
109
    /**
110
     * @return string Name of the database
111
     */
112
    public function getDatabaseName()
113
    {
114
        return $this->databaseName;
115
    }
116
117
    /**
118
     * @return User
119
     */
120
    public function getOwner()
121
    {
122
        return $this->owner;
123
    }
124
}
125