Completed
Push — master ( c2375b...0b658a )
by
unknown
09:35
created

ProxyEntityWriter::restoreStepExecution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
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) {
61
                //GuestCustomerStrategy checks both email and channel
62
                if ($item->isGuest()) {
63
                    $channel = $item->getChannel();
64
                    $identifier = preg_replace('/[^a-zA-Z0-9\.]/', '', $item->getEmail());
65
                    //set unique identifier: email and channel id
66
                    if ($channel) {
67
                        $identifier.=$channel->getId();
68
                    }
69
                } else {
70
                    $identifier = $item->getOriginId();
71
                }
72
                $this->handleIdentifier($uniqueItems, $item, $identifier);
73
            } elseif ($item instanceof Cart) {
74
                $this->handleIdentifier($uniqueItems, $item, $item->getOriginId());
75
            } elseif ($item instanceof Order) {
76
                $this->handleIdentifier($uniqueItems, $item, $item->getIncrementId());
77
            } elseif ($item instanceof NewsletterSubscriber) {
78
                $identifier = $item->getCustomer() ? $item->getCustomer()->getId() : 0;
79
                if ($identifier !== 0 && in_array($identifier, $uniqueKeys)) {
80
                    $this->logSkipped($item->getOriginId());
81
                } else {
82
                    $uniqueKeys[] = $identifier;
83
                    $uniqueItems[] = $item;
84
                }
85
86
            } else {
87
                $uniqueItems[] = $item;
88
            }
89
        }
90
91
        $this->writer->write($uniqueItems);
92
93
        // force entity cache clear if clear is skipped
94
        $this->databaseHelper->onClear();
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function setStepExecution(StepExecution $stepExecution)
101
    {
102
        if ($this->writer instanceof StepExecutionAwareInterface) {
103
            $this->writer->setStepExecution($stepExecution);
104
        }
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function restoreStepExecution()
111
    {
112
        if ($this->writer instanceof StepExecutionRestoreInterface) {
113
            $this->writer->restoreStepExecution();
114
        }
115
    }
116
117
    /**
118
     * @param array $uniqueItems
119
     * @param object $item
120
     * @param string|null $identifier
121
     */
122
    protected function handleIdentifier(array &$uniqueItems, $item, $identifier = null)
123
    {
124
        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...
125
            $this->logSkipped($identifier);
126
        }
127
128
        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...
129
            $uniqueItems[$identifier] = $item;
130
        } else {
131
            $uniqueItems[spl_object_hash($item)] = $item;
132
        }
133
    }
134
135
    /**
136
     * @param int|string $identifier
137
     */
138
    protected function logSkipped($identifier)
139
    {
140
        $this->logger->info(
141
            sprintf('[origin_id=%s] Item skipped because of newer version found', (string)$identifier)
142
        );
143
    }
144
}
145