ConfigController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 63
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A beforeFilter() 0 6 1
A fetchApplications() 0 18 3
1
<?php
2
/**
3
 * BEdita, API-first content management framework
4
 * Copyright 2022 Atlas 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\Admin;
14
15
use Cake\Event\EventInterface;
16
use Cake\Http\Response;
17
use Cake\Utility\Hash;
18
19
/**
20
 * Config Controller
21
 *
22
 * @property \App\Controller\Component\PropertiesComponent $Properties
23
 */
24
class ConfigController extends AdministrationBaseController
25
{
26
    /**
27
     * Resource type in use
28
     *
29
     * @var string
30
     */
31
    protected $resourceType = 'config';
32
33
    /**
34
     * @inheritDoc
35
     */
36
    protected $readonly = false;
37
38
    /**
39
     * @inheritDoc
40
     */
41
    protected $properties = [
42
        'name' => 'string',
43
        'context' => 'string',
44
        'content' => 'json',
45
        'application_id' => 'applications',
46
    ];
47
48
    /**
49
     * @inheritDoc
50
     */
51
    protected $sortBy = 'name';
52
53
    /**
54
     * @inheritDoc
55
     */
56
    public function beforeFilter(EventInterface $event): ?Response
57
    {
58
        parent::beforeFilter($event);
59
        $this->set('applications', $this->fetchApplications());
60
61
        return null;
62
    }
63
64
    /**
65
     * Fetch applications
66
     *
67
     * @return array
68
     */
69
    public function fetchApplications(): array
70
    {
71
        $data = [];
72
        $pageCount = $page = 1;
73
        $pageSize = 100;
74
        $query = ['filter' => ['enabled' => 1], 'page_size' => $pageSize];
75
        while ($page <= $pageCount) {
76
            $response = (array)$this->apiClient->get('/admin/applications', $query + compact('page'));
77
            $applications = (array)Hash::combine($response['data'], '{n}.id', '{n}.attributes.name');
78
            $applications = array_flip($applications);
79
            $data = empty($data) ? $applications : array_merge($data, $applications);
80
            $pageCount = (int)Hash::get($response, 'meta.pagination.page_count');
81
            $page++;
82
        }
83
        ksort($data);
84
        $data = array_flip($data);
85
86
        return ['' => __('No application')] + $data;
87
    }
88
}
89