Passed
Pull Request — master (#47)
by
unknown
11:29
created

CompilerFactoryFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 23
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A createCompilerFactory() 0 17 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\Compiler;
6
7
use hanneskod\readmetester\Config\Configs;
8
use hanneskod\readmetester\Exception\InstantiatorException;
9
use hanneskod\readmetester\Exception\InvalidInputLanguageException;
10
use hanneskod\readmetester\Utils\Instantiator;
11
12
class CompilerFactoryFactory
13
{
14
    public function __construct(
15
        private Instantiator $instantiator,
16
    ) {}
17
18
    public function createCompilerFactory(string $id): CompilerFactoryInterface
19
    {
20
        try {
21
            $factory = $this->instantiator->getNewObject(
22
                Configs::expand(Configs::INPUT_ID, $id),
23
            );
24
        } catch (InstantiatorException $exception) {
25
            throw new InvalidInputLanguageException("Invalid input language $id: {$exception->getMessage()}");
26
        }
27
28
        if (!$factory instanceof CompilerFactoryInterface) {
29
            throw new InvalidInputLanguageException(
30
                "Invalid input language $id: Class does not implement CompilerFactoryInterface"
31
            );
32
        }
33
34
        return $factory;
35
    }
36
}
37