ConverterProvider   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 51.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 69
loc 133
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getResponseBodyConverter() 23 23 4
A getRequestBodyConverter() 23 23 4
A getStringConverter() 23 23 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Converter;
10
11
use LogicException;
12
use Tebru\PhpType\TypeToken;
13
use Tebru\Retrofit\ConverterFactory;
14
use Tebru\Retrofit\RequestBodyConverter;
15
use Tebru\Retrofit\ResponseBodyConverter;
16
use Tebru\Retrofit\StringConverter;
17
18
/**
19
 * Class ConverterProvider
20
 *
21
 * Returns a [@see ConverterFactory] that matches the provided type
22
 *
23
 * @author Nate Brunette <[email protected]>
24
 */
25
final class ConverterProvider
26
{
27
    /**
28
     * A cache of [@see ResponseBodyConverter]'s
29
     *
30
     * @var ResponseBodyConverter[]
31
     */
32
    private $responseBodyConverters = [];
33
34
    /**
35
     * A cache of [@see RequestBodyConverter]'s
36
     *
37
     * @var RequestBodyConverter[]
38
     */
39
    private $requestBodyConverters = [];
40
41
    /**
42
     * A cache of [@see StringConverter]'s
43
     *
44
     * @var StringConverter[]
45
     */
46
    private $stringConverters = [];
47
48
    /**
49
     * An array of [@see ConverterFactory]'s
50
     *
51
     * @var ConverterFactory[]
52
     */
53
    private $converterFactories;
54
55
    /**
56
     * Constructor
57
     *
58
     * @param ConverterFactory[] $factories
59
     */
60 52
    public function __construct(array $factories)
61
    {
62 52
        $this->converterFactories = array_values($factories);
63 52
    }
64
65
    /**
66
     * Get a response body converter for type
67
     *
68
     * @param TypeToken $type
69
     * @return ResponseBodyConverter
70
     * @throws LogicException
71
     */
72 20 View Code Duplication
    public function getResponseBodyConverter(TypeToken $type): ResponseBodyConverter
73
    {
74 20
        $key = (string)$type;
75 20
        if (isset($this->responseBodyConverters[$key])) {
76 16
            return $this->responseBodyConverters[$key];
77
        }
78
79 20
        foreach ($this->converterFactories as $converterFactory) {
80 20
            $converter = $converterFactory->responseBodyConverter($type);
81 20
            if ($converter === null) {
82 2
                continue;
83
            }
84
85 19
            $this->responseBodyConverters[$key] = $converter;
86
87 19
            return $converter;
88
        }
89
90 1
        throw new LogicException(sprintf(
91 1
            'Retrofit: Could not get response body converter for type %s',
92 1
            $type
93
        ));
94
    }
95
96
    /**
97
     * Get a request body converter for type
98
     *
99
     * @param TypeToken $type
100
     * @return RequestBodyConverter
101
     * @throws \LogicException
102
     */
103 7 View Code Duplication
    public function getRequestBodyConverter(TypeToken $type): RequestBodyConverter
104
    {
105 7
        $key = (string)$type;
106 7
        if (isset($this->requestBodyConverters[$key])) {
107 1
            return $this->requestBodyConverters[$key];
108
        }
109
110 7
        foreach ($this->converterFactories as $converterFactory) {
111 7
            $converter = $converterFactory->requestBodyConverter($type);
112 7
            if ($converter === null) {
113 2
                continue;
114
            }
115
116 6
            $this->requestBodyConverters[$key] = $converter;
117
118 6
            return $converter;
119
        }
120
121 1
        throw new LogicException(sprintf(
122 1
            'Retrofit: Could not get request body converter for type %s',
123 1
            $type
124
        ));
125
    }
126
127
    /**
128
     * Get a string converter for type
129
     *
130
     * @param TypeToken $type
131
     * @return StringConverter
132
     * @throws \LogicException
133
     */
134 16 View Code Duplication
    public function getStringConverter(TypeToken $type): StringConverter
135
    {
136 16
        $key = (string)$type;
137 16
        if (isset($this->stringConverters[$key])) {
138 3
            return $this->stringConverters[$key];
139
        }
140
141 16
        foreach ($this->converterFactories as $converterFactory) {
142 16
            $converter = $converterFactory->stringConverter($type);
143 16
            if ($converter === null) {
144 1
                continue;
145
            }
146
147 15
            $this->stringConverters[$key] = $converter;
148
149 15
            return $converter;
150
        }
151
152 1
        throw new LogicException(sprintf(
153 1
            'Retrofit: Could not get string converter for type %s',
154 1
            $type
155
        ));
156
    }
157
}
158