Completed
Push — master ( 45ee30...d2ebdc )
by Bogdan
11s
created

ObjectSpecBuilder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 71
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A build() 0 15 3
B buildConcreteDefinition() 0 22 4
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/js-sandbox PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the
11
 * LICENSE file that was distributed with this source or visit
12
 * http://opensource.org/licenses/MIT
13
 */
14
15
16
namespace Pinepain\JsSandbox\Specs\Builder;
17
18
19
use Pinepain\JsSandbox\Specs\BindingSpecInterface;
20
use Pinepain\JsSandbox\Specs\Builder\Exceptions\BindingSpecBuilderException;
21
use Pinepain\JsSandbox\Specs\Builder\Exceptions\FunctionSpecBuilderException;
22
use Pinepain\JsSandbox\Specs\Builder\Exceptions\ObjectSpecBuilderException;
23
use Pinepain\JsSandbox\Specs\Builder\Exceptions\PropertySpecBuilderException;
24
use Pinepain\JsSandbox\Specs\FunctionSpecInterface;
25
use Pinepain\JsSandbox\Specs\PropertySpecInterface;
26
27
28
class ObjectSpecBuilder implements ObjectSpecBuilderInterface
29
{
30
    /**
31
     * @var PropertySpecBuilderInterface
32
     */
33
    private $property;
34
    /**
35
     * @var FunctionSpecBuilderInterface
36
     */
37
    private $function;
38
    /**
39
     * @var BindingSpecBuilderInterface
40
     */
41
    private $binding;
42
43 5
    public function __construct(PropertySpecBuilderInterface $property, FunctionSpecBuilderInterface $function, BindingSpecBuilderInterface $binding)
44
    {
45 5
        $this->property = $property;
46 5
        $this->function = $function;
47 5
        $this->binding  = $binding;
48 5
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 5
    public function build(array $definitions): array
54
    {
55 5
        $out = [];
56
57 5
        foreach ($definitions as $name => $definition) {
58 5
            if (!is_string($definition)) {
59 1
                $out[$name] = $definition;
60 1
                continue;
61
            }
62
63 4
            $out[$name] = $this->buildConcreteDefinition($name, $definition);
64
        }
65
66 4
        return $out;
67
    }
68
69
    /**
70
     * @param string $name
71
     * @param string $definition
72
     *
73
     * @return BindingSpecInterface|FunctionSpecInterface|PropertySpecInterface
74
     * @throws ObjectSpecBuilderException
75
     */
76 4
    protected function buildConcreteDefinition(string $name, string $definition)
77
    {
78
        try {
79 4
            return $this->property->build($definition);
80 3
        } catch (PropertySpecBuilderException $e) {
81
            //
82
        }
83
84
        try {
85 3
            return $this->function->build($definition);
86 2
        } catch (FunctionSpecBuilderException $e) {
87
            //
88
        }
89
90
        try {
91 2
            return $this->binding->build($definition);
92 1
        } catch (BindingSpecBuilderException $e) {
93
            //
94
        }
95
96 1
        throw new ObjectSpecBuilderException("Unable to build spec for '{$name}' from definition '{$definition}'");
97
    }
98
}
99