Site::create()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8657
c 0
b 0
f 0
cc 6
nc 6
nop 1
1
<?php
2
// Copyright 1999-2021. Plesk International GmbH.
3
4
namespace PleskX\Api\Operator;
5
6
use PleskX\Api\Struct\Site as Struct;
7
8
class Site extends \PleskX\Api\Operator
9
{
10
    const PROPERTIES_HOSTING = 'hosting';
11
12
    /**
13
     * @param array $properties
14
     *
15
     * @return Struct\Info
16
     */
17
    public function create(array $properties)
18
    {
19
        $packet = $this->_client->getPacket();
20
        $info = $packet->addChild($this->_wrapperTag)->addChild('add');
21
22
        $infoGeneral = $info->addChild('gen_setup');
23
        foreach ($properties as $name => $value) {
24
            if (!is_scalar($value)) {
25
                continue;
26
            }
27
            $infoGeneral->addChild($name, $value);
28
        }
29
30
        // set hosting properties
31
        if (isset($properties[static::PROPERTIES_HOSTING]) && is_array($properties[static::PROPERTIES_HOSTING])) {
32
            $hostingNode = $info->addChild('hosting')->addChild('vrt_hst');
33
            foreach ($properties[static::PROPERTIES_HOSTING] as $name => $value) {
34
                $propertyNode = $hostingNode->addChild('property');
35
                $propertyNode->addChild('name', $name);
36
                $propertyNode->addChild('value', $value);
37
            }
38
        }
39
40
        $response = $this->_client->request($packet);
41
42
        return new Struct\Info($response);
43
    }
44
45
    /**
46
     * @param string $field
47
     * @param int|string $value
48
     *
49
     * @return bool
50
     */
51
    public function delete($field, $value)
52
    {
53
        return $this->_delete($field, $value);
54
    }
55
56
    /**
57
     * @param string $field
58
     * @param int|string $value
59
     *
60
     * @return Struct\GeneralInfo
61
     */
62
    public function get($field, $value)
63
    {
64
        $items = $this->_getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
65
66
        return reset($items);
67
    }
68
69
    /**
70
     * @param string $field
71
     * @param int|string $value
72
     *
73
     * @return Struct\HostingInfo|null
74
     */
75
    public function getHosting($field, $value)
76
    {
77
        $items = $this->_getItems(Struct\HostingInfo::class, 'hosting', $field, $value, function ($node) {
78
            return isset($node->vrt_hst);
79
        });
80
81
        return empty($items) ? null : reset($items);
82
    }
83
84
    /**
85
     * @return Struct\GeneralInfo[]
86
     */
87
    public function getAll()
88
    {
89
        return $this->_getItems(Struct\GeneralInfo::class, 'gen_info');
90
    }
91
}
92