Completed
Push — development ( f4b02b...227e3a )
by Andrij
11:06
created

modules/mod_discount/discount_product.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace mod_discount;
4
5
if (!defined('BASEPATH')) {
6
    exit('No direct script access allowed');
7
}
8
9
/**
10
 * Class Discount_product for Mod_Discount module
11
 * @author DevImageCms
12
 * @copyright (c) 2013, ImageCMS
13
 * @package ImageCMSModule
14
 * @property discount_model_front $discount_model_front
15
 */
16 View Code Duplication
class Discount_product
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type mod_discount\Discount_product has been defined more than once; this definition is ignored, only the first definition in application/modules/mod_...nt/Discount_product.php (L20-147) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
17
{
18
19
    private $discountForProduct;
20
21
    private static $object;
22
23
    /**
24
     * singelton method
25
     * @return object BaseDiscount
26
     */
27
    public static function create() {
28
        if (!self::$object) {
29
            self::$object = new self;
30
        }
31
        return self::$object;
32
    }
33
34
    /**
35
     * __construct base object loaded
36
     * @access private
37
     * @author DevImageCms
38
     * @param ---
39
     * @return ---
40
     * @copyright (c) 2013, ImageCMS
41
     */
42
    private function __construct() {
43
        $this->ci = & get_instance();
44
        $lang = new \MY_Lang();
45
        $lang->load('mod_discount');
46
        include_once __DIR__ . '/models/discount_model_front.php';
0 ignored issues
show
The property ci does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
47
        $this->ci->discount_model_front = new \discount_model_front;
48
        $this->baseDiscount = \mod_discount\classes\BaseDiscount::create();
49
        $this->discountForProduct = array_merge($this->baseDiscount->discountType['product'], $this->baseDiscount->discountType['brand'], $this->createChildDiscount($this->baseDiscount->discountType['category']));
50
    }
51
0 ignored issues
show
The property baseDiscount does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
52
    /**
53
     * create child discount
54
     * @access private
55
     * @author DevImageCms
56
     * @param array
57
     * @return array
58
     * @copyright (c) 2013, ImageCMS
59
     */
60
    private function createChildDiscount($discount) {
61
62
        if (count($discount) > 0) {
63
            $resultDiscount = [];
64
            foreach ($discount as $disc) {
65
                $resultDiscount[] = $disc;
66
                if ($disc['child']) {
67
                    $childs = $this->ci->db->like('full_path_ids', ':' . $disc['category_id'] . ';')->get('shop_category')->result_array();
68
                    if (count($childs) > 0) {
69
                        foreach ($childs as $child) {
70
                            $discAux = $disc;
71
                            $discAux['category_id'] = $child['id'];
72
                            $resultDiscount[] = $discAux;
73
                        }
74
                    }
75
                }
76
            }
77
78
            return $resultDiscount;
79
        } else {
80
            return $discount;
81
        }
82
    }
83
84
    /**
85
     * get product discount for prouct_id and product_vid
86
     * @access public
87
     * @author DevImageCms
88
     * @param array product [id,vid]
89
     * @return boolean
90
     * @copyright (c) 2013, ImageCMS
91
     */
92
    public function getProductDiscount($product, $price = null) {
93
94
        $discountArray = $this->getDiscountOneProduct($product);
95
96
        if (count($discountArray) > 0) {
97
            if (null === $price) {
98
                $price = $this->ci->discount_model_front->getPrice($product['vid']);
99
            }
100
            $discountMax = $this->baseDiscount->getMaxDiscount($discountArray, $price);
101
            $discountValue = $this->baseDiscount->getDiscountValue($discountMax, $price);
102
        } else {
103
            \CMSFactory\assetManager::create()->discount = false;
104
            return false;
105
        }
106
107
        \CMSFactory\assetManager::create()->discount = [
108
            'discoun_all_product' => $discountArray,
109
            'discount_max' => $discountMax,
110
            'discount_value' => $discountValue,
111
            'price' => $price
112
        ];
113
114
        return true;
115
    }
116
117
    /**
118
     * get product discount for one prouct
119
     * @access private
120
     * @author DevImageCms
121
     * @param array product [product_id,brand_id,category_id]
122
     * @return array
123
     * @copyright (c) 2013, ImageCMS
124
     */
125
    private function getDiscountOneProduct($product) {
126
127
        $arrDiscount = [];
128
129
        foreach ($this->discountForProduct as $disc) {
130
            foreach ($product as $key => $value) {
131
                if ($disc[$key]) {
132
                    if ($disc[$key] == $value) {
133
                        $arrDiscount[] = $disc;
134
                    }
135
                }
136
            }
137
        }
138
139
        return $arrDiscount;
140
    }
141
142
}