Collection::toOptionHashOptimized()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
/**
10
 * Sub Seller collection.
11
 */
12
13
namespace Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller;
14
15
use Getnet\SubSellerMagento\Model\ResourceModel\Seller\SubSeller as SubSellerResourceModel;
16
use Getnet\SubSellerMagento\Model\Seller\SubSeller;
17
use Magento\Framework\Data\Collection\Db\FetchStrategyInterface;
18
use Magento\Framework\Data\Collection\EntityFactory;
19
use Magento\Framework\DB\Adapter\AdapterInterface;
20
use Magento\Framework\Event\ManagerInterface;
21
use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
22
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
23
use Magento\Store\Model\StoreManagerInterface;
24
use Psr\Log\LoggerInterface;
25
26
/**
27
 * Collection of SubSeller.
28
 */
29
class Collection extends AbstractCollection
30
{
31
    /**
32
     * Value of fetched from DB of rules per cycle.
33
     */
34
    public const SUB_SELLER_CHUNK_SIZE = 1000;
35
36
    /**
37
     * @var StoreManagerInterface
38
     */
39
    protected $_storeManager;
40
41
    /**
42
     * @param EntityFactory          $entityFactory
43
     * @param LoggerInterface        $logger
44
     * @param FetchStrategyInterface $fetchStrategy
45
     * @param ManagerInterface       $eventManager
46
     * @param StoreManagerInterface  $storeManager
47
     * @param mixed                  $connection
48
     * @param AbstractDb             $resource
49
     */
50
    public function __construct(
51
        EntityFactory $entityFactory,
52
        LoggerInterface $logger,
53
        FetchStrategyInterface $fetchStrategy,
54
        ManagerInterface $eventManager,
55
        StoreManagerInterface $storeManager,
56
        AdapterInterface $connection = null,
57
        AbstractDb $resource = null
58
    ) {
59
        $this->_storeManager = $storeManager;
60
        parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
61
    }
62
63
    /**
64
     * Resource initialization.
65
     *
66
     * @return void
67
     */
68
    protected function _construct()
69
    {
70
        $this->_init(SubSeller::class, SubSellerResourceModel::class);
71
    }
72
73
    /**
74
     * Add subSeller filter.
75
     *
76
     * @param int $subSellerId
77
     *
78
     * @return $this
79
     */
80
    public function addSubSellerFilter($subSellerId)
81
    {
82
        if (is_int($subSellerId) && $subSellerId > 0) {
83
            return $this->addFieldToFilter('main_table.getnet_sub_seller_id', $subSellerId);
84
        }
85
86
        return $this;
87
    }
88
89
    /**
90
     * Retrieve option array.
91
     *
92
     * @return array
93
     */
94
    public function toOptionArray()
95
    {
96
        return $this->_toOptionArray('getnet_sub_seller_id', 'code');
97
    }
98
99
    /**
100
     * Retrieve option hash.
101
     *
102
     * @return array
103
     */
104
    public function toOptionHash()
105
    {
106
        return $this->_toOptionHash('getnet_sub_seller_id', 'code');
107
    }
108
109
    /**
110
     * Convert items array to hash for select options using fetchItem method.
111
     *
112
     * @return array
113
     *
114
     * @see fetchItem()
115
     */
116
    public function toOptionHashOptimized()
117
    {
118
        $result = [];
119
        while ($item = $this->fetchItem()) {
120
            $result[$item->getData('getnet_sub_seller_id')] = $item->getData('code');
121
        }
122
123
        return $result;
124
    }
125
126
    /**
127
     * Get subSeller array without memory leak.
128
     *
129
     * @return array
130
     */
131
    public function getOptionSubSeller()
132
    {
133
        $size = self::SUB_SELLER_CHUNK_SIZE;
134
        $page = 1;
135
        $subSeller = [];
136
        do {
137
            $offset = $size * ($page - 1);
138
            $this->getSelect()->reset();
139
            $this->getSelect()
140
                ->from(
141
                    ['subSeller' => $this->getMainTable()],
0 ignored issues
show
Unused Code introduced by
The call to Magento\Framework\DB\Select::from() has too many arguments starting with array('subSeller' => $this->getMainTable()). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
                ->/** @scrutinizer ignore-call */ from(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
142
                    ['getnet_sub_seller_id', 'code']
143
                )
144
                ->limit($size, $offset);
145
146
            $subSeller[] = $this->toOptionArray();
147
            $this->clear();
148
            $page++;
149
        } while ($this->getSize() > $offset);
150
151
        return array_merge([], ...$subSeller);
152
    }
153
}
154