DataProvider::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Model\UiComponent;
28
29
use Magento\Framework\Api\FilterBuilder;
30
use Magento\Framework\Api\Search\SearchCriteriaBuilder;
31
use Magento\Framework\App\RequestInterface;
32
use Magento\Framework\View\Element\UiComponent\DataProvider\Reporting;
33
34
/**
35
 * DataProvider extension model
36
 */
37
class DataProvider extends \Magento\Framework\View\Element\UiComponent\DataProvider\DataProvider
38
{
39
    /**
40
     * PAYONE database helper
41
     *
42
     * @var \Payone\Core\Helper\Database
43
     */
44
    protected $databaseHelper;
45
46
    /**
47
     * Constructor
48
     *
49
     * @param string                       $name
50
     * @param string                       $primaryFieldName
51
     * @param string                       $requestFieldName
52
     * @param Reporting                    $reporting
53
     * @param SearchCriteriaBuilder        $searchCritBuilder
54
     * @param RequestInterface             $request
55
     * @param FilterBuilder                $filterBuilder
56
     * @param \Payone\Core\Helper\Database $databaseHelper
57
     * @param array                        $meta
58
     * @param array                        $data
59
     */
60
    public function __construct(
61
        $name,
62
        $primaryFieldName,
63
        $requestFieldName,
64
        Reporting $reporting,
65
        SearchCriteriaBuilder $searchCritBuilder,
66
        RequestInterface $request,
67
        FilterBuilder $filterBuilder,
68
        \Payone\Core\Helper\Database $databaseHelper,
69
        array $meta = [],
70
        array $data = []
71
    ) {
72
        $this->databaseHelper = $databaseHelper; // needs to be in front of constructor, doesnt work otherwise for no apparent reason
73
        parent::__construct($name, $primaryFieldName, $requestFieldName, $reporting, $searchCritBuilder, $request, $filterBuilder, $meta, $data);
74
    }
75
76
    /**
77
     * This is used to fix the admin grid filter
78
     * The admin-user can filter by increment_id
79
     * But the ajax-implementation used for the filter somehow sometimes
80
     * sends the order-id instead of the increment_id, so this
81
     * translates the order id to the increment_id
82
     *
83
     * @return void
84
     */
85
    protected function prepareUpdateUrl()
86
    {
87
        if (!isset($this->data['config']['filter_url_params'])) {
88
            return;
89
        }
90
        foreach ($this->data['config']['filter_url_params'] as $paramName => $paramValue) {
91
            if ('*' == $paramValue) {
92
                $paramValue = $this->request->getParam($paramName);
93
            }
94
            if (substr($paramValue, 0, 1) == '0') { // already an increment_id
95
                parent::prepareUpdateUrl();
96
                return;
97
            }
98
            if ($paramValue) {
99
                $sIncrementId = $this->databaseHelper->getIncrementIdByOrderId($paramValue);
100
                if ($sIncrementId) {
101
                    $paramValue = $sIncrementId;
102
                }
103
                $this->data['config']['update_url'] = sprintf(
104
                    '%s%s/%s',
105
                    $this->data['config']['update_url'],
106
                    $paramName,
107
                    $paramValue
108
                );
109
                $this->addFilter(
110
                    $this->filterBuilder->setField($paramName)->setValue($paramValue)->setConditionType('eq')->create()
111
                );
112
            }
113
        }
114
    }
115
}
116