Completed
Pull Request — master (#16)
by Alessandro
01:35
created

Factory::createTransport()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 9
cts 9
cp 1
rs 8.9777
c 0
b 0
f 0
cc 6
nc 7
nop 0
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fazland\SkebbyRestClient\Transport;
6
7
use Fazland\SkebbyRestClient\Exception\RuntimeException;
8
use Fazland\SkebbyRestClient\Runtime\Runtime;
9
use Fazland\SkebbyRestClient\Runtime\RuntimeInterface;
10
use GuzzleHttp\Client;
11
use Http\Discovery\Exception\NotFoundException;
12
use Http\Discovery\Psr17FactoryDiscovery;
13
use Http\Discovery\Psr18ClientDiscovery;
14
15
/**
16
 * Transport Factory.
17
 */
18
class Factory
19
{
20
    /**
21
     * Creates the transport based on which classes are defined.
22
     *
23
     * @throws RuntimeException
24
     */
25 4
    public static function createTransport(?RuntimeInterface $runtime = null): TransportInterface
26
    {
27 4
        $runtime ??= new Runtime();
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected '='
Loading history...
28
29 3
        if ($runtime->classExists(Psr18ClientDiscovery::class) && $runtime->classExists(Psr17FactoryDiscovery::class)) {
30 2
            try {
31
                return new Psr7ClientTransport(
32
                    Psr18ClientDiscovery::find(),
33
                    Psr17FactoryDiscovery::findRequestFactory(),
34
                    Psr17FactoryDiscovery::findStreamFactory()
35 3
                );
36 1
            } catch (NotFoundException $e) {
37
                // @ignoreException
38
            }
39 2
        }
40 1
41
        if ($runtime->classExists(Client::class)) {
42
            return new Guzzle6Transport();
43 1
        }
44
45
        if ($runtime->extensionLoaded('curl')) {
46
            return new CurlExtensionTransport();
47
        }
48
49
        throw new RuntimeException('Cannot create an HTTP transport object');
50
    }
51
}
52