|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SpeckCatalog\Service; |
|
4
|
|
|
|
|
5
|
|
|
use SpeckCatalog\Model\AbstractModel; |
|
6
|
|
|
|
|
7
|
|
|
class ProductUom extends AbstractService |
|
8
|
|
|
{ |
|
9
|
|
|
protected $entityMapper = 'speckcatalog_product_uom_mapper'; |
|
10
|
|
|
protected $availabilityService; |
|
11
|
|
|
protected $uomService; |
|
12
|
|
|
protected $productService; |
|
13
|
|
|
|
|
14
|
1 |
|
public function getByProductId($productId, $populate = false, $recursive = false) |
|
15
|
|
|
{ |
|
16
|
1 |
|
$productUoms = $this->getEntityMapper()->getByProductId($productId); |
|
17
|
1 |
|
if ($populate) { |
|
18
|
1 |
|
foreach ($productUoms as $productUom) { |
|
19
|
1 |
|
$this->populate($productUom, $recursive); |
|
20
|
1 |
|
} |
|
21
|
1 |
|
} |
|
22
|
1 |
|
return $productUoms; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function cheapestUom(array $uoms) |
|
26
|
|
|
{ |
|
27
|
|
|
$lowest = null; |
|
28
|
|
|
foreach ($uoms as $uom) { |
|
29
|
|
|
if (!$lowest) { |
|
30
|
|
|
$lowest = $uom; |
|
31
|
|
|
} |
|
32
|
|
|
if ($uom->getPrice() < $lowest->getPrice()) { |
|
33
|
|
|
$lowest = $uom; |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $lowest; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
public function populate($productUom, $recursive = false, $children = true) |
|
41
|
|
|
{ |
|
42
|
2 |
|
$allChildren = ($children === true) ? true : false; |
|
43
|
2 |
|
$children = (is_array($children)) ? $children : array(); |
|
44
|
|
|
|
|
45
|
2 |
|
if ($allChildren || in_array('availabilities', $children)) { |
|
46
|
2 |
|
$availabilities = $this->getAvailabilityService()->getByProductUom( |
|
47
|
2 |
|
$productUom->getProductId(), |
|
48
|
2 |
|
$productUom->getUomCode(), |
|
49
|
2 |
|
$productUom->getQuantity() |
|
50
|
2 |
|
); |
|
51
|
2 |
|
if ($recursive) { |
|
52
|
|
|
foreach ($availabilities as $i => $avail) { |
|
53
|
|
|
$this->getAvailabilityService()->populate($avail); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
2 |
|
$productUom->setAvailabilities($availabilities); |
|
57
|
2 |
|
} |
|
58
|
2 |
|
if ($allChildren || in_array('uom', $children)) { |
|
59
|
2 |
|
$uom = $this->getUomService()->find(array('uom_code' => $productUom->getUomCode())); |
|
60
|
2 |
|
$productUom->setUom($uom); |
|
61
|
2 |
|
} |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return availabilityService |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function getAvailabilityService() |
|
68
|
|
|
{ |
|
69
|
4 |
|
if (null === $this->availabilityService) { |
|
70
|
1 |
|
$this->availabilityService = $this->getServiceLocator()->get('speckcatalog_availability_service'); |
|
71
|
1 |
|
} |
|
72
|
4 |
|
return $this->availabilityService; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param $availabilityService |
|
77
|
|
|
* @return self |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function setAvailabilityService($availabilityService) |
|
80
|
|
|
{ |
|
81
|
3 |
|
$this->availabilityService = $availabilityService; |
|
82
|
3 |
|
return $this; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return uomService |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function getUomService() |
|
89
|
|
|
{ |
|
90
|
4 |
|
if (null === $this->uomService) { |
|
91
|
1 |
|
$this->uomService = $this->getServiceLocator()->get('speckcatalog_uom_service'); |
|
92
|
1 |
|
} |
|
93
|
4 |
|
return $this->uomService; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param $uomService |
|
98
|
|
|
* @return self |
|
99
|
|
|
*/ |
|
100
|
3 |
|
public function setUomService($uomService) |
|
101
|
|
|
{ |
|
102
|
3 |
|
$this->uomService = $uomService; |
|
103
|
3 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function update($data, array $where = null) |
|
107
|
|
|
{ |
|
108
|
|
|
$vars = array( |
|
109
|
|
|
'data' => $data, |
|
110
|
|
|
'where' => $where, |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
$this->getEventManager()->trigger('update.pre', $this, $vars); |
|
114
|
|
|
|
|
115
|
|
|
$result = parent::update($data, $where); |
|
116
|
|
|
$vars['result'] = $result; |
|
117
|
|
|
|
|
118
|
|
|
$this->getEventManager()->trigger('update.post', $this, $vars); |
|
119
|
|
|
|
|
120
|
|
|
return $result; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
2 |
|
public function insert($productUom) |
|
124
|
|
|
{ |
|
125
|
2 |
|
if ($productUom instanceof AbstractModel) { |
|
126
|
|
|
$data = array( |
|
127
|
1 |
|
'uom_code' => $productUom->getUomCode(), |
|
|
|
|
|
|
128
|
1 |
|
'product_id' => $productUom->getProductId(), |
|
|
|
|
|
|
129
|
1 |
|
'quantity' => $productUom->getQuantity(), |
|
|
|
|
|
|
130
|
1 |
|
); |
|
131
|
2 |
|
} elseif (is_array($productUom)) { |
|
132
|
1 |
|
$data = $productUom; |
|
133
|
1 |
|
} |
|
134
|
|
|
|
|
135
|
|
|
$vars = array( |
|
136
|
2 |
|
'data' => $data, |
|
|
|
|
|
|
137
|
2 |
|
); |
|
138
|
2 |
|
$this->getEventManager()->trigger('insert.pre', $this, $vars); |
|
139
|
|
|
|
|
140
|
2 |
|
$vars['result'] = parent::insert($productUom); |
|
141
|
|
|
|
|
142
|
2 |
|
$this->getEventManager()->trigger('insert.post', $this, $vars); |
|
143
|
|
|
|
|
144
|
2 |
|
$productUom = $this->find($data); |
|
145
|
2 |
|
return $productUom; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @return productService |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getProductService() |
|
152
|
|
|
{ |
|
153
|
|
|
if (null === $this->productService) { |
|
154
|
|
|
$this->productService = $this->getServiceLocator()->get('speckcatalog_product_service'); |
|
155
|
|
|
} |
|
156
|
|
|
return $this->productService; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $productService |
|
161
|
|
|
* @return self |
|
162
|
|
|
*/ |
|
163
|
|
|
public function setProductService($productService) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->productService = $productService; |
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: