ProductHydrator::prepareValues()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 3
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace DefaultValue\Bundle\AkeneoInlineEditBundle\Product;
4
5
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
6
use Pim\Bundle\DataGridBundle\Datasource\ResultRecord\HydratorInterface;
7
8
/**
9
 * Hydrate results of Doctrine ORM query as ResultRecord array, with extra scopeCode property
10
 *
11
 */
12
class ProductHydrator implements HydratorInterface
13
{
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function hydrate($qb, array $options = [])
18
    {
19
        $localeCode = $options['locale_code'];
20
        $scopeCode = $options['scope_code'];
21
        $query = $qb->getQuery();
22
        $results = $query->getArrayResult();
23
        $rows    = [];
24
        foreach ($results as $result) {
25
            $entityFields = [];
26
            if (isset($result[0])) {
27
                $entityFields = $result[0];
28
                unset($result[0]);
29
            }
30
            $otherFields = $result;
31
            $result = $entityFields + $otherFields;
32
            $result = $this->prepareValues($result);
33
            $result = $this->prepareGroups($result);
34
            $result['dataLocale'] = $localeCode;
35
            $result['scopeCode'] = $scopeCode;
36
37
            $rows[] = new ResultRecord($result);
38
        }
39
40
        return $rows;
41
    }
42
43
    /**
44
     * Prepare product values
45
     *
46
     * @param array $result
47
     *
48
     * @return array
49
     */
50
    protected function prepareValues($result)
51
    {
52
        if (isset($result['values'])) {
53
            $values = $result['values'];
54
            foreach ($values as $value) {
55
                $result[$value['attribute']['code']] = $value;
56
            }
57
            unset($result['values']);
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * Prepare product groups
65
     *
66
     * @param array $result
67
     *
68
     * @return array
69
     */
70
    protected function prepareGroups($result)
71
    {
72
        if (isset($result['groups'])) {
73
            $groups = [];
74
            foreach ($result['groups'] as $group) {
75
                $code = $group['code'];
76
                $label = (count($group['translations']) > 0) ? $group['translations'][0]['label'] : null;
77
                $groups[$code] = ['code' => $code, 'label' => $label];
78
            }
79
            $result['groups'] = $groups;
80
        }
81
82
        return $result;
83
    }
84
}
85