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

NewsletterSubscriberBridgeIterator::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace OroCRM\Bundle\MagentoBundle\Provider\Iterator;
4
5
use Oro\Bundle\IntegrationBundle\Utils\ConverterUtils;
6
use OroCRM\Bundle\MagentoBundle\Provider\BatchFilterBag;
7
use OroCRM\Bundle\MagentoBundle\Provider\Transport\ServerTimeAwareInterface;
8
use OroCRM\Bundle\MagentoBundle\Provider\Transport\SoapTransport;
9
10
class NewsletterSubscriberBridgeIterator extends AbstractBridgeIterator
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $initialId;
16
17
    /**
18
     * @param int $initialId
19
     * @return NewsletterSubscriberBridgeIterator
20
     */
21
    public function setInitialId($initialId)
22
    {
23
        $this->initialId = $initialId;
24
25
        return $this;
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    protected function applyFilter()
32
    {
33
        if ($this->isInitialSync()) {
34
            $initialId = $this->getInitialId();
35
            if ($initialId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $initialId of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
36
                $this->filter->addComplexFilter(
37
                    $this->getIdFieldName(),
38
                    [
39
                        'key' => $this->getIdFieldName(),
40
                        'value' => [
41
                            'key' => 'lt',
42
                            'value' => $initialId
43
                        ]
44
                    ]
45
                );
46
            }
47
        } else {
48
            $dateField = 'change_status_at';
49
            $this->filter->addDateFilter($dateField, 'gt', $this->lastSyncDate);
50
            $fixTime = $this->fixServerTime($dateField);
51
52
            if ($fixTime) {
53
                $this->setStartDate($fixTime);
54
            }
55
        }
56
57
        $this->applyStoreFilter($this->filter);
58
        if (null !== $this->predefinedFilters) {
59
            $this->filter->merge($this->predefinedFilters);
60
        }
61
62
        $this->logAppliedFilters($this->filter);
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected function getEntityIds()
69
    {
70
        $this->applyFilter();
71
72
        $filters = $this->filter->getAppliedFilters();
73
        $filters['pager'] = ['page' => $this->getCurrentPage(), 'pageSize' => $this->pageSize];
74
75
        $result = $this->getNewsletterSubscribers($filters);
76
        $result = $this->processCollectionResponse($result);
77
        $result = $this->convertResponseToMultiArray($result);
78
        $resultIds = [];
79
80
        if (is_array($result) && count($result) > 0) {
81
            $resultIds = array_map(
82
                function ($item) {
83
                    return $item[$this->getIdFieldName()];
84
                },
85
                $result
86
            );
87
88
            $this->entityBuffer = array_combine($resultIds, $result);
89
        }
90
91
        return $resultIds;
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function getIdFieldName()
98
    {
99
        return 'subscriber_id';
100
    }
101
102
    /**
103
     * @return int|null
104
     */
105
    protected function getInitialId()
106
    {
107
        if ($this->isInitialSync() && !$this->initialId) {
108
            $filter = new BatchFilterBag();
109
            $this->applyStoreFilter($filter);
110
            $filters = $filter->getAppliedFilters();
111
            $filters['pager'] = ['page' => 1, 'pageSize' => 1];
112
            $subscribers = $this->getNewsletterSubscribers($filters);
113
            $subscribers = $this->convertResponseToMultiArray($subscribers);
0 ignored issues
show
Bug introduced by
It seems like $subscribers can also be of type null; however, OroCRM\Bundle\MagentoBun...tResponseToMultiArray() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
114
115
            $subscriber = [];
116
            if (array_key_exists(0, $subscribers)) {
117
                $subscriber = $subscribers[0];
118
            }
119
120
            if (array_key_exists($this->getIdFieldName(), $subscriber)) {
121
                $this->initialId = (int)$subscriber[$this->getIdFieldName()] + 1;
122
            }
123
        }
124
125
        return $this->initialId;
126
    }
127
128
    /**
129
     * @param BatchFilterBag $filter
130
     */
131
    protected function applyStoreFilter(BatchFilterBag $filter)
132
    {
133
        if ($this->websiteId && $this->websiteId !== StoresSoapIterator::ALL_WEBSITES) {
134
            $filter->addStoreFilter($this->getStoresByWebsiteId($this->websiteId));
135
        }
136
    }
137
138
    /**
139
     * @param array $filters
140
     * @return array|null
141
     */
142
    protected function getNewsletterSubscribers(array $filters = [])
143
    {
144
        $result = $this->transport->call(SoapTransport::ACTION_ORO_NEWSLETTER_SUBSCRIBER_LIST, $filters);
145
146
        return ConverterUtils::objectToArray($result);
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152
    public function current()
153
    {
154
        $this->logger->info(sprintf('Loading NewsletterSubscriber by id: %s', $this->key()));
155
156
        return $this->current;
157
    }
158
159
    /**
160
     * Fix time frame if it's possible to retrieve server time.
161
     *
162
     * @param string $dateField
163
     * @return bool | \DateTime
164
     */
165
    protected function fixServerTime($dateField)
166
    {
167
        if (!$this->isInitialSync() && $this->transport instanceof ServerTimeAwareInterface) {
168
            $time = $this->transport->getServerTime();
169
            if (false !== $time) {
170
                $frameLimit = new \DateTime($time, new \DateTimeZone('UTC'));
171
                $this->filter->addDateFilter($dateField, 'lte', $frameLimit);
172
173
                return $frameLimit;
174
            }
175
        }
176
177
        return false;
178
    }
179
}
180