Completed
Push — 19.x ( bef728...5de20e )
by Tim
01:36
created

BunchSubject   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 273
Duplicated Lines 10.62 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 23
lcom 2
cbo 6
dl 29
loc 273
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 14 35 6
A getDefaultCallbackMappings() 0 4 1
A getDisplayModeByValue() 0 15 2
A getPageLayoutByValue() 0 15 2
A getStoreViewCodes() 0 4 1
A getRootCategoryStoreViewCodes() 0 30 4
A getPrimaryKeyMemberName() 0 4 1
A getEntityType() 15 15 2
A isUrlKeyOf() 0 5 2
A getCleanUpColumns() 0 14 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * TechDivision\Import\Category\Subjects\BunchSubject
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-category
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Category\Subjects;
22
23
use TechDivision\Import\Subjects\ExportableTrait;
24
use TechDivision\Import\Subjects\FileUploadTrait;
25
use TechDivision\Import\Subjects\ExportableSubjectInterface;
26
use TechDivision\Import\Subjects\FileUploadSubjectInterface;
27
use TechDivision\Import\Subjects\UrlKeyAwareSubjectInterface;
28
use TechDivision\Import\Subjects\CleanUpColumnsSubjectInterface;
29
use TechDivision\Import\Category\Utils\PageLayoutKeys;
30
use TechDivision\Import\Category\Utils\DisplayModeKeys;
31
use TechDivision\Import\Category\Utils\ConfigurationKeys;
32
use TechDivision\Import\Category\Utils\MemberNames;
33
use TechDivision\Import\Utils\StoreViewCodes;
34
use TechDivision\Import\Category\Utils\RegistryKeys;
35
36
/**
37
 * The subject implementation that handles the business logic to persist products.
38
 *
39
 * @author    Tim Wagner <[email protected]>
40
 * @copyright 2019 TechDivision GmbH <[email protected]>
41
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
42
 * @link      https://github.com/techdivision/import-category
43
 * @link      http://www.techdivision.com
44
 */
45
class BunchSubject extends AbstractCategorySubject implements ExportableSubjectInterface, FileUploadSubjectInterface, UrlKeyAwareSubjectInterface, CleanUpColumnsSubjectInterface
46
{
47
48
    /**
49
     * The trait that implements the export functionality.
50
     *
51
     * @var \TechDivision\Import\Subjects\ExportableTrait
52
     */
53
    use ExportableTrait;
54
55
    /**
56
     * The trait that provides file upload functionality.
57
     *
58
     * @var \TechDivision\Import\Subjects\FileUploadTrait
59
     */
60
    use FileUploadTrait;
61
62
    /**
63
     * The array with the available display mode keys.
64
     *
65
     * @var array
66
     */
67
    protected $availableDisplayModes = array(
68
        'Products only'             => DisplayModeKeys::DISPLAY_MODE_PRODUCTS_ONLY,
69
        'Static block only'         => DisplayModeKeys::DISPLAY_MODE_STATIC_BLOCK_ONLY,
70
        'Static block and products' => DisplayModeKeys::DISPLAY_MODE_BOTH
71
    );
72
73
    /**
74
     * The array with the available page layout keys.
75
     *
76
     * @var array
77
     */
78
    protected $availablePageLayouts = array(
79
        '1 column'                 => PageLayoutKeys::PAGE_LAYOUT_1_COLUMN,
80
        '2 columns with left bar'  => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_LEFT,
81
        '2 columns with right bar' => PageLayoutKeys::PAGE_LAYOUT_2_COLUMNS_RIGHT,
82
        '3 columns'                => PageLayoutKeys::PAGE_LAYOUT_3_COLUMNS,
83
        'Empty'                    => PageLayoutKeys::PAGE_LAYOUT_EMPTY
84
    );
85
    /**
86
     * The default callback mappings for the Magento standard category attributes.
87
     *
88
     * @var array
89
     */
90
    protected $defaultCallbackMappings = array(
91
        'display_mode' => array('import_category.callback.display.mode'),
92
        'page_layout'  => array('import_category.callback.page.layout'),
93
    );
94
95
    /**
96
     * The available entity types.
97
     *
98
     * @var array
99
     */
100
    protected $entityTypes = array();
101
102
    /**
103
     * Intializes the previously loaded global data for exactly one bunch.
104
     *
105
     * @param string $serial The serial of the actual import
106
     *
107
     * @return void
108
     */
109
    public function setUp($serial)
110
    {
111
112
        // load the status of the actual import
113
        $status = $this->getRegistryProcessor()->getAttribute(RegistryKeys::STATUS);
114
115
        // load the global data we've prepared initially
116
        $this->entityTypes = $status[RegistryKeys::GLOBAL_DATA][RegistryKeys::ENTITY_TYPES];
117
118
        // initialize the flag whether to copy images or not
119
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::COPY_IMAGES)) {
120
            $this->setCopyImages($this->getConfiguration()->getParam(ConfigurationKeys::COPY_IMAGES));
121
        }
122
123
        // initialize media directory => can be absolute or relative
124 View Code Duplication
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::MEDIA_DIRECTORY)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
            try {
126
                $this->setMediaDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::MEDIA_DIRECTORY)));
127
            } catch (\InvalidArgumentException $iae) {
128
                $this->getSystemLogger()->warning($iae);
129
            }
130
        }
131
132
        // initialize images directory => can be absolute or relative
133 View Code Duplication
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134
            try {
135
                $this->setImagesFileDir($this->resolvePath($this->getConfiguration()->getParam(ConfigurationKeys::IMAGES_FILE_DIRECTORY)));
136
            } catch (\InvalidArgumentException $iae) {
137
                $this->getSystemLogger()->warning($iae);
138
            }
139
        }
140
141
        // prepare the callbacks
142
        parent::setUp($serial);
143
    }
144
145
    /**
146
     * Return's the default callback mappings.
147
     *
148
     * @return array The default callback mappings
149
     */
150
    public function getDefaultCallbackMappings()
151
    {
152
        return $this->defaultCallbackMappings;
153
    }
154
155
    /**
156
     * Return's the display mode for the passed display mode string.
157
     *
158
     * @param string $displayMode The display mode string to return the key for
159
     *
160
     * @return integer The requested display mode
161
     * @throws \Exception Is thrown, if the requested display mode is not available
162
     */
163
    public function getDisplayModeByValue($displayMode)
164
    {
165
166
        // query whether or not, the requested display mode is available
167
        if (isset($this->availableDisplayModes[$displayMode])) {
168
            return $this->availableDisplayModes[$displayMode];
169
        }
170
171
        // throw an exception, if not
172
        throw new \Exception(
173
            $this->appendExceptionSuffix(
174
                sprintf('Found invalid display mode %s', $displayMode)
175
            )
176
        );
177
    }
178
179
    /**
180
     * Return's the page layout for the passed page layout string.
181
     *
182
     * @param string $pageLayout The page layout string to return the key for
183
     *
184
     * @return integer The requested page layout
185
     * @throws \Exception Is thrown, if the requested page layout is not available
186
     */
187
    public function getPageLayoutByValue($pageLayout)
188
    {
189
190
        // query whether or not, the requested display mode is available
191
        if (isset($this->availablePageLayouts[$pageLayout])) {
192
            return $this->availablePageLayouts[$pageLayout];
193
        }
194
195
        // throw an exception, if not
196
        throw new \Exception(
197
            $this->appendExceptionSuffix(
198
                sprintf('Found invalid page layout %s', $pageLayout)
199
            )
200
        );
201
    }
202
203
    /**
204
     * Return's the available store view codes of the available stores.
205
     *
206
     * @return array The array with the available store view codes
207
     */
208
    public function getStoreViewCodes()
209
    {
210
        return array_keys($this->stores);
211
    }
212
213
    /**
214
     * Returns the store view codes relevant to the category represented by the current row.
215
     *
216
     * @param string $path The path to return the root category's store view codes for
217
     *
218
     * @return array The store view codes for the given root category
219
     * @throws \Exception Is thrown, if the root category of the passed path is NOT available
220
     */
221
    public function getRootCategoryStoreViewCodes($path)
222
    {
223
224
        // explode the path of the root category
225
        list ($rootCategoryPath, ) = explode('/', $path);
226
227
        // query whether or not a root category with the given path exists
228
        if ($rootCategory = $this->getCategoryByPath($rootCategoryPath)) {
229
            // initialize the array with the store view codes
230
            $storeViewCodes = array();
231
232
            // try to assemble the store view codes by iterating over the available root categories
233
            foreach ($this->rootCategories as $storeViewCode => $category) {
234
                // query whether or not the entity ID of the root category matches
235
                if ((integer) $category[$this->getPrimaryKeyMemberName()] === (integer) $rootCategory[$this->getPrimaryKeyMemberName()]) {
236
                    $storeViewCodes[] = $storeViewCode;
237
                }
238
            }
239
240
            // return the array with the store view codes
241
            return $storeViewCodes;
242
        }
243
244
        // throw an exception, if the root category is NOT available
245
        throw new \Exception(
246
            $this->appendExceptionSuffix(
247
                sprintf('Can\'t load root category "%s" for path "%s"', $rootCategoryPath, $path)
248
            )
249
        );
250
    }
251
252
    /**
253
     * Return's the PK column name to create the product => attribute relation.
254
     *
255
     * @return string The PK column name
256
     */
257
    protected function getPrimaryKeyMemberName()
258
    {
259
        return MemberNames::ENTITY_ID;
260
    }
261
262
    /**
263
     * Return's the entity type for the configured entity type code.
264
     *
265
     * @return array The requested entity type
266
     * @throws \Exception Is thrown, if the requested entity type is not available
267
     */
268 View Code Duplication
    public function getEntityType()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
269
    {
270
271
        // query whether or not the entity type with the passed code is available
272
        if (isset($this->entityTypes[$entityTypeCode = $this->getEntityTypeCode()])) {
273
            return $this->entityTypes[$entityTypeCode];
274
        }
275
276
        // throw a new exception
277
        throw new \Exception(
278
            $this->appendExceptionSuffix(
279
                sprintf('Requested entity type "%s" is not available', $entityTypeCode)
280
            )
281
        );
282
    }
283
284
    /**
285
     * Return's TRUE, if the passed URL key varchar value IS related with the actual PK.
286
     *
287
     * @param array $categoryVarcharAttribute The varchar value to check
288
     *
289
     * @return boolean TRUE if the URL key is related, else FALSE
290
     */
291
    public function isUrlKeyOf(array $categoryVarcharAttribute)
292
    {
293
        return ((integer) $categoryVarcharAttribute[MemberNames::ENTITY_ID] === (integer) $this->getLastEntityId()) &&
294
               ((integer) $categoryVarcharAttribute[MemberNames::STORE_ID] === (integer) $this->getRowStoreId(StoreViewCodes::ADMIN));
295
    }
296
297
    /**
298
     * Merge the columns from the configuration with all image type columns to define which
299
     * columns should be cleaned-up.
300
     *
301
     * @return array The columns that has to be cleaned-up
302
     */
303
    public function getCleanUpColumns()
304
    {
305
306
        // initialize the array for the columns that has to be cleaned-up
307
        $cleanUpColumns = array();
308
309
        // query whether or not an array has been specified in the configuration
310
        if ($this->getConfiguration()->hasParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS)) {
311
            $cleanUpColumns = $this->getConfiguration()->getParam(ConfigurationKeys::CLEAN_UP_EMPTY_COLUMNS);
312
        }
313
314
        // return the array with the column names
315
        return $cleanUpColumns;
316
    }
317
}
318