Completed
Pull Request — master (#12)
by Chad
02:24 queued 01:06
created

Chef::deleteNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace TraderInteractive\Chef;
4
5
use Jenssegers\Chef\Chef as ChefApi;
6
7
class Chef
8
{
9
    /**
10
     * @var \Jenssegers\Chef\Chef The chef API
11
     */
12
    private $chef;
13
14
    /**
15
     * Construct the Chef Wrapper
16
     *
17
     * @param \Jenssegers\Chef\Chef $chef The chef API
18
     */
19
    public function __construct(ChefApi $chef)
20
    {
21
        $this->chef = $chef;
22
    }
23
24
    /**
25
     * Updates the data bag item overwriting existing fields with the data provided.
26
     *
27
     * @param string $name The name of the data bag.
28
     * @param string $item The name of the item in the data bag.
29
     * @param array $data Fields to update in the data bag with their data.
30
     *
31
     * @return void
32
     */
33
    public function patchDatabag($name, $item, array $data)
34
    {
35
        $itemUrl = '/data/' . rawurlencode($name) . '/' . rawurlencode($item);
36
        $data += (array)$this->chef->get($itemUrl);
37
38
        $this->chef->put($itemUrl, $data);
39
    }
40
41
    /**
42
     * Gets the list of nodes.
43
     *
44
     * @param string $role The role to query.  If unset, returns all nodes.
45
     *
46
     * @return array The names of the nodes registered in chef.
47
     */
48
    public function getNodes($role = null)
49
    {
50
        if ($role === null) {
51
            return array_keys((array)$this->chef->get('/nodes'));
52
        }
53
54
        $getNodeName = function ($node) {
55
            return isset($node->name) ? $node->name : null;
56
        };
57
58
        return array_filter(
59
            array_map(
60
                $getNodeName,
61
                (array)$this->chef->api('/search/node', 'GET', ['q' => "role:{$role}"])->rows
62
            )
63
        );
64
    }
65
66
    /**
67
     * Delete the given node.
68
     *
69
     * @param string $node The name of the node to delete.
70
     *
71
     * @return void
72
     */
73
    public function deleteNode($node)
74
    {
75
        $this->chef->delete('/nodes/' . rawurlencode($node));
76
    }
77
}
78