Passed
Branch master (203e41)
by Nate
02:00
created

DefaultConverterFactory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 5
dl 0
loc 50
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A responseBodyConverter() 0 8 2
A requestBodyConverter() 0 8 2
A stringConverter() 0 8 2
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 Psr\Http\Message\StreamInterface;
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 DefaultConverterFactory
20
 *
21
 * Creates default converters
22
 *
23
 * @author Nate Brunette <[email protected]>
24
 */
25
final class DefaultConverterFactory implements ConverterFactory
26
{
27
    /**
28
     * Return default converter if type is a stream
29
     *
30
     * @param TypeToken $type
31
     * @return null|ResponseBodyConverter
32
     */
33 14
    public function responseBodyConverter(TypeToken $type): ?ResponseBodyConverter
34
    {
35 14
        if (!$type->isA(StreamInterface::class)) {
36 2
            return null;
37
        }
38
39 12
        return new DefaultResponseBodyConverter();
40
    }
41
42
    /**
43
     * Return default converter if type is a stream
44
     *
45
     * @param TypeToken $type
46
     * @return null|RequestBodyConverter
47
     */
48 5
    public function requestBodyConverter(TypeToken $type): ?RequestBodyConverter
49
    {
50 5
        if (!$type->isA(StreamInterface::class)) {
51 2
            return null;
52
        }
53
54 3
        return new DefaultRequestBodyConverter();
55
    }
56
57
    /**
58
     * Return default string converter for any type
59
     *
60
     * If the type is a string already, use a converter that doesn't do
61
     * any type checking.
62
     *
63
     * @param TypeToken $type
64
     * @return null|StringConverter
65
     */
66 12
    public function stringConverter(TypeToken $type): ?StringConverter
67
    {
68 12
        if ($type->isString()) {
69 4
            return new NoopStringConverter();
70
        }
71
72 10
        return new DefaultStringConverter();
73
    }
74
}
75