1 | <?php |
||
22 | class ProxyEntityWriter implements |
||
23 | ItemWriterInterface, |
||
24 | StepExecutionAwareInterface, |
||
25 | StepExecutionRestoreInterface, |
||
26 | LoggerAwareInterface |
||
27 | { |
||
28 | use LoggerAwareTrait; |
||
29 | |||
30 | /** @var ItemWriterInterface */ |
||
31 | protected $writer; |
||
32 | |||
33 | /** @var DatabaseHelper */ |
||
34 | protected $databaseHelper; |
||
35 | |||
36 | /** @var StepExecution|null */ |
||
37 | protected $previousStepExecution; |
||
38 | |||
39 | /** |
||
40 | * @param ItemWriterInterface $writer |
||
41 | * @param DatabaseHelper $databaseHelper |
||
42 | */ |
||
43 | public function __construct(ItemWriterInterface $writer, DatabaseHelper $databaseHelper) |
||
44 | { |
||
45 | $this->writer = $writer; |
||
46 | $this->databaseHelper = $databaseHelper; |
||
47 | $this->logger = new NullLogger(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | * |
||
53 | * Prepare items for PersistentBatchWriter, filters for duplicates and takes only latest versions |
||
54 | */ |
||
55 | public function write(array $items) |
||
56 | { |
||
57 | $uniqueItems = []; |
||
58 | $uniqueKeys = []; |
||
59 | foreach ($items as $item) { |
||
60 | if ($item instanceof Customer || $item instanceof Cart) { |
||
61 | $this->handleIdentifier($uniqueItems, $item, $item->getOriginId()); |
||
62 | } elseif ($item instanceof Order) { |
||
63 | $this->handleIdentifier($uniqueItems, $item, $item->getIncrementId()); |
||
64 | } elseif ($item instanceof NewsletterSubscriber) { |
||
65 | $identifier = $item->getCustomer() ? $item->getCustomer()->getId() : 0; |
||
66 | if ($identifier !== 0 && in_array($identifier, $uniqueKeys)) { |
||
67 | $this->logSkipped($item->getOriginId()); |
||
68 | } else { |
||
69 | $uniqueKeys[] = $identifier; |
||
70 | $uniqueItems[] = $item; |
||
71 | } |
||
72 | |||
73 | } else { |
||
74 | $uniqueItems[] = $item; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | $this->writer->write($uniqueItems); |
||
79 | |||
80 | // force entity cache clear if clear is skipped |
||
81 | $this->databaseHelper->onClear(); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | public function setStepExecution(StepExecution $stepExecution) |
||
88 | { |
||
89 | if ($this->writer instanceof StepExecutionAwareInterface) { |
||
90 | $this->writer->setStepExecution($stepExecution); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * {@inheritdoc} |
||
96 | */ |
||
97 | public function restoreStepExecution() |
||
98 | { |
||
99 | if ($this->writer instanceof StepExecutionRestoreInterface) { |
||
100 | $this->writer->restoreStepExecution(); |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @param array $uniqueItems |
||
106 | * @param object $item |
||
107 | * @param string|null $identifier |
||
108 | */ |
||
109 | protected function handleIdentifier(array &$uniqueItems, $item, $identifier = null) |
||
121 | |||
122 | /** |
||
123 | * @param int|string $identifier |
||
124 | */ |
||
125 | protected function logSkipped($identifier) |
||
131 | } |
||
132 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: