UpdateContract   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 131
ccs 37
cts 37
cp 1
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A addColumn() 0 4 1
A __construct() 0 6 1
A addFilter() 0 4 1
A getFilters() 0 3 1
A validate() 0 7 4
A getResponse() 0 3 1
A toArray() 0 31 4
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\ColumnExpression;
12
use Brownie\BpmOnline\DataService\Column\ColumnFilter;
13
use Brownie\BpmOnline\Exception\ValidateException;
14
use Brownie\BpmOnline\DataService\Response\UpdateContract as ResponseUpdateContract;
15
16
17
/**
18
 * UpdateContract Data Contract.
19
 */
20
class UpdateContract extends Contract
21
{
22
23
    /**
24
     * The collection of column values for the entry being added.
25
     *
26
     * @var array
27
     */
28
    private $dictionary = [];
29
30
    /**
31
     * Column filters.
32
     *
33
     * @var ColumnFilter[]
34
     */
35
    private $filters = [];
36
37
    /**
38
     * Sets the input values.
39
     *
40
     * @param string $rootSchemaName The name of the root schema object.
41
     */
42 9
    public function __construct($rootSchemaName)
43
    {
44 9
        parent::__construct([
45 9
            'rootSchemaName' => $rootSchemaName,
46 9
            'operationType' => Contract::UPDATE,
47 9
            'contractType' => Contract::UPDATE_QUERY,
48
        ]);
49 9
    }
50
51
    /**
52
     * Adding a query expression to a contract.
53
     *
54
     * @param string            $name               The name of the column.
55
     * @param ColumnExpression  $columnExpression   Query expression to the schema object.
56
     *
57
     * @return self
58
     */
59 1
    public function addColumn($name, ColumnExpression $columnExpression)
60
    {
61 1
        $this->dictionary[$name] = $columnExpression;
62 1
        return $this;
63
    }
64
65
    /**
66
     * Adding query filter to contract.
67
     *
68
     * @param ColumnFilter $columnFilter Column filter.
69
     *
70
     * @return self
71
     */
72 2
    public function addFilter(ColumnFilter $columnFilter)
73
    {
74 2
        $this->filters[] = $columnFilter;
75 2
        return $this;
76
    }
77
78
    /**
79
     * Returns filters.
80
     *
81
     * @return ColumnFilter[]
82
     */
83 4
    private function getFilters()
84
    {
85 4
        return $this->filters;
86
    }
87
88
    /**
89
     * Returns data as an associative array.
90
     *
91
     * @return array
92
     */
93 3
    public function toArray()
94
    {
95 3
        $dictionary = [];
96
        /**
97
         * @var ColumnExpression $columnExpression
98
         */
99 3
        foreach ($this->dictionary as $keyName => $columnExpression) {
100 1
            $dictionary[$keyName] = $columnExpression->toArray();
101
        }
102
103
        $data = [
104 3
            'RootSchemaName' => $this->getRootSchemaName(),
105 3
            'OperationType' => $this->getOperationType(),
106
            'IsForceUpdate' => false,
107
            'ColumnValues' => [
108 3
                'Items' => $dictionary
109
            ],
110
        ];
111
112 3
        if (!empty($this->getFilters())) {
113 1
            $items = [];
114 1
            foreach ($this->getFilters() as $key => $filter) {
115 1
                $items['index_' . $key] = $filter->toArray();
116
            }
117 1
            $data['Filters'] = [
118 1
                'FilterType' => ColumnFilter::FILTER_FILTER_GROUP,
119 1
                'Items' => $items
120
            ];
121
        }
122
123 3
        return $data;
124
    }
125
126
    /**
127
     * Validates contract data, throws an exception in case of an error.
128
     *
129
     * @throws ValidateException
130
     */
131 2
    public function validate()
132
    {
133 2
        if ((2 != $this->getOperationType()) || empty($this->getFilters())) {
134 1
            throw new ValidateException('Invalid contract arguments.');
135
        }
136 1
        foreach ($this->getFilters() as $filter) {
137 1
            $filter->validate();
138
        }
139 1
    }
140
141
    /**
142
     * Returns the response of the performance contract.
143
     *
144
     * @param string    $rawResponse    Raw response.
145
     *
146
     * @return ResponseUpdateContract
147
     */
148 1
    public function getResponse($rawResponse)
149
    {
150 1
        return new ResponseUpdateContract($rawResponse);
151
    }
152
}
153