1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright © 2016-present Spryker Systems GmbH. All rights reserved. |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Spryker\Zed\ProductValidity\Business\ProductConcrete; |
9
|
|
|
|
10
|
|
|
use Orm\Zed\ProductValidity\Persistence\Map\SpyProductValidityTableMap; |
11
|
|
|
use Spryker\Zed\ProductValidity\Dependency\Facade\ProductValidityToProductFacadeInterface; |
12
|
|
|
use Spryker\Zed\ProductValidity\Persistence\ProductValidityQueryContainerInterface; |
13
|
|
|
use Spryker\Zed\PropelOrm\Business\Transaction\DatabaseTransactionHandlerTrait; |
14
|
|
|
use Traversable; |
15
|
|
|
|
16
|
|
|
class ProductConcreteSwitcher implements ProductConcreteSwitcherInterface |
17
|
|
|
{ |
18
|
|
|
use DatabaseTransactionHandlerTrait; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Spryker\Zed\ProductValidity\Persistence\ProductValidityQueryContainerInterface |
22
|
|
|
*/ |
23
|
|
|
protected $queryContainer; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Spryker\Zed\ProductValidity\Dependency\Facade\ProductValidityToProductFacadeInterface |
27
|
|
|
*/ |
28
|
|
|
protected $productFacade; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param \Spryker\Zed\ProductValidity\Persistence\ProductValidityQueryContainerInterface $queryContainer |
32
|
|
|
* @param \Spryker\Zed\ProductValidity\Dependency\Facade\ProductValidityToProductFacadeInterface $productFacade |
33
|
|
|
*/ |
34
|
|
|
public function __construct( |
35
|
|
|
ProductValidityQueryContainerInterface $queryContainer, |
36
|
|
|
ProductValidityToProductFacadeInterface $productFacade |
37
|
|
|
) { |
38
|
|
|
$this->queryContainer = $queryContainer; |
39
|
|
|
$this->productFacade = $productFacade; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return void |
44
|
|
|
*/ |
45
|
|
|
public function updateProductsValidity(): void |
46
|
|
|
{ |
47
|
|
|
if (!$this->hasTriggeredProductValidity()) { |
48
|
|
|
return; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$this->handleDatabaseTransaction(function () { |
52
|
|
|
$productsBecomingActive = $this->findProductsBecomingActive(); |
53
|
|
|
$productsBecomingInactive = $this->findProductsBecomingInactive(); |
54
|
|
|
|
55
|
|
|
$this->executeProductPublishTransaction($productsBecomingActive, $productsBecomingInactive); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
protected function hasTriggeredProductValidity(): bool |
63
|
|
|
{ |
64
|
|
|
$willProductsBecomeValid = $this |
65
|
|
|
->queryContainer |
66
|
|
|
->queryProductsBecomingValid() |
67
|
|
|
->select([SpyProductValidityTableMap::COL_ID_PRODUCT_VALIDITY]) |
68
|
|
|
->findOne(); |
69
|
|
|
|
70
|
|
|
if ($willProductsBecomeValid) { |
71
|
|
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$willProductsBecomeInvalid = $this |
75
|
|
|
->queryContainer |
76
|
|
|
->queryProductsBecomingInvalid() |
77
|
|
|
->select([SpyProductValidityTableMap::COL_ID_PRODUCT_VALIDITY]) |
78
|
|
|
->findOne(); |
79
|
|
|
|
80
|
|
|
if ($willProductsBecomeInvalid) { |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> |
89
|
|
|
*/ |
90
|
|
|
protected function findProductsBecomingActive(): Traversable |
91
|
|
|
{ |
92
|
|
|
return $this |
93
|
|
|
->queryContainer |
94
|
|
|
->queryProductsBecomingValid() |
95
|
|
|
->find(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> |
100
|
|
|
*/ |
101
|
|
|
protected function findProductsBecomingInactive(): Traversable |
102
|
|
|
{ |
103
|
|
|
return $this |
104
|
|
|
->queryContainer |
105
|
|
|
->queryProductsBecomingInvalid() |
106
|
|
|
->find(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> $productsBecomingActive |
111
|
|
|
* @param \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> $productsBecomingInactive |
112
|
|
|
* |
113
|
|
|
* @return void |
114
|
|
|
*/ |
115
|
|
|
protected function executeProductPublishTransaction( |
116
|
|
|
Traversable $productsBecomingActive, |
117
|
|
|
Traversable $productsBecomingInactive |
118
|
|
|
): void { |
119
|
|
|
$this->activateProductConcretes($productsBecomingActive); |
120
|
|
|
$this->deactivateProductConcretes($productsBecomingInactive); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> $productValidityEntities |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
|
|
protected function activateProductConcretes(Traversable $productValidityEntities): void |
129
|
|
|
{ |
130
|
|
|
foreach ($productValidityEntities as $productValidityEntity) { |
131
|
|
|
$this->productFacade |
132
|
|
|
->activateProductConcrete( |
133
|
|
|
$productValidityEntity->getFkProduct(), |
134
|
|
|
); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param \Traversable<\Orm\Zed\ProductValidity\Persistence\SpyProductValidity> $productValidityEntities |
140
|
|
|
* |
141
|
|
|
* @return void |
142
|
|
|
*/ |
143
|
|
|
protected function deactivateProductConcretes(Traversable $productValidityEntities): void |
144
|
|
|
{ |
145
|
|
|
foreach ($productValidityEntities as $productValidityEntity) { |
146
|
|
|
$this->productFacade |
147
|
|
|
->deactivateProductConcrete( |
148
|
|
|
$productValidityEntity->getFkProduct(), |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
This trait has been deprecated. The supplier of the trait has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the trait will be removed and what other trait to use instead.