Completed
Push — master ( ff3b47...62a00e )
by Alberto
21s queued 14s
created

SchemaTrait::getMeta()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 1
dl 0
loc 20
rs 9.9332
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2023 Atlas Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace App\Utility;
16
17
use Authentication\Identity;
18
use BEdita\SDK\BEditaClientException;
19
use BEdita\WebTools\ApiClientProvider;
20
use Cake\Cache\Cache;
21
use Cake\Log\LogTrait;
22
use Cake\Utility\Hash;
23
use Psr\Log\LogLevel;
24
25
trait SchemaTrait
26
{
27
    use LogTrait;
28
29
    /**
30
     * Getter for home endpoint metadata from user identity.
31
     *
32
     * @param \Authentication\Identity $user User identity.
33
     * @return array
34
     */
35
    public function getMeta(Identity $user): array
36
    {
37
        try {
38
            $home = Cache::remember(
39
                sprintf('home_%d', $user->get('id')),
40
                function () {
41
                    $client = ApiClientProvider::getApiClient();
42
43
                    return $client->get('/home');
44
                }
45
            );
46
        } catch (BEditaClientException $e) {
47
            // Something bad happened. Returning an empty array instead.
48
            // The exception is being caught _outside_ of `Cache::remember()` to avoid caching the fallback.
49
            $this->log($e->getMessage(), LogLevel::ERROR);
50
51
            return [];
52
        }
53
54
        return (array)Hash::get($home, 'meta');
55
    }
56
}
57