-
Notifications
You must be signed in to change notification settings - Fork 513
/
ProductSaver.php
117 lines (92 loc) · 3.59 KB
/
ProductSaver.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Pim\Bundle\CatalogBundle\Doctrine\Common\Saver;
use Akeneo\Component\StorageUtils\Saver\BulkSaverInterface;
use Akeneo\Component\StorageUtils\Saver\SaverInterface;
use Akeneo\Component\StorageUtils\StorageEvents;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\Util\ClassUtils;
use Pim\Component\Catalog\Manager\CompletenessManager;
use Pim\Component\Catalog\Model\ProductInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
/**
* Product saver, define custom logic and options for product saving
*
* @author Nicolas Dupont <nicolas@akeneo.com>
* @copyright 2014 Akeneo SAS (http://www.akeneo.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
class ProductSaver implements SaverInterface, BulkSaverInterface
{
/** @var ObjectManager */
protected $objectManager;
/** @var CompletenessManager */
protected $completenessManager;
/** @var EventDispatcherInterface */
protected $eventDispatcher;
/**
* @param ObjectManager $om
* @param CompletenessManager $completenessManager
* @param EventDispatcherInterface $eventDispatcher
*/
public function __construct(
ObjectManager $om,
CompletenessManager $completenessManager,
EventDispatcherInterface $eventDispatcher
) {
$this->objectManager = $om;
$this->completenessManager = $completenessManager;
$this->eventDispatcher = $eventDispatcher;
}
/**
* {@inheritdoc}
*/
public function save($product, array $options = [])
{
$this->validateProduct($product);
$options['unitary'] = true;
$this->eventDispatcher->dispatch(StorageEvents::PRE_SAVE, new GenericEvent($product, $options));
$this->completenessManager->schedule($product);
$this->objectManager->persist($product);
$this->objectManager->flush();
$this->completenessManager->generateMissingForProduct($product);
$this->eventDispatcher->dispatch(StorageEvents::POST_SAVE, new GenericEvent($product, $options));
}
/**
* {@inheritdoc}
*/
public function saveAll(array $products, array $options = [])
{
if (empty($products)) {
return;
}
$options['unitary'] = false;
$this->eventDispatcher->dispatch(StorageEvents::PRE_SAVE_ALL, new GenericEvent($products, $options));
foreach ($products as $product) {
$this->validateProduct($product);
$this->eventDispatcher->dispatch(StorageEvents::PRE_SAVE, new GenericEvent($product, $options));
$this->completenessManager->schedule($product);
$this->objectManager->persist($product);
}
$this->objectManager->flush();
foreach ($products as $product) {
$this->completenessManager->generateMissingForProduct($product);
$this->eventDispatcher->dispatch(StorageEvents::POST_SAVE, new GenericEvent($product, $options));
}
$this->eventDispatcher->dispatch(StorageEvents::POST_SAVE_ALL, new GenericEvent($products, $options));
}
/**
* @param $product
*/
protected function validateProduct($product)
{
if (!$product instanceof ProductInterface) {
throw new \InvalidArgumentException(
sprintf(
'Expects a Pim\Component\Catalog\Model\ProductInterface, "%s" provided',
ClassUtils::getClass($product)
)
);
}
}
}