StubBuilder::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Meare\Juggler\Imposter\Stub;
4
5
6
class StubBuilder
7
{
8
    /**
9
     * @var Response\ResponseFactory
10
     */
11
    private $responseFactory;
12
13
    /**
14
     * @var Predicate\PredicateFactory
15
     */
16
    private $predicateFactory;
17
18
    /**
19
     * @param Response\ResponseFactory   $responseFactory
20
     * @param Predicate\PredicateFactory $predicateFactory
21
     */
22 6
    public function __construct(Response\ResponseFactory $responseFactory, Predicate\PredicateFactory $predicateFactory)
23
    {
24 6
        $this->responseFactory = $responseFactory;
25 6
        $this->predicateFactory = $predicateFactory;
26 6
    }
27
28
    /**
29
     * @return StubBuilder
30
     */
31 2
    public static function create()
32
    {
33 2
        return new self(new Response\ResponseFactory, new Predicate\PredicateFactory);
34
    }
35
36
    /**
37
     * @param array $stub_contract
38
     * @return Stub
39
     */
40 5
    public function build(array $stub_contract)
41
    {
42 5
        if (!isset($stub_contract['responses'])) {
43 1
            throw new \InvalidArgumentException("Invalid contract: every stub should contain 'responses' field");
44
        }
45 4
        $responseObjects = $this->createResponses($stub_contract['responses']);
46 4
        $predicateObjects = $this->createPredicates(
47 4
            isset($stub_contract['predicates']) ? $stub_contract['predicates'] : []
48 4
        );
49
50 4
        return new Stub($responseObjects, $predicateObjects);
51
    }
52
53
    /**
54
     * @param array $response_contracts
55
     * @return Response\IResponse[]
56
     */
57 3
    private function createResponses(array $response_contracts)
58
    {
59 3
        $responseObjects = [];
60 3
        foreach ($response_contracts as $response_contract) {
61 3
            $type = key($response_contract);
62 3
            $contract = reset($response_contract);
63 3
            if (in_array($type, ['_behaviors'])) {
64 1
                continue;
65
            }
66
67 2
            $responseObjects[] = $this->responseFactory->createInstance($type, $contract);
68 3
        }
69
70 3
        return $responseObjects;
71
    }
72
73
    /**
74
     * @param array $predicate_contracts
75
     * @return Predicate\IPredicate[]
76
     */
77 3
    private function createPredicates(array $predicate_contracts)
78
    {
79 3
        $predicateObjects = [];
80 3
        foreach ($predicate_contracts as $predicate_contract) {
81 1
            $type = key($predicate_contract);
82 1
            $contract = reset($predicate_contract);
83
84 1
            $predicateObjects[] = $this->predicateFactory->createInstance($type, $contract);
85 3
        }
86
87 3
        return $predicateObjects;
88
    }
89
}