BatchContract::count()   A
last analyzed

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 6
    public function __construct()
33
    {
34 6
        parent::__construct([
35 6
            'operationType' => Contract::BATCH,
36 6
            'contractType' => Contract::BATCH_QUERY,
37
        ]);
38 6
    }
39
40
    /**
41
     * Adding contract.
42
     *
43
     * @param Contract $contract Contract.
44
     *
45
     * @return self
46
     */
47 4
    public function addContract(Contract $contract)
48
    {
49 4
        $this->contracts[] = $contract;
50 4
        return $this;
51
    }
52
53
    /**
54
     * Returns the number of added contracts.
55
     *
56
     * @return int
57
     */
58 1
    public function count()
59
    {
60 1
        return count($this->contracts);
61
    }
62
63
    /**
64
     * Returns contracts.
65
     *
66
     * @return Contract[]
67
     */
68 5
    private function getContracts()
69
    {
70 5
        return $this->contracts;
71
    }
72
73
    /**
74
     * Returns data as an associative array.
75
     *
76
     * @return array
77
     */
78 1
    public function toArray()
79
    {
80 1
        $items = [];
81 1
        foreach ($this->getContracts() as $index => $contract) {
82 1
            $items[] = array_merge(
83 1
                ['__type' => 'Terrasoft.Nui.ServiceModel.DataContract.' . $contract->getContractType()],
84 1
                $contract->toArray()
85
            );
86
        }
87
        return [
88 1
            'items' => $items
89
        ];
90
    }
91
92
    /**
93
     * Validates contract data, throws an exception in case of an error.
94
     *
95
     * @throws ValidateException
96
     */
97 3
    public function validate()
98
    {
99 3
        if (empty($this->getContracts())) {
100 1
            throw new ValidateException('Invalid contract arguments.');
101
        }
102 2
    }
103
104
    /**
105
     * Returns the response of the performance contract.
106
     *
107
     * @param string    $rawResponse    Raw response.
108
     *
109
     * @return ResponseBranchContract
110
     */
111 1
    public function getResponse($rawResponse)
112
    {
113 1
        return new ResponseBranchContract($rawResponse, $this->getContracts());
114
    }
115
}
116