Completed
Push — 14.x ( 50b81b )
by Tim
04:00
created

BundleSubjectTrait::addNameOptionIdMapping()   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 2
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
     */
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
     * Return's the option ID for the passed name.
151
     *
152
     * @param string $name The name to return the option ID for
153
     *
154
     * @return integer The option ID for the passed name
155
     * @throws \Exception Is thrown, if no option ID for the passed name is available
156
     */
157
    public function getOptionIdForName($name)
158
    {
159
160
        // query whether or not an option ID for the passed name is available
161
        if (isset($this->nameOptionIdMapping[$name])) {
162
            return $this->nameOptionIdMapping[$name];
163
        }
164
165
        // throw an exception, if not
166
        throw new \Exception(
167
            $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...
168
                sprintf('Can\'t find option ID for name %s', $name)
169
            )
170
        );
171
    }
172
173
    /**
174
     * Add's the mapping for the passed name => option ID.
175
     *
176
     * @param string  $name     The name of the option
177
     * @param integer $optionId The created option ID
178
     *
179
     * @return void
180
     */
181
    public function addNameOptionIdMapping($name, $optionId)
182
    {
183
        $this->nameOptionIdMapping[$name] = $optionId;
184
    }
185
186
    /**
187
     * Query whether or not the option with the passed name has already been created.
188
     *
189
     * @param string $name The option name to query for
190
     *
191
     * @return boolean TRUE if the option already exists, else FALSE
192
     */
193
    public function exists($name)
194
    {
195
        return isset($this->nameOptionIdMapping[$name]);
196
    }
197
198
    /**
199
     * Return's the last created option ID.
200
     *
201
     * @return integer $optionId The last created option ID
202
     */
203
    public function getLastOptionId()
204
    {
205
        return end($this->nameOptionIdMapping);
206
    }
207
}
208