Completed
Pull Request — master (#144)
by
unknown
05:00
created

SerializedOrJson::beforeSave()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 0
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\Config\Backend;
28
29
use Magento\Framework\App\Config\Value;
30
31
class SerializedOrJson extends Value
32
{
33
    /**
34
     * PAYONE Base helper
35
     *
36
     * @var \Payone\Core\Helper\Base
37
     */
38
    protected $baseHelper;
39
40
    /**
41
     * @param \Magento\Framework\Model\Context                             $context
42
     * @param \Magento\Framework\Registry                                  $registry
43
     * @param \Magento\Framework\App\Config\ScopeConfigInterface           $config
44
     * @param \Magento\Framework\App\Cache\TypeListInterface               $cacheTypeList
45
     * @param \Payone\Core\Helper\Base                                     $baseHelper
46
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
47
     * @param \Magento\Framework\Data\Collection\AbstractDb|null           $resourceCollection
48
     * @param array                                                        $data
49
     */
50 View Code Duplication
    public 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...
51
        \Magento\Framework\Model\Context $context,
52
        \Magento\Framework\Registry $registry,
53
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
54
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
55
        \Payone\Core\Helper\Base $baseHelper,
56
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
57
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
58
        array $data = []
59
    ) {
60
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
61
        $this->baseHelper = $baseHelper;
62
    }
63
64
    /**
65
     * @return void
66
     */
67
    protected function _afterLoad()
68
    {
69
        if (!is_array($this->getValue())) {
70
            $value = $this->getValue();
71
            $this->setValue(empty($value) ? false : $this->baseHelper->unserialize($value));
72
        }
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function beforeSave()
79
    {
80
        $value = $this->getValue();
81
        if (is_array($value)) {
82
            unset($value['__empty']);
83
            $value = $this->baseHelper->serialize($value);
84
        }
85
        $this->setValue($value);
86
        return parent::beforeSave();
87
    }
88
}