Passed
Pull Request — main (#3)
by Daryl
02:28
created

Container::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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