Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

PageFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Stitcher\Page;
4
5
use Stitcher\Exception\InvalidConfiguration;
6
use Stitcher\Variable\VariableParser;
7
8
class PageFactory
9
{
10
    private $variableParser;
11
12
    public function __construct(VariableParser $variableParser)
13
    {
14
        $this->variableParser = $variableParser;
15
    }
16
17
    public static function make(VariableParser $variableParser): PageFactory
18
    {
19
        return new self($variableParser);
20
    }
21
22
    public function create($value): Page
23
    {
24
        $id = $value['id'] ?? null;
25
        $template = $value['template'] ?? null;
26
        $variables = $value['variables'] ?? [];
27
28
        if (! $id || ! $template) {
29
            throw InvalidConfiguration::pageIdAndTemplateRequired();
30
        }
31
32
        foreach ($variables as $key => $variable) {
33
            $variables[$key] = $this->variableParser->parse($variable);
34
        }
35
36
        $page = Page::make($id, $template, $variables);
37
38
        return $page;
39
    }
40
}
41