Product::allUomsString()   C
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 0
cts 19
cp 0
rs 6.9811
cc 7
eloc 12
nc 6
nop 2
crap 56
1
<?php
2
3
namespace SpeckCatalog\View\Helper;
4
5
use Zend\View\Helper\AbstractHelper;
6
7
class Product extends AbstractHelper
8
{
9
    protected $productUomService;
10
11
    public function __invoke()
12
    {
13
        return $this;
14
    }
15
16
    public function uomString($product)
17
    {
18
        if ($product->has('uoms')) {
19
            $svc = $this->getProductUomService();
20
            $uom = $svc->cheapestUom($product->getUoms());
21
            $svc->populate($uom);
22
            $moreThan1 = ($uom->getQuantity() > 1);
23
24
            $string = $uom->getUom()->getName();
25
            if ($moreThan1) {
26
                $string .= ' of ' . $uom->getQuantity();
27
            }
28
            return $string;
29
        }
30
        // @TODO: if there are builders, get the uomString from the one we are
31
        //displaying the price from
32
        return 'Starting At';
33
    }
34
35
36
37
    public function allUomsString($product, $string = true)
38
    {
39
        $uomStrings = array();
40
41
        if ($product->type() === 'product') {
42
            foreach ($uoms as $uom) {
0 ignored issues
show
Bug introduced by
The variable $uoms does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
43
                $uomStrings[$this->uomStr($uom)] = $this->uomName($uom);
44
            }
45
        } elseif ($product->type() === 'shell') {
46
            foreach ($product->getBuilders() as $builder) {
47
                foreach ($builder->getProduct()->getUoms() as $uom) {
48
                    $uomStrings[$this->uomStr($uom)] = $this->uomName($uom);
49
                }
50
            }
51
        }
52
53
        if ($string) {
54
            return implode(',', $uomStrings);
55
        }
56
57
        return $uomStrings;
58
    }
59
60
    public function uomStr($uom)
61
    {
62
        return $uom->getUomCode() . $uom->getQuantity();
63
    }
64
65
    public function uomName($uom)
66
    {
67
        if ($uom->getQuantity() === 1) {
68
            return $uom->getUom()->getName();
69
        }
70
        return "{$uom->getUom()->getName()} of {$uom->getQuantity()}";
71
    }
72
73
    public function itemNumbers($product, $implode = false)
74
    {
75
        $numbers = array();
76
        if ($product->getProductTypeId() == 2) {
77
            $numbers[$product->getItemNumber()] = $product->getItemNumber();
78
        } else {
79
            foreach ($product->getBuilders() as $builder) {
80
                $bp = $builder->getProduct();
81
                $numbers[$bp->getItemNumber()] = $bp->getItemNumber();
82
            }
83
        }
84
        if ($implode) {
85
            return implode($implode, $numbers);
86
        }
87
        return $numbers;
88
    }
89
90
    public function getProductUomService()
91
    {
92
        return $this->productUomService;
93
    }
94
95
    public function setProductUomService($productUomService)
96
    {
97
        $this->productUomService = $productUomService;
98
        return $this;
99
    }
100
}
101