Passed
Pull Request — main (#7)
by Daryl
02:27
created

Container::createApi()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 1
nop 0
dl 0
loc 16
ccs 14
cts 14
cp 1
crap 2
rs 9.8666
c 0
b 0
f 0
1
<?php
2
3
namespace Clubdeuce\Tessitura\Base;
4
5
use Clubdeuce\Tessitura\Helpers;
6
use Clubdeuce\Tessitura\Interfaces\ApiInterface;
7
use Clubdeuce\Tessitura\Interfaces\CacheInterface;
8
use Clubdeuce\Tessitura\Interfaces\ResourceInterface;
9
use Clubdeuce\Tessitura\Resources;
10
use GuzzleHttp\Client;
11
use GuzzleHttp\ClientInterface;
12
use Psr\Log\LoggerInterface;
13
use Psr\Log\NullLogger;
14
15
/**
16
 * Class Container
17
 * @package Clubdeuce\Tessitura
18
 *
19
 * A simple service container for managing dependencies.
20
 */
21
class Container
22
{
23
    /**
24
     * @var array
25
     */
26
    private array $services = [];
27
28
    /**
29
     * @var array
30
     */
31
    private array $parameters = [];
32
33
    /**
34
     * Container constructor.
35
     *
36
     * @param array $parameters Configuration parameters
37
     */
38 17
    public function __construct(array $parameters = [])
39
    {
40 17
        $this->parameters = $parameters;
41
    }
42
43
    /**
44
     * Get a service from the container.
45
     *
46
     * @param string $id The service ID
47
     * @return mixed The service instance
48
     * @throws \Exception If the service is not found
49
     */
50 12
    public function get(string $id): mixed
51
    {
52 12
        if (!isset($this->services[$id])) {
53 9
            $this->services[$id] = $this->createService($id);
54
        }
55
56 10
        return $this->services[$id];
57
    }
58
59
    /**
60
     * Set a service in the container.
61
     *
62
     * @param string $id The service ID
63
     * @param mixed $service The service instance
64
     * @return void
65
     */
66 5
    public function set(string $id, mixed $service): void
67
    {
68 5
        $this->services[$id] = $service;
69
    }
70
71
    /**
72
     * Get a parameter from the container.
73
     *
74
     * @param string $name The parameter name
75
     * @param mixed $default The default value if the parameter is not found
76
     * @return mixed The parameter value
77
     */
78 11
    public function getParameter(string $name, mixed $default = null): mixed
79
    {
80 11
        return $this->parameters[$name] ?? $default;
81
    }
82
83
    /**
84
     * Set a parameter in the container.
85
     *
86
     * @param string $name The parameter name
87
     * @param mixed $value The parameter value
88
     * @return void
89
     */
90 2
    public function setParameter(string $name, mixed $value): void
91
    {
92 2
        $this->parameters[$name] = $value;
93
    }
94
95
    /**
96
     * Check if a service exists in the container.
97
     *
98
     * @param string $id The service ID
99
     * @return bool True if the service exists, false otherwise
100
     */
101 1
    public function has(string $id): bool
102
    {
103 1
        return isset($this->services[$id]) || method_exists($this, 'create' . ucfirst($id));
104
    }
105
106
    /**
107
     * Create a service.
108
     *
109
     * @param string $id The service ID
110
     * @return mixed The service instance
111
     * @throws \Exception If the service cannot be created
112
     */
113 9
    private function createService(string $id): mixed
114
    {
115
        switch ($id) {
116 9
            case 'http_client':
117 4
                return $this->createHttpClient();
118 5
            case 'logger':
119 1
                return $this->createLogger();
120 4
            case 'api':
121 1
                return $this->createApi();
122 3
            case 'performances':
123 1
                return $this->createPerformances();
124
            default:
125 2
                throw new \Exception(sprintf('Service "%s" not found', $id));
126
        }
127
    }
128
129
    /**
130
     * Create an HTTP client.
131
     *
132
     * @return ClientInterface
133
     */
134 4
    private function createHttpClient(): ClientInterface
135
    {
136 4
        $baseRoute = $this->getParameter('base_route', '');
137
        
138 4
        return new Client([
139 4
            'baseRoute' => $baseRoute,
140 4
            'timeout'   => $this->getParameter('timeout', 10.0),
141 4
        ]);
142
    }
143
144
    /**
145
     * Create a logger.
146
     *
147
     * @return LoggerInterface
148
     */
149 1
    private function createLogger(): LoggerInterface
150
    {
151
        // Use a custom logger if provided, otherwise use NullLogger
152 1
        return $this->getParameter('logger', new NullLogger());
153
    }
154
155
    /**
156
     * Create an API client.
157
     *
158
     * @return ApiInterface
159
     */
160 1
    private function createApi(): ApiInterface
161
    {
162 1
        $args = [
163 1
            'base_route' => $this->getParameter('base_route', ''),
164 1
            'machine' => $this->getParameter('machine', ''),
165 1
            'password' => $this->getParameter('password', ''),
166 1
            'usergroup' => $this->getParameter('usergroup', ''),
167 1
            'username' => $this->getParameter('username', ''),
168 1
            'version' => $this->getParameter('version', '16'),
169 1
        ];
170
171 1
        return new Helpers\Api(
172 1
            $args,
173 1
            $this->get('http_client'),
174 1
            $this->get('logger'),
175 1
            $this->has('cache') ? $this->get('cache') : null
176 1
        );
177
    }
178
179
    /**
180
     * Create a Performances resource.
181
     *
182
     * @return ResourceInterface
183
     */
184 1
    private function createPerformances(): ResourceInterface
185
    {
186 1
        return new Resources\Performances($this->get('api'));
187
    }
188
}