Completed
Push — master ( 476fde...6aa0c2 )
by Florian
03:19
created

StatusMapping   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 148
Duplicated Lines 5.41 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 8
loc 148
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A _construct() 8 8 1
B renderCellTemplate() 0 22 5
C getArrayRows() 0 29 7
A _getElementHtml() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\Block\Adminhtml\Config\Form\Field;
28
29
/**
30
 * Block class for status-mapping grid-element
31
 */
32
class StatusMapping extends \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
33
{
34
    /**
35
     * Element factory
36
     *
37
     * @var \Magento\Framework\Data\Form\Element\Factory
38
     */
39
    protected $elementFactory;
40
41
    /**
42
     * List of all possible OrderStatus types
43
     *
44
     * @var \Magento\Sales\Model\Config\Source\Order\Status
45
     */
46
    protected $orderStatus;
47
48
    /**
49
     * List of all possible TransactionStatus types
50
     *
51
     * @var \Payone\Core\Model\Source\TransactionStatus
52
     */
53
    protected $transactionStatus;
54
    
55
    /**
56
     * Rows cache
57
     *
58
     * @var array|null
59
     */
60
    private $_arrayRowsCache;
61
62
    /**
63
     * Constructor
64
     *
65
     * @param  \Magento\Backend\Block\Template\Context         $context
66
     * @param  \Magento\Framework\Data\Form\Element\Factory    $elementFactory
67
     * @param  \Magento\Sales\Model\Config\Source\Order\Status $orderStatus
68
     * @param  \Payone\Core\Model\Source\TransactionStatus     $transactionStatus
69
     * @param  array                                           $data
70
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
71
     */
72
    public function __construct(
73
        \Magento\Backend\Block\Template\Context $context,
74
        \Magento\Framework\Data\Form\Element\Factory $elementFactory,
75
        \Magento\Sales\Model\ResourceModel\Order\Status\Collection $orderStatus,
76
        \Payone\Core\Model\Source\TransactionStatus $transactionStatus,
77
        array $data = []
78
    ) {
79
        parent::__construct($context, $data);
80
        $this->elementFactory = $elementFactory;
81
        $this->orderStatus = $orderStatus;
0 ignored issues
show
Documentation Bug introduced by
It seems like $orderStatus of type object<Magento\Sales\Mod...rder\Status\Collection> is incompatible with the declared type object<Magento\Sales\Mod...ig\Source\Order\Status> of property $orderStatus.

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...
82
        $this->transactionStatus = $transactionStatus;
83
    }
84
85
    /**
86
     * Initialise form fields
87
     *
88
     * @return void
89
     */
90 View Code Duplication
    protected function _construct()
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...
91
    {
92
        $this->addColumn('txaction', ['label' => __('Transactionstatus-message')]);
93
        $this->addColumn('state_status', ['label' => __('Magento-status')]);
94
        $this->addAfter = false;
95
        $this->addButtonLabel = __('Add Statusmapping');
96
        parent::_construct();
97
    }
98
99
    /**
100
     * Render array cell for prototypeJS template
101
     *
102
     * @param  string $columnName
103
     * @return string
104
     */
105
    public function renderCellTemplate($columnName)
106
    {
107
        if ($columnName == 'txaction' && isset($this->_columns[$columnName])) {
108
            $aOptions = $this->transactionStatus->toOptionArray();
109
        } elseif ($columnName == 'state_status' && isset($this->_columns[$columnName])) {
110
            $aOptions = $this->orderStatus->toOptionArray();
111
        } else {
112
            return parent::renderCellTemplate($columnName);
113
        }
114
115
        $oElement = $this->elementFactory->create('select');
116
        $oElement->setForm(
117
            $this->getForm()
118
        )->setName(
119
            $this->_getCellInputElementName($columnName)
120
        )->setHtmlId(
121
            $this->_getCellInputElementId('<%- _id %>', $columnName)
122
        )->setValues(
123
            $aOptions
124
        );
125
        return str_replace("\n", '', $oElement->getElementHtml());
126
    }
127
    
128
    /**
129
     * Obtain existing data from form element
130
     *
131
     * Each row will be instance of \Magento\Framework\DataObject
132
     *
133
     * @return array
134
     */
135
    public function getArrayRows()
136
    {
137
        if (null !== $this->_arrayRowsCache) {
138
            return $this->_arrayRowsCache;
139
        }
140
        $result = [];
141
        /** @var \Magento\Framework\Data\Form\Element\AbstractElement */
142
        $element = $this->getElement();
143
        $aValue = $element->getValue();
144
        if (!is_array($aValue)) {
145
            $aValue = unserialize($aValue);
146
        }
147
        
148
        if ($aValue && is_array($aValue)) {
149
            foreach ($aValue as $rowId => $row) {
150
                $rowColumnValues = [];
151
                foreach ($row as $key => $value) {
152
                    $row[$key] = $value;
153
                    $rowColumnValues[$this->_getCellInputElementId($rowId, $key)] = $row[$key];
154
                }
155
                $row['_id'] = $rowId;
156
                $row['column_values'] = $rowColumnValues;
157
                $result[$rowId] = new \Magento\Framework\DataObject($row);
158
                $this->_prepareArrayRow($result[$rowId]);
159
            }
160
        }
161
        $this->_arrayRowsCache = $result;
162
        return $this->_arrayRowsCache;
163
    }
164
165
    /**
166
     * Get the grid and scripts contents
167
     *
168
     * @param \Magento\Framework\Data\Form\Element\AbstractElement $element
169
     * @return string
170
     */
171
    protected function _getElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
172
    {
173
        $this->setElement($element);
174
        $html = $this->_toHtml();
175
        $this->_arrayRowsCache = null;
176
        // doh, the object is used as singleton!
177
        return $html;
178
    }
179
}
180