Completed
Push — master ( 304b3f...b6b3cb )
by Marcus
12:12
created

BundleSubjectTrait::raisePositionCounter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
cc 1
nc 1
nop 0
crap 2
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
     * @deprecated Since 22.0.0
56
     */
57
    protected $positionCounter = 1;
58
59
    /**
60
     * The mapping for the price type.
61
     *
62
     * @var array
63
     */
64
    protected $priceTypeMapping = array(
65
        'fixed'   => PriceTypes::FIXED,
66
        'percent' => PriceTypes::PERCENT
67
    );
68
69
    /**
70
     * Reset the position counter to 1.
71
     *
72
     * @return void
73
     * @deprecated Since 22.0.0
74
     */
75
    public function resetPositionCounter()
76
    {
77
        $this->positionCounter = 1;
0 ignored issues
show
Deprecated Code introduced by
The property TechDivision\Import\Prod...Trait::$positionCounter has been deprecated with message: Since 22.0.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
78
    }
79
80
    /**
81
     * Returns the acutal value of the position counter and raise's it by one.
82
     *
83
     * @return integer The actual value of the position counter
84
     * @deprecated Since 22.0.0
85
     */
86
    public function raisePositionCounter()
87
    {
88
        return $this->positionCounter++;
0 ignored issues
show
Deprecated Code introduced by
The property TechDivision\Import\Prod...Trait::$positionCounter has been deprecated with message: Since 22.0.0

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
89
    }
90
91
    /**
92
     * Save's the mapping of the child SKU and the selection ID.
93
     *
94
     * @param string  $childSku    The child SKU of the selection
95
     * @param integer $selectionId The selection ID to save
96
     *
97
     * @return void
98
     */
99
    public function addChildSkuSelectionIdMapping($childSku, $selectionId)
100
    {
101
        $this->childSkuSelectionIdMapping[$childSku] = $selectionId;
102
    }
103
104
    /**
105
     * Return's the selection ID for the passed child SKU.
106
     *
107
     * @param string $childSku The child SKU to return the selection ID for
108
     *
109
     * @return integer The last created selection ID
110
     * @throws \Exception Is thrown if the SKU is not mapped yet
111
     */
112
    public function getChildSkuSelectionMapping($childSku)
113
    {
114
115
        // query whether or not a child SKU selection ID mapping is available
116
        if (isset($this->childSkuSelectionIdMapping[$childSku])) {
117
            return $this->childSkuSelectionIdMapping[$childSku];
118
        }
119
120
        // throw an exception if the SKU has not been mapped yet
121
        throw new \Exception(
122
            $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...
123
                sprintf('Found not mapped selection ID mapping for SKU %s', $childSku)
124
            )
125
        );
126
    }
127
128
    /**
129
     * Return's the mapping for the passed price type.
130
     *
131
     * @param string $priceType The price type to map
132
     *
133
     * @return integer The mapped price type
134
     * @throws \Exception Is thrown, if the passed price type can't be mapped
135
     */
136
    public function mapPriceType($priceType)
137
    {
138
139
        // query whether or not the passed price type is available
140
        if (isset($this->priceTypeMapping[$priceType])) {
141
            return $this->priceTypeMapping[$priceType];
142
        }
143
144
        // throw an exception, if not
145
        throw new \Exception(
146
            $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...
147
                sprintf('Can\'t find mapping for price type %s', $priceType)
148
            )
149
        );
150
    }
151
152
    /**
153
     * Add's the passed mapping to the subject.
154
     *
155
     * @param array $mapping The mapping to add
156
     *
157
     * @return void
158
     */
159
    public function addParentSkuNameMapping($mapping = array())
160
    {
161
        $this->nameOptionIdMapping = array_merge_recursive($this->nameOptionIdMapping, $mapping);
162
    }
163
164
    /**
165
     * Query whether or not the option for the passed parent SKU and name has already been created.
166
     *
167
     * @param string $parentSku The parent SKU to query for
168
     * @param string $name      The option name to query for
169
     *
170
     * @return boolean TRUE if the option already exists, else FALSE
171
     */
172
    public function exists($parentSku, $name)
173
    {
174
        return isset($this->nameOptionIdMapping[$parentSku][$name]);
175
    }
176
177
    /**
178
     * Return's the last created option ID.
179
     *
180
     * @return integer $optionId The last created option ID
181
     */
182
    public function getLastOptionId()
183
    {
184
        $mapping = end($this->nameOptionIdMapping);
185
        return end($mapping);
186
    }
187
}
188