Passed
Push — master ( c7d03d...66e4b0 )
by Oss
01:40
created

BatchContract::getContracts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @category    Brownie/BpmOnline
4
 * @author      Brownie <[email protected]>
5
 * @license     https://opensource.org/licenses/MIT
6
 */
7
8
namespace Brownie\BpmOnline\DataService\Contract;
9
10
use Brownie\BpmOnline\DataService\Contract;
11
use Brownie\BpmOnline\Exception\ValidateException;
12
use Brownie\BpmOnline\DataService\Response\BatchContract as ResponseBranchContract;
13
14
/**
15
 * BatchContract Data Contract.
16
 */
17
class BatchContract extends Contract
18
{
19
20
    /**
21
     * Contracts.
22
     *
23
     * @var Contract[]
24
     */
25
    private $contracts = [];
26
27
    /**
28
     * Sets the input values.
29
     *
30
     * BatchContract constructor.
31
     */
32 5
    public function __construct()
33
    {
34 5
        parent::__construct([
35 5
            'operationType' => Contract::BATCH,
36 5
            'contractType' => Contract::BATCH_QUERY,
37
        ]);
38 5
    }
39
40
    /**
41
     * Adding contract.
42
     *
43
     * @param Contract $contract Contract.
44
     *
45
     * @return self
46
     */
47 3
    public function addContract(Contract $contract)
48
    {
49 3
        $this->contracts[] = $contract;
50 3
        return $this;
51
    }
52
53
    /**
54
     * Returns contracts.
55
     *
56
     * @return Contract[]
57
     */
58 4
    private function getContracts()
59
    {
60 4
        return $this->contracts;
61
    }
62
63
    /**
64
     * Returns data as an associative array.
65
     *
66
     * @return array
67
     */
68 1
    public function toArray()
69
    {
70 1
        $items = [];
71 1
        foreach ($this->getContracts() as $index => $contract) {
72 1
            $items[] = array_merge(
73 1
                ['__type' => 'Terrasoft.Nui.ServiceModel.DataContract.' . $contract->getContractType()],
74 1
                $contract->toArray()
75
            );
76
        }
77
        return [
78 1
            'items' => $items
79
        ];
80
    }
81
82
    /**
83
     * Validates contract data, throws an exception in case of an error.
84
     *
85
     * @throws ValidateException
86
     */
87 2
    public function validate()
88
    {
89 2
        if (empty($this->getContracts())) {
90 1
            throw new ValidateException('Invalid contract arguments.');
91
        }
92 1
    }
93
94
    /**
95
     * Returns the response of the performance contract.
96
     *
97
     * @param string    $rawResponse    Raw response.
98
     *
99
     * @return ResponseBranchContract
100
     */
101 1
    public function getResponse($rawResponse)
102
    {
103 1
        return new ResponseBranchContract($rawResponse, $this->getContracts());
104
    }
105
}
106