Completed
Push — master ( 613b03...399412 )
by Stefano
16s queued 12s
created

Applications   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 4
eloc 15
c 3
b 2
f 0
dl 0
loc 45
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 19 3
A getName() 0 3 1
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
14
namespace App\Utility;
15
16
use BEdita\SDK\BEditaClientException;
17
use BEdita\WebTools\ApiClientProvider;
18
use Cake\Cache\Cache;
19
use Cake\Utility\Hash;
20
21
/**
22
 * Applications class, to get applications data
23
 */
24
class Applications
25
{
26
    /**
27
     * Applications
28
     *
29
     * @var array
30
     */
31
    public static $applications = null;
32
33
    /**
34
     * Get application name by application ID
35
     *
36
     * @param string $applicationId The application ID
37
     * @return string
38
     */
39
    public static function getName(string $applicationId): string
40
    {
41
        return (string)Hash::get(self::list(), $applicationId);
42
    }
43
44
    /**
45
     * Get applications, in the form [<id>: <name>].
46
     * This uses cache 'applications'.
47
     *
48
     * @return array
49
     */
50
    public static function list(): array
51
    {
52
        if (!empty(static::$applications)) {
53
            return static::$applications;
54
        }
55
        try {
56
            static::$applications = Cache::remember(
57
                CacheTools::cacheKey('applications'),
58
                function () {
59
                    $response = (array)ApiClientProvider::getApiClient()->get('applications');
60
61
                    return (array)Hash::combine($response, 'data.{n}.id', 'data.{n}.attributes.name');
62
                }
63
            );
64
        } catch (BEditaClientException $e) {
65
            return [];
66
        }
67
68
        return static::$applications;
69
    }
70
}
71