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

Products::fetchOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Waredesk;
4
5
use Waredesk\Mappers\ProductMapper;
6
use Waredesk\Mappers\ProductsMapper;
7
use Waredesk\Models\Product;
8
9
class Products extends Controller
10
{
11
    private const ENDPOINT = '/v1-alpha/products';
12
13 3
    public function create(Product $product): Product
14
    {
15 3
        return $this->doCreate(
16 3
            self::ENDPOINT,
17
            $product,
18
            function ($response) use ($product) {
19 2
                return (new ProductMapper())->map($product, $response);
20 3
            }
21
        );
22
    }
23
24
    public function update(Product $product): Product
25
    {
26
        $this->validateIsNotNewEntity($product->getId());
27
        return $this->doUpdate(
28
            self::ENDPOINT."/{$product->getId()}",
29
            $product,
30
            function ($response) use ($product) {
31
                return (new ProductMapper())->map($product, $response);
32
            }
33
        );
34
    }
35
36 1
    public function fetch(string $orderBy = null, string $order = self::ORDER_BY_ASC, int $limit = null): Collections\Products
37
    {
38 1
        return $this->doFetch(
39 1
            self::ENDPOINT,
40
            $orderBy,
41
            $order,
42
            $limit,
43
            function ($response) {
44 1
                return (new ProductsMapper())->map(new Collections\Products(), $response);
45 1
            }
46
        );
47
    }
48
49
    public function fetchOne(string $orderBy = null, string $order = self::ORDER_BY_ASC): ? Product
50
    {
51
        return $this->doFetchOne(
52
            self::ENDPOINT,
53
            $orderBy,
54
            $order,
55
            function ($response) {
56
                return (new ProductsMapper())->map(new Collections\Products(), $response);
57
            }
58
        );
59
    }
60
61 1
    public function findOneBy(array $criteria, string $orderBy = null, string $order = self::ORDER_BY_ASC): ? Product
62
    {
63 1
        return $this->doFindOneBy(
64 1
            self::ENDPOINT,
65
            $criteria,
66
            $orderBy,
67
            $order,
68 1
            function ($response) {
69 1
                return (new ProductsMapper())->map(new Collections\Products(), $response);
70 1
            }
71
        );
72
    }
73
}
74