Completed
Push — master ( aef33f...952551 )
by Gallice
51s queued 10s
created

EntityApi::addValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
ccs 3
cts 3
cp 1
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Tgallice\Wit;
4
5
use Tgallice\Wit\Model\EntityValue;
6
use Tgallice\Wit\Model\Entity;
7
8
class EntityApi
9
{
10
    use ResponseHandler;
11
12
    /**
13
     * @var Client
14
     */
15
    private $client;
16
17 10
    public function __construct(Client $client)
18
    {
19 10
        $this->client = $client;
20 10
    }
21
22
    /**
23
     * @param null|string $entityId
24
     *
25
     * @return mixed
26
     */
27 2
    public function get($entityId = null)
28
    {
29 2
        if (null !== $entityId) {
30 1
            $entityId = '/'.$entityId;
31 1
        }
32
33
34 2
        $response = $this->client->get(sprintf('/entities%s', $entityId));
35
36 2
        return $this->decodeResponse($response);
37
    }
38
39
    /**
40
     * @param string $entityId
41
     * @param EntityValue[] $entityValues
42
     * @param null|string $description
43
     * @param null|string $newId
44
     *
45
     * @return mixed
46
     */
47 1
    public function update($entityId, array $entityValues = [], $description = null, $newId = null)
48
    {
49 1
        $data = [];
50
51 1
        if (!empty($entityValues)) {
52 1
            $data['values'] = $entityValues;
53 1
        }
54
55 1
        if (!empty($description)) {
56 1
            $data['doc'] = $description;
57 1
        }
58
59 1
        if (!empty($newId)) {
60 1
            $data['id'] = $newId;
61 1
        }
62
63 1
        $response = $this->client->put(sprintf('/entities/%s', $entityId), $data);
64
65 1
        return $this->decodeResponse($response);
66
    }
67
68
    /**
69
     * @param string $entityId
70
     *
71
     * @return mixed
72
     */
73 1
    public function delete($entityId)
74
    {
75 1
        $response = $this->client->delete(sprintf('/entities/%s', $entityId));
76
77 1
        return $this->decodeResponse($response);
78
    }
79
80
    /**
81
     * @param string $entityId
82
     * @param string $entityValue
83
     * @return mixed
84
     */
85 1
    public function deleteValue($entityId, $entityValue)
86
    {
87 1
        $response = $this->client->delete(sprintf('/entities/%s/values/%s', $entityId, $entityValue));
88
89 1
        return $this->decodeResponse($response);
90
    }
91
92
    /**
93
     * @param string $entityId
94
     * @param EntityValue $entityValue
95
     *
96
     * @return mixed
97
     */
98 1
    public function addValue($entityId, EntityValue $entityValue)
99
    {
100 1
        $response = $this->client->send('POST', sprintf('/entities/%s/values', $entityId), $entityValue);
101
102 1
        return $this->decodeResponse($response);
103
    }
104
105
    /**
106
     * @param string $entityId
107
     * @param string $entityValue
108
     * @param string $expression
109
     *
110
     * @return mixed
111
     */
112 1
    public function addExpression($entityId, $entityValue, $expression)
113
    {
114 1
        $response = $this->client->post(sprintf('/entities/%s/values/%s/expressions', $entityId, $entityValue), [
115 1
            'expression' => $expression,
116 1
        ]);
117
118 1
        return $this->decodeResponse($response);
119
    }
120
121
    /**
122
     * @param string $entityId
123
     * @param string $entityValue
124
     * @param string $expression
125
     *
126
     * @return mixed
127
     */
128 1
    public function deleteExpression($entityId, $entityValue, $expression)
129
    {
130 1
        $response = $this->client->delete(sprintf('/entities/%s/values/%s/expressions/%s', $entityId, $entityValue, $expression));
131
132 1
        return $this->decodeResponse($response);
133
    }
134
135
    /**
136
     * @param string $entityId
137
     * @param EntityValue[] $entityValues
138
     * @param null|string $description
139
     * @param array $lookups
140
     *
141
     * @return mixed
142
     */
143 1
    public function create($entityId, array $entityValues = [], $description = null, $lookups = [Entity::LOOKUP_KEYWORDS])
144
    {
145
        $data = [
146 1
            'id' => $entityId,
147 1
            'values' => $entityValues,
148 1
            'doc' => $description,
149 1
            'lookups' => $lookups,
150 1
        ];
151
152 1
        $response = $this->client->post('/entities', $data);
153
154 1
        return $this->decodeResponse($response);
155
    }
156
}
157