Passed
Push — master ( 255049...bd130d )
by Robert
09:40 queued 11s
created

BackdropHeadlessClient::getParagraph()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 4
eloc 9
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 14
rs 9.9666
1
<?php
2
3
namespace Robertgarrigos\BackdropHeadlessClient;
4
5
use Illuminate\Support\Facades\Http;
6
use stdClass;
7
8
class BackdropHeadlessClient
9
{
10
    /**
11
     * Get a view
12
     *
13
     * @param String $view view's machine name
14
     * @param String $display_id view's display_id
15
     * @param String $args any additional arguments
16
     **/
17
    public function getView($view, $display_id, $args = null)
18
    {
19
        // TODO: check for trailing slash??
20
        $url = config('backdrop-headless-client.backdrop_api_server')
21
            . '/api/v2/views/'
22
            . $view . '/'
23
            . $display_id . '/'
24
            . $args;
25
26
        $response = Http::get($url)->throw();
27
28
29
        $view = $response->json();
30
31
        return $view;
32
    }
33
34
    /**
35
     * Get a mapped node
36
     *
37
     * @param String $type type of node
38
     * @param Int $id id of node
39
     **/
40
    public function getNode($type, $id)
41
    {
42
        $url = config('backdrop-headless-client.backdrop_api_server')
43
            . '/api/v2/node/'
44
            . $type
45
            . '/' . $id;
46
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_SL on line 46 at column 0
Loading history...
47
        $response = Http::get($url)->throw();
48
49
        $node = $response->json();
50
51
        if (config('backdrop-headless-client.node_types.' . $type) != null) {
52
                $mapped_node = $this->mapToNode($type, $node);
53
            }
54
            else {
55
                $mapped_node = $node;
56
            }
57
=======
58
        try {
59
            $response = $this->client->get($url);
60
        } catch (RequestException $e) {
61
            abort(404);
62
        }
63
        $node = json_decode($response->getBody()->getContents());
64
        if (null != config('backdrop-headless-client.node_types.' . $type)) {
65
            $mapped_node = $this->mapToNode($type, $node);
66
        } else {
67
            $mapped_node = $node;
68
        }
69
>>>>>>> 255049a96841e2f4aa7c207df8cf0e2c7292f4c2
70
71
        return $mapped_node;
72
    }
73
74
    /**
75
     * Get a term object
76
     *
77
     * @param String $vocabulary term's vocabulary
78
     * @param Int $id term's id
79
     **/
80
    public function getTerm($vocabulary, $id)
81
    {
82
        $url = config('backdrop-headless-client.backdrop_api_server')
83
            . '/api/'
84
            . $vocabulary
85
            . '/term/' . $id;
86
        $response = Http::get($url)->throw();
87
88
        $term = $response->json();
89
90
        return $term;
91
    }
92
93
    /**
94
     * Get a paragraphs item
95
     *
96
     * @param String $view view's machine name
97
     * @param String $display_id view's display_id
98
     * @param String $args any additional arguments
99
     **/
100
    public function getParagraph($type, $id)
101
    {
102
        $url = config('backdrop-headless-client.backdrop_api_server') .
103
            '/api/v2/paragraphs/' .
104
            $type . '/' .
105
            $id;
106
        $response = Http::get($url)->throw();
107
108
        $paragraph = $response->json();
109
110
        return $paragraph;
111
    }
112
113
    /**
114
     * Get a block item
115
     *
116
     * @param String $view view's machine name
117
     * @param String $display_id view's display_id
118
     * @param String $args any additional arguments
119
     **/
120
    public function getBlock($name)
121
    {
122
        $url = config('backdrop-headless-client.backdrop_api_server') .
123
            '/api/blocks/' .
124
            $name;
125
126
        $response = Http::get($url)->throw();
127
128
        $block = $response->json();
129
130
        return $block;
131
    }
132
133
134
    /**
135
     * Map backdrop node to the configured fields
136
     *
137
     * @param String $type Type of node
138
     * @param Object $node node to map
139
     * @return object
140
     **/
141
    public function mapToNode($type, $node)
142
    {
143
        $node_types = config('backdrop-headless-client.node_types');
144
        $mapped_node = new stdClass();
145
        if (isset($node_types[$type])) {
146
            foreach ($node_types[$type] as $field => $value) {
147
                if ($value['type'] == 'single') {
148
                    $p = implode('.', $value['properties']);
149
                    $mapped_node->$field = data_get($node, $p);
150
                }
151
                if ($value['type'] == 'multiple') {
152
                    $a = data_get($node, implode('.', $value['properties']));
153
                    if (is_array($a)) {
154
                        foreach ($a as $k => $v) {
155
                            $a2 = array_merge($value['properties'], array($k), $value['value']);
156
                            $mapped_node->$field[] = data_get($node, $a2);
157
                        }
158
                    }
159
                }
160
            }
161
        }
162
        return $mapped_node;
163
    }
164
165
}
166