InventorySourceItemRepository   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 87
ccs 0
cts 20
cp 0
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 10 1
A load() 0 5 1
A findAll() 0 5 1
A findOneBySkuAndSourceCode() 0 12 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Msi\Repositories\InventorySourceItemRepository
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Tim Wagner <[email protected]>
9
 * @copyright 2018 TechDivision GmbH <[email protected]>
10
 * @license   https://opensource.org/licenses/MIT
11
 * @link      https://github.com/techdivision/import-product-msi
12
 * @link      http://www.techdivision.com
13
 */
14
15
namespace TechDivision\Import\Product\Msi\Repositories;
16
17
use TechDivision\Import\Dbal\Collection\Repositories\AbstractRepository;
18
use TechDivision\Import\Product\Msi\Utils\MemberNames;
19
use TechDivision\Import\Product\Msi\Utils\SqlStatementKeys;
20
21
/**
22
 * Repository implementation to load MSI inventory source item data.
23
 *
24
 * @author    Tim Wagner <[email protected]>
25
 * @copyright 2018 TechDivision GmbH <[email protected]>
26
 * @license   https://opensource.org/licenses/MIT
27
 * @link      https://github.com/techdivision/import-product-msi
28
 * @link      http://www.techdivision.com
29
 */
30
class InventorySourceItemRepository extends AbstractRepository implements InventorySourceItemRepositoryInterface
31
{
32
33
    /**
34
     * The prepared statement to load a inventory source item with the passed ID.
35
     *
36
     * @var \PDOStatement
37
     */
38
    protected $inventorySourceItemStmt;
39
40
    /**
41
     * The prepared statement to load a inventory source item with the passed SKU and source code.
42
     *
43
     * @var \PDOStatement
44
     */
45
    protected $inventorySourceItemBySkuAndSourceCodeStmt;
46
47
    /**
48
     * The prepared statement to load the existing inventory source items.
49
     *
50
     * @var \PDOStatement
51
     */
52
    protected $inventorySourceItemsStmt;
53
54
    /**
55
     * Initializes the repository's prepared statements.
56
     *
57
     * @return void
58
     */
59
    public function init()
60
    {
61
62
        // initialize the prepared statements
63
        $this->inventorySourceItemStmt =
64
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::INVENTORY_SOURCE_ITEM));
65
        $this->inventorySourceItemBySkuAndSourceCodeStmt =
66
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::INVENTORY_SOURCE_ITEM_BY_SKU_AND_SOURCE_CODE));
67
        $this->inventorySourceItemsStmt =
68
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::INVENTORY_SOURCE_ITEMS));
69
    }
70
71
    /**
72
     * Return's the inventory source item with the passed entity ID.
73
     *
74
     * @param integer $id The ID of the inventory source item to return
75
     *
76
     * @return array|null The inventory source item
77
     */
78
    public function load($id)
79
    {
80
        // load and return the available products
81
        $this->inventorySourceItemStmt->execute(array(MemberNames::SOURCE_ITEM_ID => $id));
82
        return $this->inventorySourceItemStmt->fetchAll(\PDO::FETCH_ASSOC);
83
    }
84
85
    /**
86
     * Load's the inventory source item with the passed SKU and source code.
87
     *
88
     * @param string $sku        The SKU of the inventory source item to return
89
     * @param string $sourceCode The source code of the inventory source item to return
90
     *
91
     * @return array The inventory source item
92
     */
93
    public function findOneBySkuAndSourceCode($sku, $sourceCode)
94
    {
95
96
        // initialize the params
97
        $params = array(
98
            MemberNames::SKU         => $sku,
99
            MemberNames::SOURCE_CODE => $sourceCode
100
        );
101
102
        // if not, try to load the inventory source item with the passed SKU/source code
103
        $this->inventorySourceItemBySkuAndSourceCodeStmt->execute($params);
104
        return $this->inventorySourceItemBySkuAndSourceCodeStmt->fetch(\PDO::FETCH_ASSOC);
105
    }
106
107
    /**
108
     * Return's the available inventory source items.
109
     *
110
     * @return array The available inventory source items
111
     */
112
    public function findAll()
113
    {
114
        // load and return all the available inventory source items
115
        $this->inventorySourceItemsStmt->execute();
116
        return $this->inventorySourceItemsStmt->fetchAll(\PDO::FETCH_ASSOC);
117
    }
118
}
119