Completed
Pull Request — master (#1)
by Laurent
04:28
created

StockMovementsService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Dolibarr\Client\Service;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Dolibarr\Client\Domain\Product\ProductId;
7
use Dolibarr\Client\Domain\Resource\ApiResource;
8
use Dolibarr\Client\Domain\StockMovement\StockMovement;
9
use Dolibarr\Client\Domain\StockMovement\StockMovementId;
10
use Dolibarr\Client\Domain\Warehouse\WarehouseId;
11
use Dolibarr\Client\Exception\ApiException;
12
use Dolibarr\Client\HttpClient\HttpClientInterface;
13
use JMS\Serializer\SerializerInterface;
14
15
/**
16
 * @author Laurent De Coninck <[email protected]>
17
 */
18
final class StockMovementsService extends AbstractService
19
{
20
21
    /**
22
     * @param HttpClientInterface $httpClient
23
     * @param SerializerInterface $serializerInterface
24
     */
25
    public function __construct(HttpClientInterface $httpClient, SerializerInterface $serializerInterface)
26
    {
27
        parent::__construct($httpClient, $serializerInterface, new ApiResource('stockmovements'));
28
    }
29
30
    /**
31
     * @param StockMovement $movement
32
     *
33
     * @return StockMovementId
34
     *
35
     * @throws ApiException
36
     */
37
    public function create(StockMovement $movement)
38
    {
39
        $resourceId = $this->post($this->serialize($movement));
0 ignored issues
show
Bug introduced by
$this->serialize($movement) of type string is incompatible with the type array expected by parameter $json of Dolibarr\Client\Service\AbstractService::post(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $resourceId = $this->post(/** @scrutinizer ignore-type */ $this->serialize($movement));
Loading history...
40
41
        return StockMovementId::fromResourceId($resourceId);
42
    }
43
44
    /**
45
     * @param WarehouseId $warehouseId
46
     *
47
     * @return ArrayCollection|StockMovement[]
48
     *
49
     * @throws ApiException
50
     */
51
    public function getByWarehouse(WarehouseId $warehouseId)
52
    {
53
        $resources = $this->getList(['query' => [
54
            'sqlfilters' => 'fk_entrepot='.$warehouseId->getId()
55
        ]]);
56
57
        return $this->deserializeCollection($resources, StockMovement::class);
58
    }
59
60
    /**
61
     * @param ProductId $productId
62
     *
63
     * @return ArrayCollection|StockMovement[]
64
     *
65
     * @throws ApiException
66
     */
67
    public function getByProduct(ProductId $productId)
68
    {
69
        $resources = $this->getList(['query' => [
70
            'sqlfilters' => 'fk_product='.$productId->getId()
71
        ]]);
72
73
        return $this->deserializeCollection($resources, StockMovement::class);
74
    }
75
}
76