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

CategoriesController::index()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
c 0
b 0
f 0
dl 0
loc 41
rs 9.488
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2021 ChannelWeb Srl, Chialab Srl
5
 *
6
 * This file is part of BEdita: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
12
 */
13
namespace App\Controller\Model;
14
15
use BEdita\SDK\BEditaClientException;
16
use Cake\Http\Response;
17
use Cake\Utility\Hash;
18
19
/**
20
 * Categories Model Controller: list, add, edit, remove categories
21
 *
22
 *
23
 * @property \App\Controller\Component\PropertiesComponent $Properties
24
 */
25
class CategoriesController extends ModelBaseController
26
{
27
    /**
28
     * Resource type currently used
29
     *
30
     * @var string
31
     */
32
    protected $resourceType = 'categories';
33
34
    /**
35
     * Single resource view exists
36
     *
37
     * @var bool
38
     */
39
    protected $singleView = false;
40
41
    /**
42
     * @inheritDoc
43
     */
44
    public function index(): ?Response
45
    {
46
        $this->request->allowMethod(['get']);
47
        $query = $this->request->getQueryParams() + [
48
            'page_size' => 100,
49
        ];
50
51
        try {
52
            $response = $this->apiClient->get('/model/categories', $query);
53
        } catch (BEditaClientException $e) {
54
            $this->log($e, 'error');
55
            $this->Flash->error($e->getMessage(), ['params' => $e]);
56
57
            return $this->redirect($this->referer());
58
        }
59
60
        $resources = (array)Hash::combine((array)$response['data'], '{n}.id', '{n}');
61
62
        $grouped = [
63
            '_' => [],
64
        ];
65
        foreach ($resources as $category) {
66
            if (empty($category['attributes']['parent_id'])) {
67
                $grouped['_'][] = $category['id'];
68
            } else {
69
                $grouped[$category['attributes']['parent_id']][] = $category['id'];
70
            }
71
        }
72
73
        $objectTypes = $this->Schema->objectTypesFeatures()['categorized'];
74
        $objectTypes = array_combine($objectTypes, $objectTypes);
75
76
        $this->set(compact('resources', 'grouped'));
77
        $this->set('object_types', $objectTypes);
78
        $this->set('meta', (array)$response['meta']);
79
        $this->set('links', (array)$response['links']);
80
        $this->set('schema', $this->Schema->getSchema());
81
        $this->set('properties', $this->Properties->indexList('categories'));
82
        $this->set('filter', $this->Properties->filterList('categories'));
83
84
        return null;
85
    }
86
}
87