FactoryClass::asString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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> */
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<Factory> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<Factory>.
Loading history...
28
    private $factory;
29
30
    /**
31
     * @param string|class-string<Factory> $classNameConfig
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
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>
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
48
     */
49
    public function asString(): string
50
    {
51
        return $this->factory;
52
    }
53
54
    /**
55
     * @throws ConfigurationException
56
     * @return string|class-string<Factory>
0 ignored issues
show
Documentation Bug introduced by
The doc comment string|class-string<Factory> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in string|class-string<Factory>.
Loading history...
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