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

Chef   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A patchDatabag() 0 7 1
A getNodes() 0 17 3
A deleteNode() 0 4 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