Completed
Push — master ( 743367...2eb0ee )
by Gabriel
06:30
created

Controller::doDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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 3
    protected function validateIsNotNewEntity(string $id)
20
    {
21 3
        if (!$id) {
22
            throw new CannotUpdateNewEntityException();
23
        }
24 3
    }
25
26 6
    protected function doCreate(string $path, $entity, callable $mappingFunction)
27
    {
28 6
        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 2
    protected function doDelete(string $path)
43
    {
44 2
        $this->requestHandler->delete($path);
45 2
        return true;
46
    }
47
48 7
    protected function doFetch(string $path, string $orderBy = null, string $order = self::ORDER_BY_ASC, int $limit = null, callable $mappingFunction)
49
    {
50 7
        $response = $this->requestHandler->get($path, [
51 7
            'order_by' => $orderBy,
52 7
            'order' => $order,
53 7
            'limit' => $limit
54
        ]);
55 7
        return $mappingFunction($response);
56
    }
57
58 2
    protected function doFetchOne(string $path, string $orderBy = null, string $order = self::ORDER_BY_ASC, callable $mappingFunction)
59
    {
60 2
        $response = $this->requestHandler->get($path, [
61 2
            'order_by' => $orderBy,
62 2
            'order' => $order,
63 2
            'limit' => 1
64
        ]);
65
        /** @var Collection $items */
66 2
        $items = $mappingFunction($response);
67 2
        if (count($items)) {
68 2
            return $items->first();
69
        }
70
        return null;
71
    }
72
73 1
    protected function doFindOneBy(string $path, array $criteria, string $orderBy = null, string $order = self::ORDER_BY_ASC, callable $mappingFunction)
74
    {
75 1
        $response = $this->requestHandler->get($path, [
76 1
            'criteria' => $criteria,
77 1
            'order_by' => $orderBy,
78 1
            'order' => $order,
79 1
            'limit' => 1
80
        ]);
81
        /** @var Collection $items */
82 1
        $items = $mappingFunction($response);
83 1
        if (count($items)) {
84 1
            return $items->first();
85
        }
86
        return null;
87
    }
88
}
89