Issues (5)

src/ApiClientProvider.php (1 issue)

Labels
Severity
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2018 ChannelWeb 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 BEdita\WebTools;
16
17
use BEdita\SDK\BEditaClient;
18
use Cake\Core\Configure;
19
20
/**
21
 * BEdita API client provider singleton class.
22
 */
23
class ApiClientProvider
24
{
25
    use SingletonTrait;
0 ignored issues
show
The type BEdita\WebTools\SingletonTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
27
    /**
28
     * BEdita API client
29
     *
30
     * @var \BEdita\SDK\BEditaClient|null
31
     */
32
    private ?BEditaClient $apiClient = null;
33
34
    /**
35
     * Read singleton API client data.
36
     * In `$options` you may provide a log configuration via `Log` key setting a log file.
37
     * Example:
38
     * ```
39
     *   [
40
     *     'Log' => [
41
     *       'log_file' => LOGS . 'api.log',
42
     *     ],
43
     *   ]
44
     * ```
45
     *
46
     * @param array $options Client options
47
     * @return \BEdita\SDK\BEditaClient
48
     */
49
    public static function getApiClient(array $options = []): BEditaClient
50
    {
51
        if (static::getInstance()->apiClient) {
52
            $logOptions = !empty($options['Log']) ? $options['Log'] : Configure::read('API.log');
53
            if (!empty($logOptions)) {
54
                static::getInstance()->apiClient->initLogger($logOptions);
55
            }
56
57
            return static::getInstance()->apiClient;
58
        }
59
60
        return static::getInstance()->createClient($options);
61
    }
62
63
    /**
64
     * Create new default API client.
65
     *
66
     * @param mixed $options Client options
67
     * @return \BEdita\SDK\BEditaClient
68
     */
69
    protected function createClient(array $options = []): BEditaClient
70
    {
71
        $this->apiClient = new BEditaClient(
72
            Configure::read('API.apiBaseUrl'),
73
            Configure::read('API.apiKey'),
74
            [],
75
            Configure::read('API.guzzleConfig', [])
76
        );
77
        $logOptions = !empty($options['Log']) ? $options['Log'] : Configure::read('API.log');
78
        if (!empty($logOptions)) {
79
            $this->apiClient->initLogger($logOptions);
80
        }
81
82
        return $this->apiClient;
83
    }
84
85
    /**
86
     * Set a new API client.
87
     *
88
     * @param \BEdita\SDK\BEditaClient|null $client New API client to set
89
     * @return void
90
     */
91
    public static function setApiClient(?BEditaClient $client): void
92
    {
93
        static::getInstance()->apiClient = $client;
94
    }
95
}
96