Completed
Pull Request — master (#72)
by Nate
03:35
created

CallAdapterProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 10 3
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Internal\CallAdapter;
10
11
use LogicException;
12
use Tebru\PhpType\TypeToken;
13
use Tebru\Retrofit\CallAdapter;
14
use Tebru\Retrofit\CallAdapterFactory;
15
16
/**
17
 * Class CallAdapterProvider
18
 *
19
 * Returns a [@see CallAdapterFactory] that can handle a given type
20
 *
21
 * @author Nate Brunette <[email protected]>
22
 */
23
final class CallAdapterProvider
24
{
25
    /**
26
     * @var CallAdapterFactory[]
27
     */
28
    private $callAdapterFactories;
29
30
    /**
31
     * Constructor
32
     *
33
     * @param CallAdapterFactory[] $callAdapterFactories
34
     */
35
    public function __construct(array $callAdapterFactories)
36
    {
37
        $this->callAdapterFactories = $callAdapterFactories;
38
    }
39
40
    /**
41
     * Given a type, find the first available [@see CallAdapterFactory] and return it
42
     *
43
     * @param TypeToken $type
44
     * @return CallAdapter
45
     * @throws \LogicException
46
     */
47
    public function get(TypeToken $type): CallAdapter
48
    {
49
        foreach ($this->callAdapterFactories as $callAdapterFactory) {
50
            if ($callAdapterFactory->supports($type)) {
51
                return $callAdapterFactory->create($type);
52
            }
53
        }
54
55
        throw new LogicException(sprintf('Retrofit: Could not get call adapter for type %s', $type));
56
    }
57
}
58