Completed
Push — master ( 0bae06...931bd3 )
by
unknown
78:08
created

ProxyEntityWriter::logSkipped()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4286
cc 1
eloc 3
nc 1
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) {
0 ignored issues
show
Bug introduced by
The class Oro\Bundle\BatchBundle\S...ecutionRestoreInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
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