Completed
Push — master ( 3a08fe...aee93b )
by Tim
18:05 queued 16:13
created

getCategoriesWithResolvedPathByStoreView()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 37

Duplication

Lines 37
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 37
loc 37
ccs 0
cts 21
cp 0
rs 8.7057
c 0
b 0
f 0
cc 6
nc 5
nop 1
crap 42
1
<?php
2
3
/**
4
 * TechDivision\Import\Assembler\CategoryAssembler
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 2016 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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Assembler;
22
23
use TechDivision\Import\Utils\MemberNames;
24
use TechDivision\Import\Repositories\CategoryRepository;
25
use TechDivision\Import\Repositories\CategoryVarcharRepository;
26
27
/**
28
 * Repository implementation to assemble category data.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
class CategoryAssembler implements CategoryAssemblerInterface
37
{
38
39
    /**
40
     * The repository to access categories.
41
     *
42
     * @var \TechDivision\Import\Repositories\CategoryRepository
43
     */
44
    protected $categoryRepository;
45
46
    /**
47
     * The repository to access category varchar values.
48
     *
49
     * @var \TechDivision\Import\Repositories\CategoryVarcharRepository
50
     */
51
    protected $categoryVarcharRepository;
52
53
    /**
54
     * Initialize the assembler with the passed instances.
55
     *
56
     * @param \TechDivision\Import\Repositories\CategoryRepository        $categoryRepository        The repository to access categories
57
     * @param \TechDivision\Import\Repositories\CategoryVarcharRepository $categoryVarcharRepository The repository instance
58
     */
59
    public function __construct(
60
        CategoryRepository $categoryRepository,
61
        CategoryVarcharRepository $categoryVarcharRepository
62
    ) {
63
        $this->categoryRepository = $categoryRepository;
64
        $this->categoryVarcharRepository = $categoryVarcharRepository;
65
    }
66
67
    /**
68
     * Returns an array with the available categories and their
69
     * resolved path as keys.
70
     *
71
     * @return array The array with the categories
72
     */
73 View Code Duplication
    public function getCategoriesWithResolvedPath()
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...
74
    {
75
76
        // prepare the categories
77
        $categories = array();
78
79
        // load the categories from the database
80
        $availableCategories = $this->categoryRepository->findAll();
81
82
        // create the array with the resolved category path as keys
83
        foreach ($availableCategories as $category) {
84
            // expload the entity IDs from the category path
85
            $entityIds = explode('/', $category[MemberNames::PATH]);
86
87
            // cut-off the root category
88
            array_shift($entityIds);
89
90
            // continue with the next category if no entity IDs are available
91
            if (sizeof($entityIds) === 0) {
92
                continue;
93
            }
94
95
            // initialize the array for the path elements
96
            $path = array();
97
            foreach ($entityIds as $entityId) {
98
                $cat = $this->categoryVarcharRepository->findByEntityId($entityId);
99
                if ($cat && $cat[MemberNames::VALUE]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cat of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
100
                    $path[] = $cat[MemberNames::VALUE];
101
                }
102
            }
103
104
            // append the catogory with the string path as key
105
            $categories[implode('/', $path)] = $category;
106
        }
107
108
        // return array with the categories
109
        return $categories;
110
    }
111
112
113
    /**
114
     * Return's an array with the available categories and their resolved path
115
     * as keys by store view ID.
116
     *
117
     * @param integer $storeViewId The store view ID to return the categories for
118
     *
119
     * @return array The available categories for the passed store view ID
120
     */
121 View Code Duplication
    public function getCategoriesWithResolvedPathByStoreView($storeViewId)
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...
122
    {
123
        // prepare the categories
124
        $categories = array();
125
126
        // load the categories from the database
127
        $availableCategories = $this->categoryRepository->findAllByStoreView($storeViewId);
128
129
        // create the array with the resolved category path as keys
130
        foreach ($availableCategories as $category) {
131
            // expload the entity IDs from the category path
132
            $entityIds = explode('/', $category[MemberNames::PATH]);
133
134
            // cut-off the root category
135
            array_shift($entityIds);
136
137
            // continue with the next category if no entity IDs are available
138
            if (sizeof($entityIds) === 0) {
139
                continue;
140
            }
141
142
            // initialize the array for the path elements
143
            $path = array();
144
            foreach ($entityIds as $entityId) {
145
                $cat = $this->categoryVarcharRepository->findByEntityId($entityId);
146
                if ($cat && $cat[MemberNames::VALUE]) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cat of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
147
                    $path[] = $cat[MemberNames::VALUE];
148
                }
149
            }
150
151
            // append the catogory with the string path as key
152
            $categories[implode('/', $path)] = $category;
153
        }
154
155
        // return array with the categories
156
        return $categories;
157
    }
158
}
159