Passed
Push — master ( 431a99...5fb423 )
by Mariano
04:28
created

Factory::createRemoteConnection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 6
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of Phiremock.
4
 *
5
 * Phiremock is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * Phiremock is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with Phiremock.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Mcustiel\Phiremock\Client;
20
21
use Exception;
22
use Mcustiel\Phiremock\Client\Connection\Host;
23
use Mcustiel\Phiremock\Client\Connection\Port;
24
use Mcustiel\Phiremock\Client\Connection\Scheme;
25
use Mcustiel\Phiremock\Client\Utils\Http\GuzzlePsr18Client;
26
use Mcustiel\Phiremock\Factory as PhiremockFactory;
27
use Psr\Http\Client\ClientInterface;
28
29
class Factory
30
{
31
    /** @var PhiremockFactory */
32
    private $phiremockFactory;
33
34
    public function __construct(PhiremockFactory $factory)
35
    {
36
        $this->phiremockFactory = $factory;
37
    }
38
39
    public static function createDefault(): self
40
    {
41
        return new static(new PhiremockFactory());
42
    }
43
44
    /** @throws Exception */
45
    public function createPhiremockClient(Host $host, Port $port, ?Scheme $scheme = null): Phiremock
46
    {
47
        return new Phiremock(
48
            $host,
49
            $port,
50
            $this->createRemoteConnection(),
51
            $this->phiremockFactory->createV2UtilsFactory()->createExpectationToArrayConverter(),
52
            $this->phiremockFactory->createV2UtilsFactory()->createArrayToExpectationConverter(),
53
            $this->phiremockFactory->createV2UtilsFactory()->createScenarioStateInfoToArrayConverter(),
54
            $scheme
55
        );
56
    }
57
58
    public function createSecurePhiremockClient(Host $host, Port $port): Phiremock
59
    {
60
        return new Phiremock(
61
            $host,
62
            $port,
63
            $this->createRemoteConnection(),
64
            $this->phiremockFactory->createV2UtilsFactory()->createExpectationToArrayConverter(),
65
            $this->phiremockFactory->createV2UtilsFactory()->createArrayToExpectationConverter(),
66
            $this->phiremockFactory->createV2UtilsFactory()->createScenarioStateInfoToArrayConverter(),
67
            Scheme::createHttps()
68
        );
69
    }
70
71
    /** @throws Exception */
72
    public function createRemoteConnection(): ClientInterface
73
    {
74
        if (!class_exists('\GuzzleHttp\Client', true)) {
75
            throw new Exception('A default http client implementation is needed. ' . 'Please extend the factory to return a PSR18-compatible HttpClient or install Guzzle Http Client v6');
76
        }
77
        return new GuzzlePsr18Client();
78
    }
79
80
    protected function getPhiremockFactory(): PhiremockFactory
81
    {
82
        return $this->phiremockFactory;
83
    }
84
}
85