AbstractImposterBuilder   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 9 2
A getBuilder() 0 8 2
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