-
Notifications
You must be signed in to change notification settings - Fork 0
/
UpdateProduct.php
53 lines (38 loc) · 1.23 KB
/
UpdateProduct.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Northstyle\Module\Shop\Behavior;
use Northstyle\Common\Behavior;
use Northstyle\Module\Core\Model\ObjectValue as ObjectValueModel;
use Northstyle\Module\Shop\Model\Product as Model;
use Northstyle\Module\Shop\Behavior\DataObject\UpdateProduct as UpdateBehaviorDO;
class UpdateProduct extends Behavior {
protected $productModel = null;
public function __construct(Model $productModel) {
$this->productModel = $productModel;
}
public function loadProductEntityById($id) {
$entity = $this->productModel->find($id);
if (!$entity) {
throw new EntityNotFoundException('Entity Not Found');
}
return $entity;
}
public function valueEntity($type, $valueDO) {
$valueEntity = new ObjectValueModel();
$valueEntity->object_type = $type;
$valueEntity->name = $valueDO->name;
$valueEntity->value = $valueDO->value;
return $valueEntity;
}
public function handle(UpdateBehaviorDO $do) {
\DB::transaction(function() use ($do) {
$entity = $this->loadProductEntityById($do->id->value());
$entity->save();
if (count($do->values)) {
$entity->values()->delete();
foreach ($do->values as $valueDO) {
$entity->values()->save($this->valueEntity(Model::OBJECT_TYPE, $valueDO));
}
}
});
}
}