Passed
Pull Request — master (#652)
by Edoardo
02:48
created

CategoriesComponent::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
1
<?php
2
namespace App\Controller\Component;
3
4
use BEdita\WebTools\ApiClientProvider;
5
use Cake\Controller\Component;
6
use Cake\Utility\Hash;
7
8
/**
9
 * Categories component
10
 */
11
class CategoriesComponent extends Component
12
{
13
    /**
14
     * Fetch categories list.
15
     *
16
     * @param string|null $objectType The object type filter for categories.
17
     * @param array|null $options Query options.
18
     * @return array The BEdita API response for the categories list.
19
     */
20
    public function index(?string $objectType = null, ?array $options = [])
21
    {
22
        $apiClient = ApiClientProvider::getApiClient();
23
24
        $options = $options + [
25
            'page_size' => 100,
26
        ];
27
        if (!empty($objectType)) {
28
            $options['filter'] = $options['filter'] ?? [];
29
            $options['filter']['type'] = $objectType;
30
        }
31
32
        return $apiClient->get('/model/categories', $options);
33
    }
34
35
    /**
36
     * Create a key/value map of categories from the BEdita categories list response.
37
     *
38
     * @param array $response The BEdita API response for the categories list.
39
     * @return array A map with the category ids as keys and the category attributes as values.
40
     */
41
    public function map(?array $response)
42
    {
43
        return (array)Hash::combine((array)$response['data'], '{n}.id', '{n}');
44
    }
45
46
    /**
47
     * Create an id-based categories tree.
48
     *
49
     * @param array $map The categories map returned by the map function.
50
     * @return array The categories tree.
51
     */
52
    public function tree(?array $map)
53
    {
54
        $tree = [
55
            '_' => [],
56
        ];
57
        foreach ($map as $category) {
58
            if (empty($category['attributes']['parent_id'])) {
59
                $tree['_'][] = $category['id'];
60
            } else {
61
                $tree[$category['attributes']['parent_id']][] = $category['id'];
62
            }
63
        }
64
65
        return $tree;
66
    }
67
68
    /**
69
     * Save a category using the `/model/` API.
70
     *
71
     * @param array $data Data to save.
72
     * @return array The BEdita API response for the saved category.
73
     */
74
    public function save(array $data)
75
    {
76
        $id = Hash::get($data, 'id');
77
        unset($data['id']);
78
        $body = [
79
            'data' => [
80
                'type' => 'categories',
81
                'attributes' => $data,
82
            ],
83
        ];
84
85
        $apiClient = ApiClientProvider::getApiClient();
86
        $endpoint = sprintf('/model/%s', 'categories');
87
        if (empty($id)) {
88
            return $apiClient->post($endpoint, json_encode($body));
89
        }
90
91
        $body['data']['id'] = $id;
92
93
        return $apiClient->patch(sprintf('%s/%s', $endpoint, $id), json_encode($body));
94
    }
95
96
    /**
97
     * Delete a category using the `/model/` API.
98
     *
99
     * @param string|int $id The category id to delete.
100
     * @return array The BEdita API response for the deleted category.
101
     */
102
    public function delete(string $id)
103
    {
104
        $apiClient = ApiClientProvider::getApiClient();
105
106
        return $apiClient->delete(sprintf('/model/%s/%s', 'categories', $id));
107
    }
108
}
109