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

CallAdapterProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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