Passed
Push — master ( bd130d...0a71bb )
by Robert
07:09 queued 04:40
created

BackdropHeadlessClient::getParagraph()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 1
eloc 7
c 3
b 0
f 1
nc 1
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
namespace Robertgarrigos\BackdropHeadlessClient;
4
5
use Illuminate\Support\Facades\Http;
0 ignored issues
show
Bug introduced by
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
        if (config('backdrop-headless-client.node_types.' . $type) != null) {
51
                $mapped_node = $this->mapToNode($type, $node);
52
            }
53
            else {
54
                $mapped_node = $node;
55
            }
56
57
        return $mapped_node;
58
    }
59
60
    /**
61
     * Get a term object
62
     *
63
     * @param String $vocabulary term's vocabulary
64
     * @param Int $id term's id
65
     **/
66
    public function getTerm($vocabulary, $id)
67
    {
68
        $url = config('backdrop-headless-client.backdrop_api_server')
69
            . '/api/'
70
            . $vocabulary
71
            . '/term/' . $id;
72
        $response = Http::get($url)->throw();
73
74
        $term = $response->json();
75
76
        return $term;
77
    }
78
79
    /**
80
     * Get a paragraphs item
81
     *
82
     * @param String $view view's machine name
83
     * @param String $display_id view's display_id
84
     * @param String $args any additional arguments
85
     **/
86
    public function getParagraph($type, $id)
87
    {
88
        $url = config('backdrop-headless-client.backdrop_api_server') .
89
            '/api/v2/paragraphs/' .
90
            $type . '/' .
91
            $id;
92
        $response = Http::get($url)->throw();
93
94
        $paragraph = $response->json();
95
96
        return $paragraph;
97
    }
98
99
    /**
100
     * Get a block item
101
     *
102
     * @param String $view view's machine name
103
     * @param String $display_id view's display_id
104
     * @param String $args any additional arguments
105
     **/
106
    public function getBlock($name)
107
    {
108
        $url = config('backdrop-headless-client.backdrop_api_server') .
109
            '/api/blocks/' .
110
            $name;
111
112
        $response = Http::get($url)->throw();
113
114
        $block = $response->json();
115
116
        return $block;
117
    }
118
119
120
    /**
121
     * Map backdrop node to the configured fields
122
     *
123
     * @param String $type Type of node
124
     * @param Object $node node to map
125
     * @return object
126
     **/
127
    public function mapToNode($type, $node)
128
    {
129
        $node_types = config('backdrop-headless-client.node_types');
130
        $mapped_node = new stdClass();
131
        if (isset($node_types[$type])) {
132
            foreach ($node_types[$type] as $field => $value) {
133
                if ($value['type'] == 'single') {
134
                    $p = implode('.', $value['properties']);
135
                    $mapped_node->$field = data_get($node, $p);
136
                }
137
                if ($value['type'] == 'multiple') {
138
                    $a = data_get($node, implode('.', $value['properties']));
139
                    if (is_array($a)) {
140
                        foreach ($a as $k => $v) {
141
                            $a2 = array_merge($value['properties'], array($k), $value['value']);
142
                            $mapped_node->$field[] = data_get($node, $a2);
143
                        }
144
                    }
145
                }
146
            }
147
        }
148
        return $mapped_node;
149
    }
150
151
}
152