Passed
Push — master ( 1bd22b...d7c979 )
by Mariano
11:45
created

Factory::createPhiremockClient()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
eloc 7
dl 0
loc 9
rs 10
c 3
b 1
f 1
cc 1
nc 1
nop 2
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 GuzzleHttp\Client as GuzzleClient;
22
use Mcustiel\Phiremock\Client\Connection\Host;
23
use Mcustiel\Phiremock\Client\Connection\Port;
24
use Mcustiel\Phiremock\Client\Utils\Http\GuzzlePsr18Client;
25
use Mcustiel\Phiremock\Factory as PhiremockFactory;
26
use Psr\Http\Client\ClientInterface;
27
28
class Factory
29
{
30
    const CLIENT_CONFIG = [
31
        'http_errors' => false,
32
    ];
33
34
    /** @var PhiremockFactory */
35
    private $phiremockFactory;
36
37
    public function __construct(PhiremockFactory $factory)
38
    {
39
        $this->phiremockFactory = $factory;
40
    }
41
42
    public static function createDefault(): self
43
    {
44
        return new static(new PhiremockFactory());
45
    }
46
47
    public function createPhiremockClient(Host $host, Port $port): Phiremock
48
    {
49
        return new Phiremock(
50
            $host,
51
            $port,
52
            $this->createRemoteConnection(),
53
            $this->phiremockFactory->createV2UtilsFactory()->createExpectationToArrayConverter(),
54
            $this->phiremockFactory->createV2UtilsFactory()->createArrayToExpectationConverter(),
55
            $this->phiremockFactory->createV2UtilsFactory()->createScenarioStateInfoToArrayConverter()
56
        );
57
    }
58
59
    public function createRemoteConnection(): ClientInterface
60
    {
61
        if (!class_exists(GuzzleClient::class, true)) {
62
            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');
63
        }
64
        return new GuzzlePsr18Client();
65
    }
66
67
    protected function getPhiremockFactory(): PhiremockFactory
68
    {
69
        return $this->phiremockFactory;
70
    }
71
}
72