Builder::getLinkers()   B
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 1
Metric Value
c 1
b 1
f 1
dl 0
loc 34
ccs 0
cts 29
cp 0
rs 8.439
cc 5
eloc 21
nc 5
nop 1
crap 30
1
<?php
2
3
namespace SpeckCatalog\Mapper;
4
5
use Zend\Db\Sql;
6
7
class Builder extends AbstractMapper
8
{
9
    protected $tableName = 'catalog_builder_product';
10
    protected $tableFields = array('product_id', 'choice_id', 'option_id');
11
    protected $tableKeyFields = array('product_id', 'choice_id', 'option_id');
12
    protected $model = '\SpeckCatalog\Model\Builder\Relational';
13
14
    public function persist($row)
15
    {
16
        $where = array(
17
            'product_id' => $row['product_id'],
18
            'option_id'  => $row['option_id'],
19
        );
20
21
        if ($this->findRow($where)) {
22
            $this->update($row, $where);
23
        } else {
24
            $this->insert($row);
25
        }
26
    }
27
28
    public function getLinkers(array $data)
29
    {
30
        if (!isset($data['product_id']) && !isset($data['parent_product_id'])) {
31
            throw new \Exception('need a product_id or a parent_product_id!');
32
        }
33
34
        $cbp = 'catalog_builder_product';
35
        $cpo = 'catalog_product_option';
36
        $fields = array(
37
            'cpo' => array(
38
                'parent_product_id' => 'product_id',
39
            ),
40
        );
41
42
        $select = $this->getSelect($cbp); // linker is 1to1 with choices
43
44
        $select->join(
45
            $cpo,
46
            "{$cpo}.option_id = {$cbp}.option_id",
47
            $fields['cpo']
48
        );
49
50
        $where = new Sql\Where();
51
        if (isset($data['product_id'])) {
52
            $where->equalTo("{$cbp}.product_id", $data['product_id']);
53
        }
54
        if (isset($data['parent_product_id'])) {
55
            $where->equalTo("{$cpo}.product_id", $data['parent_product_id']);
56
        }
57
        $select->where($where);
58
        $result = $this->selectMany($select);
59
60
        return $result;
61
    }
62
63
64
    public function getBuildersByProductId($productId)
65
    {
66
        $c_p_o = 'catalog_product_option';
67
        $c_b_p = 'catalog_builder_product';
68
69
        $select = $this->getSelect($c_b_p)
70
            ->columns(array('*'))
71
            ->join(
72
                $c_p_o,
73
                $c_p_o . '.option_id = ' . $c_b_p . '.option_id',
74
                array(),
75
                'INNER'
76
            )
77
            ->where(array($c_p_o . '.product_id' => $productId));
78
79
        $result = $this->selectMany($select);
80
81
        $products = array();
82
        foreach ($result as $row) {
83
            $products[$row['product_id']][$row['option_id']] = $row['choice_id'];
84
        }
85
86
        return $products;
87
    }
88
89
    public function getBuilderOptionsByProductId($productId, $builderProductId = null)
90
    {
91
        $c_c   = 'catalog_choice';
92
        $c_o   = 'catalog_option';
93
        $c_p_o = 'catalog_product_option';
94
        $c_b_p = 'catalog_builder_product';
95
96
        $concat = "CONCAT(`{$c_o}`.`name`,' > ', `{$c_c}`.`override_name`)";
97
        $choiceName = new Sql\Expression($concat);
98
99
        $select = $this->getSelect($c_c)
100
            ->columns(array('choice_id', 'choice_name' => $choiceName))
101
            ->join(
102
                $c_p_o,
103
                $c_p_o . '.option_id=' . $c_c . '.option_id',
104
                array('option_id'),
105
                'INNER'
106
            )
107
            ->join(
108
                $c_o,
109
                $c_o . '.option_id=' . $c_c . '.option_id',
110
                array('name'),
111
                'INNER'
112
            )
113
            ->where(array(
114
                $c_o   . '.builder' => 1,
115
                $c_p_o . '.product_id' => $productId,
116
            ));
117
118
        if ($builderProductId) {
119
            $select->join(
120
                $c_b_p,
121
                $c_c . '.choice_id=' . $c_b_p . '.choice_id',
122
                array('product_id'),
123
                'LEFT OUTER'
124
            );
125
        }
126
127
        $result = $this->selectMany($select);
128
129
        $options = array();
130
        foreach ($result as $row) {
131
            $options[$row['option_id']]['choices'][$row['choice_id']] = $row['choice_name'];
132
            $options[$row['option_id']]['name'] = $row['name'];
133
            if ($builderProductId && $row['product_id'] == $builderProductId) {
134
                $options[$row['option_id']]['selected'] = $row['choice_id'];
135
            } elseif (!isset($options[$row['option_id']]['selected'])) {
136
                $options[$row['option_id']]['selected'] = null;
137
            }
138
        }
139
140
        return $options;
141
    }
142
}
143