Completed
Push — 19.x ( 019ccc )
by Tim
02:02
created

BundleSubjectTrait   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 4
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 10
lcom 4
cbo 0
dl 0
loc 151
ccs 0
cts 47
cp 0
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A resetPositionCounter() 0 4 1
A raisePositionCounter() 0 4 1
A addChildSkuSelectionIdMapping() 0 4 1
A getChildSkuSelectionMapping() 0 15 2
A mapPriceType() 0 15 2
A addParentSkuNameMapping() 0 4 1
A exists() 0 4 1
A getLastOptionId() 0 5 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Product\Bundle\Subjects\BundleSubjectTrait
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2019 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-product-bundle
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Product\Bundle\Subjects;
22
23
use TechDivision\Import\Product\Bundle\Utils\PriceTypes;
24
25
/**
26
 * A trait implementation that provides functionality to handle the bunch import on subject level.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2019 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/techdivision/import-product-bundle
32
 * @link      http://www.techdivision.com
33
 */
34
trait BundleSubjectTrait
35
{
36
37
    /**
38
     * The option name => option ID mapping.
39
     *
40
     * @var array
41
     */
42
    protected $nameOptionIdMapping = array();
43
44
    /**
45
     * The ID of the last created selection.
46
     *
47
     * @var integer
48
     */
49
    protected $childSkuSelectionIdMapping = array();
50
51
    /**
52
     * The position counter, if no position for the bundle selection has been specified.
53
     *
54
     * @var integer
55
     */
56
    protected $positionCounter = 1;
57
58
    /**
59
     * The mapping for the price type.
60
     *
61
     * @var array
62
     */
63
    protected $priceTypeMapping = array(
64
        'fixed'   => PriceTypes::FIXED,
65
        'percent' => PriceTypes::PERCENT
66
    );
67
68
    /**
69
     * Reset the position counter to 1.
70
     *
71
     * @return void
72
     */
73
    public function resetPositionCounter()
74
    {
75
        $this->positionCounter = 1;
76
    }
77
78
    /**
79
     * Returns the acutal value of the position counter and raise's it by one.
80
     *
81
     * @return integer The actual value of the position counter
82
     */
83
    public function raisePositionCounter()
84
    {
85
        return $this->positionCounter++;
86
    }
87
88
    /**
89
     * Save's the mapping of the child SKU and the selection ID.
90
     *
91
     * @param string  $childSku    The child SKU of the selection
92
     * @param integer $selectionId The selection ID to save
93
     *
94
     * @return void
95
     */
96
    public function addChildSkuSelectionIdMapping($childSku, $selectionId)
97
    {
98
        $this->childSkuSelectionIdMapping[$childSku] = $selectionId;
99
    }
100
101
    /**
102
     * Return's the selection ID for the passed child SKU.
103
     *
104
     * @param string $childSku The child SKU to return the selection ID for
105
     *
106
     * @return integer The last created selection ID
107
     * @throws \Exception Is thrown if the SKU is not mapped yet
108
     */
109
    public function getChildSkuSelectionMapping($childSku)
110
    {
111
112
        // query whether or not a child SKU selection ID mapping is available
113
        if (isset($this->childSkuSelectionIdMapping[$childSku])) {
114
            return $this->childSkuSelectionIdMapping[$childSku];
115
        }
116
117
        // throw an exception if the SKU has not been mapped yet
118
        throw new \Exception(
119
            $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
120
                sprintf('Found not mapped selection ID mapping for SKU %s', $childSku)
121
            )
122
        );
123
    }
124
125
    /**
126
     * Return's the mapping for the passed price type.
127
     *
128
     * @param string $priceType The price type to map
129
     *
130
     * @return integer The mapped price type
131
     * @throws \Exception Is thrown, if the passed price type can't be mapped
132
     */
133
    public function mapPriceType($priceType)
134
    {
135
136
        // query whether or not the passed price type is available
137
        if (isset($this->priceTypeMapping[$priceType])) {
138
            return $this->priceTypeMapping[$priceType];
139
        }
140
141
        // throw an exception, if not
142
        throw new \Exception(
143
            $this->appendExceptionSuffix(
0 ignored issues
show
Bug introduced by
It seems like appendExceptionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
144
                sprintf('Can\'t find mapping for price type %s', $priceType)
145
            )
146
        );
147
    }
148
149
    /**
150
     * Add's the passed mapping to the subject.
151
     *
152
     * @param array $mapping The mapping to add
153
     *
154
     * @return void
155
     */
156
    public function addParentSkuNameMapping($mapping = array())
157
    {
158
        $this->nameOptionIdMapping = array_merge_recursive($this->nameOptionIdMapping, $mapping);
159
    }
160
161
    /**
162
     * Query whether or not the option for the passed parent SKU and name has already been created.
163
     *
164
     * @param string $parentSku The parent SKU to query for
165
     * @param string $name      The option name to query for
166
     *
167
     * @return boolean TRUE if the option already exists, else FALSE
168
     */
169
    public function exists($parentSku, $name)
170
    {
171
        return isset($this->nameOptionIdMapping[$parentSku][$name]);
172
    }
173
174
    /**
175
     * Return's the last created option ID.
176
     *
177
     * @return integer $optionId The last created option ID
178
     */
179
    public function getLastOptionId()
180
    {
181
        $mapping = end($this->nameOptionIdMapping);
182
        return end($mapping);
183
    }
184
}
185