FrontendEnabled   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 16
lcom 0
cbo 0
dl 0
loc 116
ccs 0
cts 81
cp 0
rs 10
c 3
b 1
f 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A canProductBeEnabled() 0 12 4
A insertProductUom() 0 18 3
B updateProductUom() 0 24 3
A updateProduct() 0 19 3
A insertProduct() 0 20 3
1
<?php
2
3
namespace SpeckCatalog\Event;
4
5
class FrontendEnabled
6
{
7
    /*
8
     * After insert/update of a productuom,
9
     * check all uoms belonging to that product,
10
     * if any productuom is enabled, enable the product,
11
     * otherwise, disable it.
12
     *
13
     */
14
15
    public function insertProductUom($e)
16
    {
17
        $dbResult = $e->getParam('result');
18
        if (!count($dbResult)) {
19
            return; //no rows affected
20
        }
21
22
        $productId      = is_array($e->getParam('data'))
23
                        ? $e->getParam('data')['product_id']
24
                        : $e->getParam('data')->getProductId();
25
        $productService = $e->getTarget()->getProductService();
26
        $find           = array('product_id' => $productId);
27
        $product        = $productService->find($find, array('uoms'));
28
29
        $enabled = $this->canProductBeEnabled($product);
30
31
        $productService->setEnabledProduct($productId, $enabled);
32
    }
33
34
    public function updateProductUom($e)
35
    {
36
        $productUomService = $e->getTarget();
37
        $productService    = $productUomService->getProductService();
38
39
        $where = $e->getParam('where');
40
        $uoms  = $productUomService->findRows($where);
41
42
        $ids = array();
43
        foreach ($uoms as $uom) {
44
            $ids[$uom['product_id']] = $uom['product_id'];
45
        }
46
        $products = $productService->getProductsById($ids);
47
48
        foreach ($products as $product) {
49
            $productService->populate($product, array('uoms'));
50
            $row = array(
51
                'product_id' => $productId,
0 ignored issues
show
Bug introduced by
The variable $productId does not exist. Did you mean $product?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
52
                'enabled'    => $this->canProductBeEnabled($product)
53
            );
54
            $where = array('product_id' => $productId);
0 ignored issues
show
Bug introduced by
The variable $productId does not exist. Did you mean $product?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
55
            $productService->update($row, $where);
56
        }
57
    }
58
59
    public function canProductBeEnabled($product)
60
    {
61
        if ($product->getProductTypeId() == 2) {
62
            return true;
63
        }
64
        foreach ($product->getUoms() as $uom) {
65
            if ($uom->getEnabled()) {
66
                return true;
67
            }
68
        }
69
        return false;
70
    }
71
72
    /*
73
     * After insert/update of product(s),
74
     * find any choices that reference the product,
75
     * if the product is enabled, enable the choice,
76
     * otherwise, disable it.
77
     *
78
     */
79
80
    public static function updateProduct($e)
81
    {
82
        $productService = $e->getTarget();
83
        $choiceService  = $productService->getChoiceService();
84
85
        $where = $e->getParam('where');
86
        $products = $productService->findMany($where, array('uoms'));
87
88
        foreach ($products as $product) {
89
            $productId = $product->getProductId();
90
            $enabled   = ($product->getEnabled()) ? 1 : 0;
91
            $choiceService->update(
92
                array('enabled'    => $enabled), //data
93
                array('product_id' => $productId)    //where
94
            );
95
        }
96
97
        $productIds = $e->getTarget();
0 ignored issues
show
Unused Code introduced by
$productIds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
    }
99
100
    public static function insertProduct($e)
101
    {
102
        $result = $e->getParam('result');
103
        if (!$result) {
104
            return;
105
        }
106
        $productId      = $result;
107
        $data           = array('product_id' => $productId);
108
109
        $productService = $e->getTarget();
110
        $product        = $productService->find($data);
111
112
        $enabled        = ($product->getEnabled()) ? 1 : 0;
113
114
        $choiceService  = $productService->getChoiceService();
115
        $choiceService->update(
116
            array('enabled'    => $enabled), //data
117
            array('product_id' => $productId)    //where
118
        );
119
    }
120
}
121