Completed
Pull Request — master (#16)
by Alessandro
06:12
created

Factory   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 6
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
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