DeleteContract::addFilter()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\DataService\Column\ColumnFilter;
12
use Brownie\BpmOnline\Exception\ValidateException;
13
use Brownie\BpmOnline\DataService\Response\DeleteContract as ResponseDeleteContract;
14
15
/**
16
 * DeleteContract Data Contract.
17
 */
18
class DeleteContract extends Contract
19
{
20
21
    /**
22
     * Column filter.
23
     *
24
     * @var ColumnFilter[]
25
     */
26
    private $filters = [];
27
28
    /**
29
     * Sets the input values.
30
     *
31
     * @param string $rootSchemaName The name of the root schema object.
32
     */
33 8
    public function __construct($rootSchemaName)
34
    {
35 8
        parent::__construct([
36 8
            'rootSchemaName' => $rootSchemaName,
37 8
            'operationType' => Contract::DELETE,
38 8
            'contractType' => Contract::DELETE_QUERY,
39
        ]);
40 8
    }
41
42
    /**
43
     * Adding query filter to contract.
44
     *
45
     * @param ColumnFilter $columnFilter Column filter.
46
     *
47
     * @return self
48
     */
49 2
    public function addFilter(ColumnFilter $columnFilter)
50
    {
51 2
        $this->filters[] = $columnFilter;
52 2
        return $this;
53
    }
54
55
    /**
56
     * Returns filters.
57
     *
58
     * @return ColumnFilter[]
59
     */
60 3
    private function getFilters()
61
    {
62 3
        return $this->filters;
63
    }
64
65
    /**
66
     * Returns data as an associative array.
67
     *
68
     * @return array
69
     */
70 2
    public function toArray()
71
    {
72
        $data = [
73 2
            'RootSchemaName' => $this->getRootSchemaName(),
74 2
            'OperationType' => $this->getOperationType(),
75
        ];
76
77 2
        $items = [];
78 2
        foreach ($this->getFilters() as $key => $filter) {
79 1
            $items['index_' . $key] = $filter->toArray();
80
        }
81
82 2
        $data['Filters'] = [
83 2
            'FilterType' => ColumnFilter::FILTER_FILTER_GROUP,
84 2
            'Items' => $items
85
        ];
86
87 2
        return $data;
88
    }
89
90
    /**
91
     * Validates contract data, throws an exception in case of an error.
92
     *
93
     * @throws ValidateException
94
     */
95 2
    public function validate()
96
    {
97 2
        if ((3 != $this->getOperationType()) || empty($this->getFilters())) {
98 1
            throw new ValidateException('Invalid contract arguments.');
99
        }
100 1
        foreach ($this->getFilters() as $filter) {
101 1
            $filter->validate();
102
        }
103 1
    }
104
105
    /**
106
     * Returns the response of the performance contract.
107
     *
108
     * @param string    $rawResponse    Raw response.
109
     *
110
     * @return ResponseDeleteContract
111
     */
112 1
    public function getResponse($rawResponse)
113
    {
114 1
        return new ResponseDeleteContract($rawResponse);
115
    }
116
}
117