Issues (1)

src/BackdropHeadlessClient.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Robertgarrigos\BackdropHeadlessClient;
4
5
use Illuminate\Support\Facades\Http;
0 ignored issues
show
The type Illuminate\Support\Facades\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
        $response = Http::get($url)->throw();
47
48
        $node = $response->json();
49
50
        $node = $this->getMappedNode($type, $node);
51
52
        return $node;
53
    }
54
55
    /**
56
     * Get a term object
57
     *
58
     * @param String $vocabulary term's vocabulary
59
     * @param Int $id term's id
60
     **/
61
    public function getTerm($vocabulary, $id)
62
    {
63
        $url = config('backdrop-headless-client.backdrop_api_server')
64
            . '/api/'
65
            . $vocabulary
66
            . '/term/' . $id;
67
        $response = Http::get($url)->throw();
68
69
        $term = $response->json();
70
71
        return $term;
72
    }
73
74
    /**
75
     * Get a paragraphs item
76
     *
77
     * @param Integer $id paragraphs' id
78
     **/
79
    public function getParagraph($id)
80
    {
81
        $url = config('backdrop-headless-client.backdrop_api_server') .
82
            '/api/v3/paragraphs/' .
83
            $id;
84
        $response = Http::get($url)->throw();
85
86
        $paragraph = $response->json();
87
88
        return $paragraph;
89
    }
90
91
    /**
92
     * Get a block item
93
     *
94
     * @param String $name block's machine name
95
     **/
96
    public function getBlock($name)
97
    {
98
        $url = config('backdrop-headless-client.backdrop_api_server') .
99
            '/api/blocks/' .
100
            $name;
101
102
        $response = Http::get($url)->throw();
103
104
        $block = $response->json();
105
106
        return $block;
107
    }
108
109
    /**
110
     * Get an item (node or taxonomy term) given a path alias
111
     *
112
     * @param String $path path
113
     **/
114
    public function getPath($path)
115
    {
116
        $url = config('backdrop-headless-client.backdrop_api_server') .
117
            '/api/router/' .
118
            $path;
119
120
        $response = Http::get($url)->throw();
121
122
        $item = $response->json();
123
        if (isset($item['nid'])) {
124
            $toMap = new stdClass;
125
            $toMap->{'#node'} = $item;
126
            # item is a node and should be mapped (or not)
127
            $item = $this->getMappedNode($item['type'], $toMap);
128
        }
129
130
        return $item;
131
    }
132
133
    /**
134
     * Map backdrop node to the configured fields
135
     *
136
     * @param String $type Type of node
137
     * @param Object $node node to map
138
     * @return object
139
     **/
140
    public function mapToNode($type, $node)
141
    {
142
        $node_types = config('backdrop-headless-client.node_types');
143
        $mapped_node = new stdClass();
144
        if (isset($node_types[$type])) {
145
            foreach ($node_types[$type] as $field => $value) {
146
                switch ($value['type']) {
147
                    case 'single':
148
                        $p = implode('.', $value['properties']);
149
                        $mapped_node->$field = data_get($node, $p);
150
                        break;
151
152
                    case 'multiple':
153
                        $a = data_get($node, implode('.', $value['properties']));
154
                        if (is_array($a)) {
155
                            foreach ($a as $k => $v) {
156
                                $a2 = array_merge($value['properties'], array($k), $value['value']);
157
                                $mapped_node->$field[] = data_get($node, $a2);
158
                            }
159
                        }
160
                        break;
161
                }
162
            }
163
        }
164
        return $mapped_node;
165
    }
166
167
    /**
168
     * return a mapped node (or not)
169
     *
170
     *
171
     * @param String $type type of node
172
     * @param Object $node node to map
173
     **/
174
    public function getMappedNode($type, $node)
175
    {
176
        if (config('backdrop-headless-client.node_types.' . $type) != null) {
177
            return $this->mapToNode($type, $node);
178
        } else {
179
            return $node;
180
        }
181
    }
182
}
183