Test Failed
Push — main ( 1d1810...62d3b7 )
by Dylan
02:46
created

Products::all()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Lifeboat\Services;
4
5
use Lifeboat\Exceptions\ApiException;
6
use Lifeboat\Exceptions\OAuthException;
7
use Lifeboat\Models\Product;
8
use Lifeboat\Resource\ListResource;
9
10
/**
11
 * Class Product
12
 * @package Lifeboat\Services
13
 */
14
class Products extends ApiService {
15
16
    const LIST_DETAILED     = 'detailed';
17
    const LIST_SIMPLE       = 'simple';
18
    const LIST_BARE         = 'bare';
19
    const SORT_TITLE_ASC    = 'title_az';
20
    const SORT_TITLE_DESC   = 'title_za';
21
    const SORT_SKU_ASC      = 'title_az';
22
    const SORT_SKU_DESC     = 'title_za';
23
    const SORT_PRICE_ASC    = 'title_az';
24
    const SORT_PRICE_DESC   = 'title_za';
25
    const SORT_CREATED_ASC  = 'title_az';
26
    const SORT_CREATED_DESC = 'title_za';
27
    const SORT_EDITED_ASC   = 'title_az';
28
    const SORT_EDITED_DESC  = 'title_za';
29
    
30
    /**
31
     * @param int $id
32
     * @return Product|null
33
     * @throws ApiException
34
     * @throws OAuthException
35
     */
36
    public function fetch(int $id): ?Product
37
    {
38
        /** @var Product|null $fetch */
39
        $fetch = $this->_get('api/products/product/' . $id);
40
        return $fetch;
41
    }
42
43
    /**
44
     * @param array $data
45
     * @return Product|null
46
     * @throws ApiException
47
     * @throws OAuthException
48
     */
49
    public function create(array $data): ?Product
50
    {
51
        /** @var Product|null $create */
52
        $create = $this->_post('api/products/product/', $data);
53
        return $create;
54
    }
55
56
    /**
57
     * @param int $id
58
     * @param array $data
59
     * @return Product|null
60
     * @throws ApiException
61
     * @throws OAuthException
62
     */
63
    public function update(int $id, array $data): ?Product
64
    {
65
        /** @var Product|null $post */
66
        $post = $this->_post('api/products/product' . $id, $data);
67
        return $post;
68
    }
69
70
    /**
71
     * @param string $search
72
     * @param string $sort
73
     * @param string $type
74
     * @return ListResource
75
     */
76
    public function all(
77
        string $search = '', 
78
        string $sort = self::SORT_CREATED_DESC, 
79
        string $type = self::LIST_BARE
80
    ): ListResource {
81
        return new ListResource($this->getClient(), 'api/products/all', [
82
            'search'    => $search,
83
            'sort'      => $sort,
84
            'data'      => $type
85
        ], 20);
86
    }
87
}
88