Completed
Push — master ( 65613c...8f8233 )
by Lawrence
02:16
created

Product::getProduct()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 13
nc 3
nop 1
1
<?php
2
3
namespace ABM\Wasabi;
4
5
use Analog\Analog;
6
7
class Product extends Prestashop
8
{
9
  /**
10
     * @param string $data
11
     */
12
    private function getProductData($data)
13
    {
14
        $category = (int) substr($data, 0, strpos($data, ','));
15
        if ($category != 0) {
16
            $products = $this->getProducts($category);
17
        } else {
18
            $product = substr($data, strpos($data, ',') + 1);
19
            $products = $this->getProducts($product);
20
        }
21
        Analog::log("Product variables: $data");
22
23
        return $products;
24
    }
25
26
    /**
27
     * @param string $category
28
     */
29
    private function getProducts($category)
30
    {
31
        $product_ids = $this->getProductIDs($category);
32
        $products = $this->getProduct($product_ids);
33
34
        return $products;
35
    }
36
37
    /**
38
     * @param string $category
39
     */
40
    private function getProductIDs($category)
41
    {
42
        $sql = 'SELECT DISTINCT p.id_product
43
                from '._DB_PREFIX_.'product as p
44
                LEFT JOIN '._DB_PREFIX_.'image AS i ON i.id_product = p.id_product 
45
                LEFT JOIN '._DB_PREFIX_.'product_lang as pl ON pl.id_product = p.id_product
46
                WHERE p.active = 1
47
                AND p.id_category_default = '.(int) $category.'
48
                GROUP BY p.id_product';
49
        $pcats = $this->dbConn->fetchRowMany($sql);
50
        $ids = '';
51
        if (is_array($pcats) && (!empty($pcats))) {
52
            foreach ($pcats as $row) {
53
                $ids .= $row['id_product'].',';
54
            }
55
        }
56
57
        $ids = rtrim($ids, ',');
58
59
        return $ids;
60
    }
61
62
    /**
63
     * @param string $ids
64
     */
65
    private function getProduct($ids)
66
    {
67
        $sql = 'SELECT p.id_product, p.id_supplier, p.ean13, p.upc, p.price, p.wholesale_price, p.on_sale, p.quantity, p.id_category_default,
68
                    p.show_price, p.available_for_order, p.minimal_quantity, p.customizable,
69
                    p.out_of_stock, pl.link_rewrite, pl.name, i.id_image, il.legend
70
                    FROM '._DB_PREFIX_.'product as p                 
71
                    LEFT JOIN '._DB_PREFIX_.'image AS i ON i.id_product = p.id_product 
72
                    LEFT JOIN '._DB_PREFIX_.'image_lang as il ON i.id_image = il.id_image
73
                    WHERE p.id_product IN ('.$ids.')
74
                    AND i.cover = 1
75
                    AND p.active = 1
76
                    GROUP BY p.id_product
77
                    ORDER BY p.price ASC';
78
79
        $result = $this->dbConn->fetchRowMany($sql);
80
81
        if (is_array($result)) {
82
            foreach ($result as $key => $value) {
83
                $result['cat_id'] = $value['id_category_default'];
84
                $result['orderprice'] = $this->getOrderPrice($value['id_product']);
85
                $result['category_default'] = $this->getProductCat($value['id_category_default']);
86
            }
87
88
            return $result;
89
        }
90
    }
91
92 View Code Duplication
    private function getOrderPrice($product)
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...
93
    {
94
        $sql = 'SELECT ps.price from '._DB_PREFIX_.'product_shop as ps WHERE ps.id_product = '.(int) $product;
95
        $result = $this->dbConn->fetchColumn($sql);
96
97
        return $result;
98
    }
99
100
    /**
101
     * @param string $product
102
     */
103 View Code Duplication
    private function getProductName($product)
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...
104
    {
105
        $sql = 'SELECT pl.name from '._DB_PREFIX_.'product_lang as pl WHERE pl.id_product = '.(int) $product;
106
        $result = $this->dbConn->fetchColumn($sql);
107
108
        return $result;
109
    }
110
111 View Code Duplication
    private function getProductCat($category)
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...
112
    {
113
        $sql = 'SELECT cl.name from '._DB_PREFIX_.'category_lang as cl WHERE cl.id_category = '.(int) $category;
114
        $result = $this->dbConn->fetchColumn($sql);
115
116
        return $result;
117
    }
118
119
}
120