Completed
Push — master ( 3c4917...c12914 )
by Gabriel
03:23
created

Controller::doUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
namespace Waredesk;
4
5
use Waredesk\Exceptions\CannotUpdateNewEntityException;
6
7
abstract class Controller
8
{
9
    protected const ORDER_BY_ASC = 'ASC';
10
    protected const ORDER_BY_DESC = 'DESC';
11
12
    protected $requestHandler;
13
14 18
    public function __construct(RequestHandler $requestHandler)
15
    {
16 18
        $this->requestHandler = $requestHandler;
17 18
    }
18
19 2
    protected function validateIsNotNewEntity(string $id)
20
    {
21 2
        if (!$id) {
22
            throw new CannotUpdateNewEntityException();
23
        }
24 2
    }
25
26 1
    protected function doCreate(string $path, $entity, callable $mappingFunction)
27
    {
28 1
        return $mappingFunction($this->requestHandler->post(
29
            $path,
30
            $entity
31
        ));
32
    }
33
34 1
    protected function doUpdate(string $path, $entity, callable $mappingFunction)
35
    {
36 1
        return $mappingFunction($this->requestHandler->update(
37
            $path,
38
            $entity
39
        ));
40
    }
41
42 1
    protected function doDelete(string $path)
43
    {
44 1
        $this->requestHandler->delete($path);
45 1
        return true;
46
    }
47
48 1
    protected function doFetch(string $path, callable $mappingFunction)
49
    {
50 1
        return $mappingFunction($this->requestHandler->get($path));
51
    }
52
53 2
    protected function doFetchOne(string $path, string $orderBy = null, string $order = self::ORDER_BY_ASC, callable $mappingFunction)
54
    {
55 2
        $response = $this->requestHandler->get($path, [
56 2
            'order_by' => $orderBy,
57 2
            'order' => $order,
58 2
            'limit' => 1
59
        ]);
60
        /** @var Collection $items */
61 2
        $items = $mappingFunction($response);
62 2
        if (count($items)) {
63 2
            return $items->first();
64
        }
65
        return null;
66
    }
67
}
68