Choice::populate()   B
last analyzed

Complexity

Conditions 10
Paths 48

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 3
Bugs 2 Features 2
Metric Value
c 3
b 2
f 2
dl 0
loc 20
ccs 0
cts 18
cp 0
rs 7.2765
cc 10
eloc 13
nc 48
nop 3
crap 110

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace SpeckCatalog\Service;
4
5
class Choice extends AbstractService
6
{
7
    protected $entityMapper = 'speckcatalog_choice_mapper';
8
    protected $optionService;
9
    protected $productService;
10
11
    public function getByOptionId($optionId, $populate = false, $recursive = false)
12
    {
13
        $choices = $this->getEntityMapper()->getByOptionId($optionId);
14
        if ($populate) {
15
            foreach ($choices as $choice) {
16
                $this->populate($choice, $recursive);
17
            }
18
        }
19
        return $choices;
20
    }
21
22
    public function populate($choice, $recursive = false, $children = true)
23
    {
24
        $allChildren = ($children === true) ? true : false;
25
        $children    = (is_array($children)) ? $children : array();
26
27
        if ($allChildren || in_array('parent', $children)) {
28
            $option = $this->getOptionService()->find(array('option_id' => $choice->getOptionId()));
29
            $choice->setParent($option);
30
        }
31
        if ($allChildren || in_array('options', $children)) {
32
            $options = $this->getOptionService()->getByParentChoiceId($choice->getChoiceId(), $recursive);
33
            $choice->setOptions($options);
34
        }
35
        if ($allChildren || in_array('product', $children)) {
36
            if ($choice->getProductId()) {
37
                $product = $this->getProductService()->getFullProduct($choice->getProductId());
38
                $choice->setProduct($product);
39
            }
40
        }
41
    }
42
43
    public function addOption($choiceOrId, $optionOrId)
44
    {
45
        $choiceId = ( is_int($choiceOrId) ? $choiceOrId : $choiceOrId->getChoiceId() );
46
        $optionId = ( is_int($optionOrId) ? $optionOrId : $optionOrId->getOptionId() );
47
        $this->getEntityMapper()->addOption($choiceId, $optionId);
48
        return $this->getOptionService()->find(array('option_id' => $optionId));
49
    }
50
51
    public function persist($form)
52
    {
53
        $data = $form->getData();
54
        $orig = $form->getOriginalData();
55
56
        if (isset($data['choice_id']) && (int) $data['choice_id'] > 0) {
57
            return $this->update($data, $orig);
58
        }
59
60
        $model = $this->insert($data);
61
        return $model;
62
    }
63
64
    public function sortOptions($choiceId, $order)
65
    {
66
        return $this->getEntityMapper()->sortOptions($choiceId, $order);
67
    }
68
69
    public function removeOption($choiceOrId, $optionOrId)
70
    {
71
        $productId = ( is_int($choiceOrId) ? $choiceOrId : $choiceOrId->getProductId() );
72
        $optionId  = ( is_int($optionOrId) ? $optionOrId : $optionOrId->getOptionId() );
73
        $this->getEntityMapper()->removeOption($productId, $optionId);
74
    }
75
76
    public function choiceFromProduct($productOrId)
77
    {
78
        $product = ( !is_int($productOrId) ? $productOrId : $this->getProductService()->find($productOrId) );
79
        $choice = $this->getEntity()->setProductId($product->getProductId());
0 ignored issues
show
Bug introduced by
The method getEntity() does not exist on SpeckCatalog\Service\Choice. Did you maybe mean getEntityMapper()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
        return $this->persist($choice);
81
    }
82
83
    /**
84
     * @return optionService
85
     */
86
    public function getOptionService()
87
    {
88
        if (null === $this->optionService) {
89
            $this->optionService = $this->getServiceLocator()->get('speckcatalog_option_service');
90
        }
91
        return $this->optionService;
92
    }
93
94
    /**
95
     * @param $optionService
96
     * @return self
97
     */
98
    public function setOptionService($optionService)
99
    {
100
        $this->optionService = $optionService;
101
        return $this;
102
    }
103
104
    /**
105
     * @return productService
106
     */
107
    public function getProductService()
108
    {
109
        if (null === $this->productService) {
110
            $this->productService = $this->getServiceLocator()->get('speckcatalog_product_service');
111
        }
112
        return $this->productService;
113
    }
114
115
    /**
116
     * @param $productService
117
     * @return self
118
     */
119
    public function setProductService($productService)
120
    {
121
        $this->productService = $productService;
122
        return $this;
123
    }
124
}
125