Completed
Push — master ( 107c9c...401828 )
by Alexei
02:05
created

Site::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 5
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 5
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3
4
namespace PleskX\Api\Operator;
5
use PleskX\Api\Struct\Site as Struct;
6
7 View Code Duplication
class Site extends \PleskX\Api\Operator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
8
{
9
10
    /**
11
     * @param array $properties
12
     * @return Struct\Info
13
     */
14
    public function create(array $properties)
15
    {
16
        $packet = $this->_client->getPacket();
17
        $info = $packet->addChild('site')->addChild('add');
18
19
        $infoGeneral = $info->addChild('gen_setup');
20
        foreach ($properties as $name => $value) {
21
            $infoGeneral->addChild($name, $value);
22
        }
23
24
        $response = $this->_client->request($packet);
25
        return new Struct\Info($response);
26
    }
27
28
    /**
29
     * @param string $field
30
     * @param integer|string $value
31
     * @return bool
32
     */
33
    public function delete($field, $value)
34
    {
35
        $packet = $this->_client->getPacket();
36
        $packet->addChild('site')->addChild('del')->addChild('filter')->addChild($field, $value);
37
        $response = $this->_client->request($packet);
38
        return 'ok' === (string)$response->status;
39
    }
40
41
    /**
42
     * @param string $field
43
     * @param integer|string $value
44
     * @return Struct\GeneralInfo
45
     */
46
    public function get($field, $value)
47
    {
48
        $items = $this->getAll($field, $value);
49
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\Site\GeneralInfo|false adds false to the return on line 49 which is incompatible with the return type documented by PleskX\Api\Operator\Site::get of type PleskX\Api\Struct\Site\GeneralInfo. It seems like you forgot to handle an error condition.
Loading history...
50
    }
51
52
    /**
53
     * @return Struct\GeneralInfo[]
54
     */
55
    public function getAll($field = null, $value = null)
56
    {
57
        $packet = $this->_client->getPacket();
58
        $getTag = $packet->addChild('site')->addChild('get');
59
60
        $filterTag = $getTag->addChild('filter');
61
        if (!is_null($field)) {
62
            $filterTag->addChild($field, $value);
63
        }
64
65
        $getTag->addChild('dataset')->addChild('gen_info');
66
67
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
68
69
        $items = [];
70
        foreach ($response->xpath('//result') as $xmlResult) {
71
            $items[] = new Struct\GeneralInfo($xmlResult->data->gen_info);
72
        }
73
74
        return $items;
75
    }
76
77
}
78