Completed
Push — master ( 31c29f...47345c )
by
unknown
10:18
created

ProxyEntityWriter::restoreStepExecution()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4286
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\ImportExportBundle\Field\DatabaseHelper;
14
15
use OroCRM\Bundle\MagentoBundle\Entity\Cart;
16
use OroCRM\Bundle\MagentoBundle\Entity\Order;
17
use OroCRM\Bundle\MagentoBundle\Entity\Customer;
18
19
class ProxyEntityWriter implements ItemWriterInterface, StepExecutionAwareInterface, LoggerAwareInterface
20
{
21
    use LoggerAwareTrait;
22
23
    /** @var ItemWriterInterface */
24
    protected $writer;
25
26
    /** @var DatabaseHelper */
27
    protected $databaseHelper;
28
29
    /**
30
     * @param ItemWriterInterface $writer
31
     * @param DatabaseHelper $databaseHelper
32
     */
33
    public function __construct(ItemWriterInterface $writer, DatabaseHelper $databaseHelper)
34
    {
35
        $this->writer = $writer;
36
        $this->databaseHelper = $databaseHelper;
37
        $this->logger = new NullLogger();
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     *
43
     * Prepare items for PersistentBatchWriter, filters for duplicates and takes only latest versions
44
     */
45
    public function write(array $items)
46
    {
47
        $uniqueItems = [];
48
        $uniqueKeys = [];
49
        foreach ($items as $item) {
50
            if ($item instanceof Customer || $item instanceof Cart) {
51
                $identifier = $item->getOriginId();
52
                if (array_key_exists($identifier, $uniqueItems)) {
53
                    $this->logSkipped($identifier);
54
                }
55
56
                $uniqueItems[$identifier] = $item;
57
            } elseif ($item instanceof Order) {
58
                $identifier = $item->getIncrementId();
59
                if (array_key_exists($identifier, $uniqueItems)) {
60
                    $this->logSkipped($item->getIncrementId());
61
                }
62
63
                $uniqueItems[$identifier] = $item;
64
            } elseif ($item instanceof NewsletterSubscriber) {
0 ignored issues
show
Bug introduced by
The class OroCRM\Bundle\MagentoBun...er\NewsletterSubscriber 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...
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
     * @param int|string $identifier
96
     */
97
    protected function logSkipped($identifier)
98
    {
99
        $this->logger->info(
100
            sprintf('[origin_id=%s] Item skipped because of newer version found', (string)$identifier)
101
        );
102
    }
103
}
104