Collection   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 109
c 0
b 0
f 0
wmc 16
lcom 0
cbo 0
ccs 0
cts 66
cp 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A _construct() 0 4 1
A _afterLoad() 0 5 1
A loadStoresData() 0 20 5
A addFieldToFilter() 0 8 2
A addStoreFilter() 0 17 4
A _renderFiltersBefore() 0 7 2
A joinStoreRelationTable() 0 10 1
1
<?php
2
/**
3
 * File: Collection.php
4
 *
5
 * @author      Maciej Sławik <[email protected]>
6
 * Github:      https://github.com/maciejslawik
7
 */
8
9
namespace MSlwk\ICatalogue\Model\ResourceModel\Catalogue;
10
11
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection;
12
use Magento\Store\Model\Store;
13
use MSlwk\ICatalogue\Model\ResourceModel\Catalogue as CatalogueResource;
14
use MSlwk\ICatalogue\Api\CatalogueInterface;
15
use MSlwk\ICatalogue\Api\Catalogue\StoreInterface;
16
17
/**
18
 * Class Collection
19
 *
20
 * @package MSlwk\ICatalogue\Model\ResourceModel\Catalogue
21
 */
22
class Collection extends AbstractCollection
23
{
24
    /**
25
     * @inheritdoc
26
     */
27
    protected function _construct()
28
    {
29
        $this->_init('MSlwk\ICatalogue\Model\Catalogue', 'MSlwk\ICatalogue\Model\ResourceModel\Catalogue');
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    protected function _afterLoad()
36
    {
37
        $this->loadStoresData(CatalogueResource::CATALOGUE_STORE_TABLE, CatalogueInterface::CATALOGUE_ID);
38
        return parent::_afterLoad();
39
    }
40
41
    /**
42
     * @param $tableName
43
     * @param $linkField
44
     * @return void
45
     */
46
    protected function loadStoresData($tableName, $linkField)
47
    {
48
        $connection = $this->getConnection();
49
        $select = $connection->select()->from($tableName);
50
        $result = $connection->fetchAll($select);
51
        if ($result) {
52
            $storesData = [];
53
            foreach ($result as $storeData) {
54
                $storesData[$storeData[$linkField]][] = $storeData[StoreInterface::STORE_ID];
55
            }
56
57
            foreach ($this as $item) {
58
                $linkedId = $item->getData($linkField);
59
                if (!isset($storesData[$linkedId])) {
60
                    continue;
61
                }
62
                $item->setStores($storesData[$linkedId]);
63
            }
64
        }
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function addFieldToFilter($field, $condition = null)
71
    {
72
        if ($field === 'stores') {
73
            return $this->addStoreFilter($condition, false);
74
        }
75
76
        return parent::addFieldToFilter($field, $condition);
77
    }
78
79
    /**
80
     * Add filter by store
81
     *
82
     * @param int|array|Store $store
83
     * @param bool $withAdmin
84
     * @return $this
85
     */
86
    public function addStoreFilter($store, $withAdmin = true)
87
    {
88
        if ($store instanceof Store) {
0 ignored issues
show
Bug introduced by
The class Magento\Store\Model\Store 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...
89
            $store = [$store->getId()];
90
        }
91
92
        if (!is_array($store)) {
93
            $store = [$store];
94
        }
95
96
        if ($withAdmin) {
97
            $store[] = Store::DEFAULT_STORE_ID;
98
        }
99
100
        $this->addFilter(StoreInterface::STORE_ID, ['in' => $store]);
101
        return $this;
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107
    protected function _renderFiltersBefore()
108
    {
109
        if ($this->getFilter(StoreInterface::STORE_ID)) {
110
            $this->joinStoreRelationTable();
111
        }
112
        return parent::_renderFiltersBefore();
113
    }
114
115
    /**
116
     * Join store relation table
117
     *
118
     * @return void
119
     */
120
    protected function joinStoreRelationTable()
121
    {
122
        $this->getSelect()->join(
123
            ['store_table' => CatalogueResource::CATALOGUE_STORE_TABLE],
124
            'main_table.' . CatalogueInterface::CATALOGUE_ID . ' = store_table.' . CatalogueInterface::CATALOGUE_ID,
125
            []
126
        )->group(
127
            'main_table.' . CatalogueInterface::CATALOGUE_ID
128
        );
129
    }
130
}
131