Passed
Pull Request — master (#3)
by Mariano
01:24
created

FactoryClass   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 44
rs 10
c 2
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A asString() 0 3 1
A getInstance() 0 5 1
A getClassNameFromConfig() 0 11 3
1
<?php
2
3
namespace Mcustiel\Phiremock\Codeception\Util;
4
5
use Codeception\Exception\ConfigurationException;
6
use Mcustiel\Phiremock\Client\Factory;
7
8
class FactoryClass
9
{
10
    /** @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...
11
    private $factory;
12
13
    /**
14
     * @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...
15
     * @throws ConfigurationException
16
     */
17
    public function __construct(string $classNameConfig)
18
    {
19
        $this->factory = $this->getClassNameFromConfig($classNameConfig);
20
    }
21
22
    public function getInstance(): Factory
23
    {
24
        /** @var class-string<Factory> $factory */
25
        $factory = $this->factory;
26
        return $factory::createDefault();
27
    }
28
29
    /**
30
     * @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...
31
     */
32
    public function asString(): string
33
    {
34
        return $this->factory;
35
    }
36
37
    /**
38
     * @throws ConfigurationException
39
     * @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...
40
     */
41
    private function getClassNameFromConfig(string $className): string
42
    {
43
        if ($className !== 'default') {
44
            if (!is_a($className, Factory::class, true)) {
45
                throw new ConfigurationException(
46
                    sprintf('%s does not extend %s', $className, Factory::class)
47
                );
48
            }
49
            return $className;
50
        }
51
        return Factory::class;
52
    }
53
}
54