Completed
Push — 1.10 ( 3bcec0...1a0641 )
by
unknown
08:47
created

AbstractBridgeIterator::fixServerTime()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 3
nop 1
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Provider\Iterator;
4
5
use Oro\Bundle\IntegrationBundle\Utils\ConverterUtils;
6
7
use OroCRM\Bundle\MagentoBundle\Provider\BatchFilterBag;
8
use OroCRM\Bundle\MagentoBundle\Provider\Transport\ServerTimeAwareInterface;
9
use OroCRM\Bundle\MagentoBundle\Provider\Transport\SoapTransport;
10
11
abstract class AbstractBridgeIterator extends AbstractPageableSoapIterator implements PredefinedFiltersAwareInterface
12
{
13
    const DEFAULT_PAGE_SIZE = 100;
14
15
    /** @var int */
16
    protected $currentPage = 1;
17
18
    /** @var bool */
19
    protected $lastPageAssumed = false;
20
21
    /** @var BatchFilterBag */
22
    protected $predefinedFilters;
23
24
    /** @var int */
25
    protected $pageSize;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function __construct(SoapTransport $transport, array $settings)
31
    {
32
        parent::__construct($transport, $settings);
33
34
        $this->pageSize = !empty($settings['page_size']) ? (int)$settings['page_size'] : self::DEFAULT_PAGE_SIZE;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function setPredefinedFiltersBag(BatchFilterBag $bag)
41
    {
42
        $this->predefinedFilters = $bag;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected function applyFilter()
49
    {
50
        if ($this->isInitialSync()) {
51
            $dateField = 'created_at';
52
            $this->filter->addDateFilter($dateField, 'from', $this->getToDateInitial($this->lastSyncDate));
53
            $this->filter->addDateFilter($dateField, 'to', $this->lastSyncDate);
54
        } else {
55
            $dateField = 'updated_at';
56
            $this->filter->addDateFilter($dateField, 'from', $this->lastSyncDate);
57
            $this->filter->addDateFilter($dateField, 'to', $this->getToDate($this->lastSyncDate));
58
        }
59
60
        if (null !== $this->predefinedFilters) {
61
            $this->filter->merge($this->predefinedFilters);
62
        }
63
64
        $this->filter->resetFilterWithEmptyValue();
65
66
        $this->logAppliedFilters($this->filter);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    protected function findEntitiesToProcess()
73
    {
74
        if ($this->lastPageAssumed) {
75
            return null;
76
        }
77
78
        $this->logger->info('Looking for batch');
79
        $this->entitiesIdsBuffer = $this->getEntityIds();
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getEntityIds() of type * is incompatible with the declared type array of property $entitiesIdsBuffer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
        $this->currentPage++;
81
82
        $this->logger->info(sprintf('found %d entities', count($this->entitiesIdsBuffer)));
83
84
        // if previous result batch items count less then requested page size
85
        // then assume that it's last page
86
        if (count($this->entityBuffer) < $this->pageSize) {
87
            $this->lastPageAssumed = true;
88
        }
89
90
        return empty($this->entitiesIdsBuffer) ? null : true;
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    protected function getCurrentPage()
97
    {
98
        return $this->currentPage;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 View Code Duplication
    protected function getEntity($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (!array_key_exists($id, $this->entityBuffer)) {
107
            $this->logger->warning(sprintf('Entity with id "%s" was not found', $id));
108
109
            return false;
110
        }
111
112
        $result = $this->entityBuffer[$id];
113
114
        return ConverterUtils::objectToArray($result);
115
    }
116
}
117