TierPriceUpdateObserver   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 10
c 1
b 0
f 0
dl 0
loc 44
ccs 0
cts 11
cp 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId() 0 3 1
A initializeTierPrice() 0 17 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\TierPrice\Observers\TierPriceUpdateObserver
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\Observers;
18
19
use TechDivision\Import\Product\TierPrice\Utils\MemberNames;
20
21
/**
22
 * Observer for creating/updating/deleting tier prices from the database.
23
 *
24
 * @author    Klaas-Tido Rühl <[email protected]>
25
 * @author    Tim Wagner <[email protected]>
26
 * @copyright 2019 REFUSiON GmbH <[email protected]>
27
 * @license   https://opensource.org/licenses/MIT
28
 * @link      https://github.com/techdivision/import-product-tier-price
29
 * @link      https://www.techdivision.com
30
 * @link      https://www.refusion.com
31
 */
32
class TierPriceUpdateObserver extends TierPriceObserver
33
{
34
35
    /**
36
     * Initialize the product website with the passed attributes and returns an instance.
37
     *
38
     * @param array $attr The product website attributes
39
     *
40
     * @return array The initialized product website
41
     * @throws \RuntimeException Is thrown, if the attributes can not be initialized
42
     */
43
    protected function initializeTierPrice(array $attr)
44
    {
45
46
        // load the unique key parameters
47
        $pk = $attr[$this->getPrimaryKeyMemberName()];
48
        $qty = $attr[MemberNames::QTY];
49
        $allGroups = $attr[MemberNames::ALL_GROUPS];
50
        $websiteId = $attr[MemberNames::WEBSITE_ID];
51
        $customerGroupId = $attr[MemberNames::CUSTOMER_GROUP_ID];
52
53
        // try load the product by the given SKU
54
        if ($entity = $this->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)) {
55
            return $this->mergeEntity($entity, $attr);
56
        }
57
58
        // return the the attributes
59
        return $attr;
60
    }
61
62
    /**
63
     * Returns the tier price with the given parameters.
64
     *
65
     * @param string  $pk              The PK of the product relation
66
     * @param integer $allGroups       The flag if all groups are affected or not
67
     * @param integer $customerGroupId The customer group ID
68
     * @param integer $qty             The tier price quantity
69
     * @param integer $websiteId       The website ID the tier price is related to
70
     *
71
     * @return array The tier price
72
     */
73
    protected function loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId)
74
    {
75
        return $this->getTierPriceProcessor()->loadTierPriceByPkAndAllGroupsAndCustomerGroupIdAndQtyAndWebsiteId($pk, $allGroups, $customerGroupId, $qty, $websiteId);
76
    }
77
}
78