Passed
Pull Request — master (#652)
by Edoardo
03:11
created

CategoriesController::index()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 28
c 0
b 0
f 0
dl 0
loc 43
rs 9.1608
cc 5
nc 7
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
18
/**
19
 * Categories Model Controller: list, add, edit, remove categories
20
 *
21
 *
22
 * @property \App\Controller\Component\PropertiesComponent $Properties
23
 */
24
class CategoriesController extends ModelBaseController
25
{
26
    /**
27
     * Resource type currently used
28
     *
29
     * @var string
30
     */
31
    protected $resourceType = 'categories';
32
33
    /**
34
     * Single resource view exists
35
     *
36
     * @var bool
37
     */
38
    protected $singleView = false;
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function index(): ?Response
44
    {
45
        $this->request->allowMethod(['get']);
46
        $query = $this->request->getQueryParams() + [
47
            'page_size' => 500,
48
        ];
49
50
        try {
51
            $response = $this->apiClient->get('/model/categories', $query);
52
        } catch (BEditaClientException $e) {
53
            $this->log($e, 'error');
54
            $this->Flash->error($e->getMessage(), ['params' => $e]);
55
56
            return $this->redirect(['_name' => 'dashboard']);
57
        }
58
59
        $resources = [];
60
        foreach ((array)$response['data'] as $category) {
61
            $resources[$category['id']] = $category;
62
        }
63
64
        $grouped = [
65
            '_' => [],
66
        ];
67
        foreach ($resources as $category) {
68
            if (empty($category['attributes']['parent_id'])) {
69
                $grouped['_'][] = $category['id'];
70
            } else {
71
                $grouped[$category['attributes']['parent_id']][] = $category['id'];
72
            }
73
        }
74
75
        $object_types = $this->Schema->objectTypesFeatures()['categorized'];
76
        $object_types = array_combine($object_types, $object_types);
77
78
        $this->set(compact('resources', 'grouped', 'object_types'));
79
        $this->set('meta', (array)$response['meta']);
80
        $this->set('links', (array)$response['links']);
81
        $this->set('schema', $this->Schema->getSchema());
82
        $this->set('properties', $this->Properties->indexList('categories'));
83
        $this->set('filter', $this->Properties->filterList('categories'));
84
85
        return null;
86
    }
87
}
88