FlatTreeContext   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 6 1
A makeTree() 0 10 2
A makeNode() 0 9 1
A configureOptions() 0 18 1
1
<?php
2
/*
3
 * WellCommerce Open-Source E-Commerce Platform
4
 *
5
 * This file is part of the WellCommerce package.
6
 *
7
 * (c) Adam Piotrowski <[email protected]>
8
 *
9
 * For the full copyright and license information,
10
 * please view the LICENSE file that was distributed with this source code.
11
 */
12
13
namespace WellCommerce\Component\DataSet\Context;
14
15
use Doctrine\ORM\QueryBuilder;
16
use Symfony\Component\OptionsResolver\OptionsResolver;
17
use WellCommerce\Component\DataSet\Cache\CacheOptions;
18
use WellCommerce\Component\DataSet\Column\ColumnCollection;
19
use WellCommerce\Component\DataSet\Request\DataSetRequestInterface;
20
21
/**
22
 * Class FlatTreeContext
23
 *
24
 * @author Adam Piotrowski <[email protected]>
25
 */
26
class FlatTreeContext extends ArrayContext
27
{
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getResult(QueryBuilder $builder, DataSetRequestInterface $request, ColumnCollection $columns, CacheOptions $cache)
32
    {
33
        $result = parent::getResult($builder, $request, $columns, $cache);
34
35
        return $this->makeTree($result['rows']);
36
    }
37
38
    /**
39
     * Creates flat tree structure from result rows
40
     *
41
     * @param array $result
42
     *
43
     * @return array
44
     */
45
    private function makeTree(array $result)
46
    {
47
        $tree = [];
48
49
        foreach ($result as $row) {
50
            $this->makeNode($row, $tree);
51
        }
52
53
        return $tree;
54
    }
55
56
    /**
57
     * Converts single row to flat-tree node
58
     *
59
     * @param array $row
60
     * @param array $tree
61
     */
62
    private function makeNode(&$row, &$tree)
63
    {
64
        $hasChildren = $this->propertyAccessor->getValue($row, "[{$this->options['children_column']}]");
65
        $weight      = $this->propertyAccessor->getValue($row, "[{$this->options['hierarchy_column']}]");
66
67
        $row['hasChildren'] = (bool)($hasChildren > 0);
68
        $row['weight']      = $weight;
69
        $tree[$row['id']]   = $row;
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function configureOptions(OptionsResolver $resolver)
76
    {
77
        parent::configureOptions($resolver);
78
79
        $resolver->setRequired([
80
            'children_column',
81
            'hierarchy_column',
82
        ]);
83
84
        $resolver->setDefaults([
85
            'children_column'  => 'children',
86
            'hierarchy_column' => 'hierarchy',
87
            'pagination'       => false
88
        ]);
89
90
        $resolver->setAllowedTypes('children_column', 'string');
91
        $resolver->setAllowedTypes('hierarchy_column', 'string');
92
    }
93
}
94