|
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 Mcustiel\Phiremock\Client\Connection\Host; |
|
22
|
|
|
use Mcustiel\Phiremock\Client\Connection\Port; |
|
23
|
|
|
use Mcustiel\Phiremock\Client\Utils\Http\GuzzlePsr18Client; |
|
24
|
|
|
use Mcustiel\Phiremock\Client\Utils\Http\Scheme; |
|
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, ?Scheme $scheme = null): 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
|
|
|
$scheme |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function createSecurePhiremockClient(Host $host, Port $port): Phiremock |
|
61
|
|
|
{ |
|
62
|
|
|
return new Phiremock( |
|
63
|
|
|
$host, |
|
64
|
|
|
$port, |
|
65
|
|
|
$this->createRemoteConnection(), |
|
66
|
|
|
$this->phiremockFactory->createV2UtilsFactory()->createExpectationToArrayConverter(), |
|
67
|
|
|
$this->phiremockFactory->createV2UtilsFactory()->createArrayToExpectationConverter(), |
|
68
|
|
|
$this->phiremockFactory->createV2UtilsFactory()->createScenarioStateInfoToArrayConverter(), |
|
69
|
|
|
Scheme::createHttps() |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function createRemoteConnection(): ClientInterface |
|
74
|
|
|
{ |
|
75
|
|
|
if (!class_exists('\GuzzleHttp\Client', true)) { |
|
76
|
|
|
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'); |
|
77
|
|
|
} |
|
78
|
|
|
return new GuzzlePsr18Client(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getPhiremockFactory(): PhiremockFactory |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->phiremockFactory; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|