ProductsService   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 15
c 2
b 0
f 0
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getById() 0 5 1
A getByBarcode() 0 7 1
A getAll() 0 15 2
1
<?php
2
3
4
namespace Dolibarr\Client\Service;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
use Dolibarr\Client\Domain\Common\Barcode;
8
use Dolibarr\Client\Domain\Product\Product;
9
use Dolibarr\Client\Domain\Product\ProductId;
10
use Dolibarr\Client\Domain\Product\Type;
11
use Dolibarr\Client\Domain\Resource\ApiResource;
12
use Dolibarr\Client\Exception\ApiException;
13
use Dolibarr\Client\HttpClient\HttpClientInterface;
14
use JMS\Serializer\SerializerInterface;
15
16
/**
17
 * @package Dolibarr\Client\Service
18
 */
19
final class ProductsService extends AbstractService
20
{
21
22
    /**
23
     * @param HttpClientInterface $httpClient
24
     * @param SerializerInterface $serializerInterface
25
     */
26
    public function __construct(HttpClientInterface $httpClient, SerializerInterface $serializerInterface)
27
    {
28
        parent::__construct($httpClient, $serializerInterface, new ApiResource('products'));
29
    }
30
31
    /**
32
     * @param Barcode $barcode
33
     *
34
     * @return ArrayCollection|Product[]
35
     *
36
     * @throws ApiException
37
     */
38
    public function getByBarcode(Barcode $barcode)
39
    {
40
        $resources = $this->getList(['query' => [
41
            'sqlfilters' => 'barcode='.$barcode->barcode()
42
        ]]);
43
44
        return $this->deserializeCollection($resources, Product::class);
45
    }
46
47
    /**
48
     * @param ProductId $productId
49
     *
50
     * @return Product
51
     *
52
     * @throws ApiException
53
     */
54
    public function getById(ProductId $productId)
55
    {
56
        $resource = $this->get($productId->getId());
57
58
        return $this->deserialize($resource, Product::class);
59
    }
60
61
    /**
62
     * @param int       $page
63
     * @param int       $limit
64
     * @param Type|null $mode
65
     *
66
     * @return ArrayCollection|Product[]
67
     *
68
     * @throws ApiException
69
     */
70
    public function getAll($page, $limit, Type $mode = null)
71
    {
72
        if (!$mode) {
73
            $mode = Type::all();
74
        }
75
76
        $resources = $this->getList([
77
            'query' => [
78
                'limit' => $limit,
79
                'page'  => $page,
80
                'mode'  => $mode->getValue(),
81
            ]
82
        ]);
83
84
        return $this->deserializeCollection($resources, Product::class);
85
    }
86
}
87