Completed
Pull Request — master (#37)
by Tim
03:54
created

CategoryAssembler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 2
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
25
/**
26
 * Repository implementation to assemble category data.
27
 *
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2016 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
32
 * @link      http://www.techdivision.com
33
 */
34
class CategoryAssembler
35
{
36
37
    /**
38
     * The repository to access categories.
39
     *
40
     * @var \TechDivision\Import\Repositories\CategoryRepository
41
     */
42
    protected $categoryRepository;
43
44
    /**
45
     * The repository to access category varchar values.
46
     *
47
     * @var \TechDivision\Import\Repositories\CategoryVarcharRepository
48
     */
49
    protected $categoryVarcharRepository;
50
51
    /**
52
     * Initialize the assembler with the passed instances.
53
     *
54
     * @param \TechDivision\Import\Repositories\CategoryRepository        $categoryRepository        The repository to access categories
55
     * @param \TechDivision\Import\Repositories\CategoryVarcharRepository $categoryVarcharRepository The repository instance
56
     */
57
    public function __construct($categoryRepository, $categoryVarcharRepository)
58
    {
59
        $this->categoryRepository = $categoryRepository;
60
        $this->categoryVarcharRepository = $categoryVarcharRepository;
61
    }
62
63
    /**
64
     * Returns an array with the available categories and their
65
     * resolved path as keys.
66
     *
67
     * @return array The array with the categories
68
     */
69
    public function getCategoriesWithResolvedPath()
70
    {
71
72
        // prepare the categories
73
        $categories = array();
74
75
        // create the array with the resolved category path as keys
76
        foreach ($this->categoryRepository->findAll() as $category) {
77
            // expload the entity IDs from the category path
78
            $entityIds = explode('/', $category[MemberNames::PATH]);
79
80
            // cut-off the root category
81
            array_shift($entityIds);
82
83
            // continue with the next category if no entity IDs are available
84
            if (sizeof($entityIds) === 0) {
85
                continue;
86
            }
87
88
            // initialize the array for the path elements
89
            $path = array();
90
            foreach ($this->categoryVarcharRepository->findAllByEntityIds($entityIds) as $cat) {
91
                $path[] = $cat[MemberNames::VALUE];
92
            }
93
94
            // append the catogory with the string path as key
95
            $categories[implode('/', $path)] = $category;
96
        }
97
98
        // return array with the categories
99
        return $categories;
100
    }
101
}
102