TierPriceRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 22
c 1
b 0
f 0
dl 0
loc 117
ccs 0
cts 25
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 5 1
A __construct() 0 11 1
A getPrimaryKeyMemberName() 0 3 1
A getPrimaryKeyUtil() 0 3 1
A init() 0 7 1
A findOneByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId() 0 15 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Repositories\TierPriceRepository
5
 *
6
 * PHP version 7
7
 *
8
 * @author    Klaas-Tido Rühl <[email protected]>
9
 * @author    Tim Wagner <[email protected]>
10
 * @copyright 2019 REFUSiON GmbH <[email protected]>
11
 * @license   https://opensource.org/licenses/MIT
12
 * @link      https://github.com/techdivision/import-product-tier-price
13
 * @link      https://www.techdivision.com
14
 * @link      https://www.refusion.com
15
 */
16
17
namespace TechDivision\Import\Product\TierPrice\Repositories;
18
19
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
20
use TechDivision\Import\Product\TierPrice\Utils\SqlStatementKeys;
21
use TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface;
22
use TechDivision\Import\Dbal\Connection\ConnectionInterface;
23
use TechDivision\Import\Dbal\Collection\Repositories\AbstractRepository;
24
use TechDivision\Import\Dbal\Repositories\SqlStatementRepositoryInterface;
25
26
/**
27
 * Default implementation of repository for accessing tier price data.
28
 *
29
 * @author    Klaas-Tido Rühl <[email protected]>
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2019 REFUSiON GmbH <[email protected]>
32
 * @license   https://opensource.org/licenses/MIT
33
 * @link      https://github.com/techdivision/import-product-tier-price
34
 * @link      https://www.techdivision.com
35
 * @link      https://www.refusion.com
36
 */
37
class TierPriceRepository extends AbstractRepository implements TierPriceRepositoryInterface
38
{
39
40
    /**
41
     * The primary key util instance.
42
     *
43
     * @var \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface
44
     */
45
    protected $primaryKeyUtil;
46
47
    /**
48
     * The prepared statement to load the existing tier prices.
49
     *
50
     * @var \PDOStatement
51
     */
52
    protected $tierPricesStmt;
53
54
    /**
55
     * The prepared statement to load an existing tier price by PK, all groups, customer group ID, qty and website ID.
56
     *
57
     * @var \PDOStatement
58
     */
59
    protected $tierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteIdStmt;
60
61
    /**
62
     * Initialize the repository with the passed connection and utility class name.
63
     * .
64
     * @param \TechDivision\Import\Dbal\Connection\ConnectionInterface               $connection             The connection instance
65
     * @param \TechDivision\Import\Dbal\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
66
     * @param \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface                $primaryKeyUtil         The primary key util instance
67
     */
68
    public function __construct(
69
        ConnectionInterface $connection,
70
        SqlStatementRepositoryInterface $sqlStatementRepository,
71
        PrimaryKeyUtilInterface $primaryKeyUtil
72
    ) {
73
74
        // pass the connection and the SQL statement repository through to the parent instance
75
        parent::__construct($connection, $sqlStatementRepository);
76
77
        // set the primary key util
78
        $this->primaryKeyUtil = $primaryKeyUtil;
79
    }
80
81
    /**
82
     * Returns the primary key util instance.
83
     *
84
     * @return \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface The primary key util instance
85
     */
86
    public function getPrimaryKeyUtil()
87
    {
88
        return $this->primaryKeyUtil;
89
    }
90
91
    /**
92
     * Returns the primary key member name for the actual Magento edition.
93
     *
94
     * @return string The primary key member name
95
     * @see \TechDivision\Import\Dbal\Utils\PrimaryKeyUtilInterface::getPrimaryKeyMemberName()
96
     */
97
    public function getPrimaryKeyMemberName()
98
    {
99
        return $this->getPrimaryKeyUtil()->getPrimaryKeyMemberName();
100
    }
101
102
    /**
103
     * Initializes the repository's prepared statements.
104
     *
105
     * @return void
106
     */
107
    public function init()
108
    {
109
        // initialize the prepared statements
110
        $this->tierPricesStmt =
111
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::TIER_PRICES));
112
        $this->tierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteIdStmt =
113
            $this->getConnection()->prepare($this->loadStatement(SqlStatementKeys::TIER_PRICE_BY_PK_AND_ALL_GROUPS_AND_CUSTOMER_GROUP_ID_AND_QTY_AND_WEBSITE_ID));
114
    }
115
116
    /**
117
     * Returns the tier price with the given parameters.
118
     *
119
     * @param string  $pk              The PK of the product relation
120
     * @param integer $allGroups       The flag if all groups are affected or not
121
     * @param integer $customerGroupId The customer group ID
122
     * @param integer $qty             The tier price quantity
123
     * @param integer $websiteId       The website ID the tier price is related to
124
     *
125
     * @return array The tier price
126
     */
127
    public function findOneByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)
128
    {
129
130
        // initialize the params
131
        $params = array(
132
            $this->getPrimaryKeyMemberName() => $pk,
133
            MemberNames::ALL_GROUPS          => $allGroups,
134
            MemberNames::CUSTOMER_GROUP_ID   => $customerGroupId,
135
            MemberNames::QTY                 => $qty,
136
            MemberNames::WEBSITE_ID          => $websiteId
137
        );
138
139
        // load and return the tier price with the passed params
140
        $this->tierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteIdStmt->execute($params);
141
        return $this->tierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteIdStmt->fetch(\PDO::FETCH_ASSOC);
142
    }
143
144
    /**
145
     * Returns an array with all the tier prices.
146
     *
147
     * @return array The tier prices
148
     */
149
    public function findAll()
150
    {
151
        // load and return the tier prices
152
        $this->tierPricesStmt->execute();
153
        return $this->tierPricesStmt->fetchAll(\PDO::FETCH_ASSOC);
154
    }
155
}
156