1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of phiremock-codeception-extension. |
5
|
|
|
* |
6
|
|
|
* phiremock-codeception-extension is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
9
|
|
|
* (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* phiremock-codeception-extension is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU General Public License |
17
|
|
|
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Mcustiel\Phiremock\Codeception\Util; |
21
|
|
|
|
22
|
|
|
use Codeception\Exception\ConfigurationException; |
23
|
|
|
use Mcustiel\Phiremock\Client\Factory; |
24
|
|
|
|
25
|
|
|
class FactoryClass |
26
|
|
|
{ |
27
|
|
|
/** @var class-string<Factory> */ |
|
|
|
|
28
|
|
|
private $factory; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string|class-string<Factory> $classNameConfig |
|
|
|
|
32
|
|
|
* @throws ConfigurationException |
33
|
|
|
*/ |
34
|
|
|
public function __construct(string $classNameConfig) |
35
|
|
|
{ |
36
|
|
|
$this->factory = $this->getClassNameFromConfig($classNameConfig); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getInstance(): Factory |
40
|
|
|
{ |
41
|
|
|
/** @var class-string<Factory> $factory */ |
42
|
|
|
$factory = $this->factory; |
43
|
|
|
return $factory::createDefault(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string|class-string<Factory> |
|
|
|
|
48
|
|
|
*/ |
49
|
|
|
public function asString(): string |
50
|
|
|
{ |
51
|
|
|
return $this->factory; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws ConfigurationException |
56
|
|
|
* @return string|class-string<Factory> |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
private function getClassNameFromConfig(string $className): string |
59
|
|
|
{ |
60
|
|
|
if ($className !== 'default') { |
61
|
|
|
if (!is_a($className, Factory::class, true)) { |
62
|
|
|
throw new ConfigurationException( |
63
|
|
|
sprintf('%s does not extend %s', $className, Factory::class) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
return $className; |
67
|
|
|
} |
68
|
|
|
return Factory::class; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|