Passed
Push — master ( 7f9295...bd7035 )
by Oss
01:56
created

UpdateContract::getResponse()   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
eloc 1
dl 0
loc 3
ccs 2
cts 2
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\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 filter.
32
     *
33
     * @var ColumnFilter
34
     */
35
    private $filter = null;
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->filter = $columnFilter;
75 2
        return $this;
76
    }
77
78
    /**
79
     * Returns data as an associative array.
80
     *
81
     * @return array
82
     */
83 3
    public function toArray()
84
    {
85 3
        $dictionary = [];
86
        /**
87
         * @var ColumnExpression $columnExpression
88
         */
89 3
        foreach ($this->dictionary as $keyName => $columnExpression) {
90 1
            $dictionary[$keyName] = $columnExpression->toArray();
91
        }
92 3
        $filter = [];
93 3
        if (!empty($this->filter)) {
94 1
            $filter = $this->filter->toArray();
95
        }
96
        return [
97 3
            'RootSchemaName' => $this->getRootSchemaName(),
98 3
            'OperationType' => $this->getOperationType(),
99
            'IsForceUpdate' => false,
100
            'ColumnValues' => [
101 3
                'Items' => $dictionary
102
            ],
103 3
            'Filters' => $filter
104
        ];
105
    }
106
107
    /**
108
     * Validates contract data, throws an exception in case of an error.
109
     *
110
     * @throws ValidateException
111
     */
112 2
    public function validate()
113
    {
114 2
        if ((2 != $this->getOperationType()) || empty($this->filter)) {
115 1
            throw new ValidateException('Invalid contract arguments.');
116
        }
117 1
        $this->filter->validate();
118 1
    }
119
120
    /**
121
     * Returns the response of the performance contract.
122
     *
123
     * @param string    $rawResponse    Raw response.
124
     *
125
     * @return ResponseUpdateContract
126
     */
127 1
    public function getResponse($rawResponse)
128
    {
129 1
        return new ResponseUpdateContract($rawResponse);
130
    }
131
}
132