EndPointConfigurator::configure()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 10
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Parroauth2\Client\Factory;
4
5
use Parroauth2\Client\Client;
6
use Parroauth2\Client\ClientInterface;
7
use Parroauth2\Client\EndPoint\EndPointInterface;
8
9
/**
10
 * Configure endpoints for a client
11
 *
12
 * The configurator will check if the provider supports the endpoint, and then instantiate the endpoint
13
 */
14
final class EndPointConfigurator
15
{
16
    /**
17
     * @var array<string, class-string<EndPointInterface>|callable(ClientInterface):EndPointInterface>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-stri...ace):EndPointInterface> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<EndPointInterface>|callable(ClientInterface):EndPointInterface>.
Loading history...
18
     */
19
    private $endpoints;
20
21
22
    /**
23
     * EndPointConfigurator constructor.
24
     *
25
     * @param array<string, class-string<EndPointInterface>|callable(ClientInterface):EndPointInterface> $endpoints
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<string, class-stri...ace):EndPointInterface> at position 4 could not be parsed: Unknown type name 'class-string' at position 4 in array<string, class-string<EndPointInterface>|callable(ClientInterface):EndPointInterface>.
Loading history...
26
     *     Map of endpoint class name or factory, indexed by the endpoint name
27
     */
28 196
    public function __construct(array $endpoints)
29
    {
30 196
        $this->endpoints = $endpoints;
31 196
    }
32
33
    /**
34
     * Configure the endpoint on the client
35
     *
36
     * @param ClientInterface $client
37
     */
38 144
    public function configure(ClientInterface $client): void
39
    {
40 144
        foreach ($this->endpoints as $name => $endpoint) {
41 144
            if (!$client->provider()->supportsEndpoint($name)) {
42 4
                continue;
43
            }
44
45 144
            $endpoint = is_callable($endpoint) ? $endpoint($client) : new $endpoint($client);
46 144
            $client->endPoints()->add($endpoint);
47
        }
48 144
    }
49
}
50