Completed
Push — 1.9 ( d8eb28...5c3e2e )
by
unknown
61:52 queued 29s
created

ProxyEntityWriter::write()   D

Complexity

Conditions 9
Paths 8

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 4.909
cc 9
eloc 19
nc 8
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\ImportExport\Writer;
4
5
use Psr\Log\NullLogger;
6
use Psr\Log\LoggerAwareTrait;
7
use Psr\Log\LoggerAwareInterface;
8
9
use Akeneo\Bundle\BatchBundle\Entity\StepExecution;
10
use Akeneo\Bundle\BatchBundle\Item\ItemWriterInterface;
11
use Akeneo\Bundle\BatchBundle\Step\StepExecutionAwareInterface;
12
13
use Oro\Bundle\BatchBundle\Step\StepExecutionRestoreInterface;
14
15
use Oro\Bundle\ImportExportBundle\Field\DatabaseHelper;
16
17
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
18
use OroCRM\Bundle\MagentoBundle\Entity\Order;
19
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
20
use OroCRM\Bundle\MagentoBundle\Entity\NewsletterSubscriber;
21
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)
110
    {
111
        if ($identifier && array_key_exists($identifier, $uniqueItems)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
112
            $this->logSkipped($identifier);
113
        }
114
115
        if ($identifier) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $identifier of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch 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:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
116
            $uniqueItems[$identifier] = $item;
117
        } else {
118
            $uniqueItems[spl_object_hash($item)] = $item;
119
        }
120
    }
121
122
    /**
123
     * @param int|string $identifier
124
     */
125
    protected function logSkipped($identifier)
126
    {
127
        $this->logger->info(
128
            sprintf('[origin_id=%s] Item skipped because of newer version found', (string)$identifier)
129
        );
130
    }
131
}
132