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

ParsingCompiler::compile()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
rs 9.9
cc 4
nc 8
nop 1
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