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

ParsingCompiler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 12
c 0
b 0
f 0
dl 0
loc 25
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A compile() 0 19 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hanneskod\readmetester\InputLanguage;
6
7
use hanneskod\readmetester\Compiler\CompilerInterface;
8
use hanneskod\readmetester\Example\CombinedExampleStore;
9
use hanneskod\readmetester\Example\ExampleStoreInterface;
10
use hanneskod\readmetester\Exception\InvalidInputException;
11
use hanneskod\readmetester\Exception\InvalidPhpCodeException;
12
13
final class ParsingCompiler implements CompilerInterface
14
{
15
    public function __construct(
16
        private ParserInterface $parser,
17
    ) {}
18
19
    public function compile(iterable $inputs): ExampleStoreInterface
20
    {
21
        $globalStore = new CombinedExampleStore();
22
23
        foreach ($inputs as $input) {
24
            try {
25
                $template = $this->parser->parseContent($input->getContents());
26
27
                $template->setDefaultNamespace($input->getName());
28
29
                $globalStore->addExampleStore($template->render());
30
            } catch (InvalidPhpCodeException $exception) {
31
                throw new InvalidInputException($exception->getMessage(), $input, $exception->getPhpCode());
32
            } catch (\Exception $exception) {
33
                throw new InvalidInputException($exception->getMessage(), $input, $exception->getMessage());
34
            }
35
        }
36
37
        return $globalStore;
38
    }
39
}
40