InventoryLevel::connect()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
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 1
dl 0
loc 8
rs 10
1
<?php
2
3
namespace Helix\Shopify;
4
5
use Helix\Shopify\Base\Data;
6
7
/**
8
 * An inventory level, which acts as a join between a location and an inventory item.
9
 *
10
 * @see https://shopify.dev/docs/admin-api/rest/reference/inventory/inventorylevel
11
 *
12
 * @see Location::newInventoryLevel()
13
 *
14
 * @method null|int getAvailable            ()
15
 * @method string   getInventoryItemId      () injected
16
 * @method string   getLocationId           () injected
17
 * @method string   getUpdatedAt            ()
18
 */
19
class InventoryLevel extends Data
20
{
21
22
    /**
23
     * @param int $qty +/-
24
     * @return $this
25
     */
26
    public function adjustAvailable(int $qty)
27
    {
28
        $remote = $this->api->post('inventory_levels', [
29
            'inventory_item_id' => $this->getInventoryItemId(),
30
            'location_id' => $this->getLocationId(),
31
            'available_adjustment' => $qty
32
        ]);
33
        return $this->_setData($remote['inventory_level']);
34
    }
35
36
    /**
37
     * @param bool $relocate
38
     * @return $this
39
     */
40
    public function connect(bool $relocate = false)
41
    {
42
        $remote = $this->api->post('inventory_levels/connect', [
43
            'inventory_item_id' => $this->getInventoryItemId(),
44
            'location_id' => $this->getLocationId(),
45
            'relocate_if_necessary' => $relocate
46
        ]);
47
        return $this->_setData($remote['inventory_level']);
48
    }
49
50
    public function disconnect(): void
51
    {
52
        $this->api->delete('inventory_levels', [
53
            'inventory_item_id' => $this->getInventoryItemId(),
54
            'location_id' => $this->getLocationId()
55
        ]);
56
    }
57
58
    /**
59
     * @return InventoryItem
60
     */
61
    public function getInventoryItem()
62
    {
63
        return InventoryItem::load($this, $this->getInventoryItemId());
64
    }
65
66
    /**
67
     * @return Location
68
     */
69
    public function getLocation()
70
    {
71
        return Location::load($this, $this->getLocationId());
72
    }
73
74
    /**
75
     * @param int $qty
76
     * @return InventoryLevel
77
     */
78
    public function setAvailable(int $qty)
79
    {
80
        $remote = $this->api->post('inventory_levels', [
81
            'inventory_item_id' => $this->getInventoryItemId(),
82
            'location_id' => $this->getLocationId(),
83
            'available' => $qty
84
        ]);
85
        return $this->_setData($remote['inventory_level']);
86
    }
87
}