AbstractImposterBuilder::getBuilder()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Meare\Juggler\Imposter\Builder;
4
5
use Meare\Juggler\Imposter\Imposter;
6
7
class AbstractImposterBuilder
8
{
9
    /**
10
     * @var array protocol-to-Builder map
11
     */
12
    protected $buildersMap = [
13
        Imposter::PROTOCOL_HTTP => HttpImposterBuilder::class,
14
    ];
15
16
    /**
17
     * Builds Imposter object from JSON contract using appropriate Builder
18
     *
19
     * @param string $json
20
     * @return \Meare\Juggler\Imposter\Imposter
21
     * @throws \InvalidArgumentException if contract has no protocol or no appropriate Builder found
22
     */
23 5
    public function build($json)
24
    {
25 5
        $contract = \GuzzleHttp\json_decode($json, true);
26 3
        if (!isset($contract['protocol'])) {
27 1
            throw new \InvalidArgumentException('Invalid contract; Protocol is not specified');
28
        }
29
30 2
        return $this->getBuilder($contract['protocol'])->build($contract);
31
    }
32
33
    /**
34
     * @param string $protocol
35
     * @return ImposterBuilder
36
     * @throws \InvalidArgumentException if no appropriate Builder found
37
     */
38 2
    protected function getBuilder($protocol)
39
    {
40 2
        if (!isset($this->buildersMap[$protocol])) {
41 1
            throw new \InvalidArgumentException("'$protocol' imposter objects are not supported");
42
        }
43
44 1
        return call_user_func([$this->buildersMap[$protocol], 'create']);
45
    }
46
}
47