Choice   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 3 Features 3
Metric Value
wmc 31
lcom 1
cbo 2
dl 0
loc 120
ccs 0
cts 65
cp 0
rs 9.8
c 5
b 3
f 3

11 Methods

Rating   Name   Duplication   Size   Complexity  
A sortOptions() 0 4 1
A getByOptionId() 0 10 3
B populate() 0 20 10
A addOption() 0 7 3
A persist() 0 12 3
A removeOption() 0 6 3
A choiceFromProduct() 0 6 2
A getOptionService() 0 7 2
A setOptionService() 0 5 1
A getProductService() 0 7 2
A setProductService() 0 5 1
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