Passed
Pull Request — master (#396)
by
unknown
02:33
created

Upload::beforeSave()   B

Complexity

Conditions 10
Paths 18

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 20
nc 18
nop 0
dl 0
loc 33
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Payone\Core\Model\System\Config\Backend;
4
5
use Magento\Framework\Filesystem;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Filesystem was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Magento\Framework\Filesystem\DirectoryList;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Filesystem\DirectoryList was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class Upload extends \Magento\Framework\App\Config\Value
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Config\Value was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
{
10
    /**
11
     * @var \Magento\Framework\Filesystem\Directory\ReadInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Filesy...Directory\ReadInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
     */
13
    protected $_tmpDirectory;
14
15
    /**
16
     * @var \Payone\Core\Helper\ApplePay
17
     */
18
    protected $applePayHelper;
19
20
    /**
21
     * @param \Magento\Framework\Model\Context $context
22
     * @param \Magento\Framework\Registry $registry
23
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
24
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
25
     * @param \Magento\Framework\Filesystem $filesystem
26
     * @param \Payone\Core\Helper\ApplePay $applePayHelper
27
     * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
28
     * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
29
     * @param array $data
30
     */
31
    public function __construct(
32
        \Magento\Framework\Model\Context $context,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
        \Magento\Framework\Registry $registry,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Registry was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
        \Magento\Framework\App\Config\ScopeConfigInterface $config,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Config\ScopeConfigInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
        \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Cache\TypeListInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
36
        \Magento\Framework\Filesystem $filesystem,
37
        \Payone\Core\Helper\ApplePay $applePayHelper,
38
        \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Model\...eModel\AbstractResource was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
        \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Data\Collection\AbstractDb was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
        array $data = []
41
    )
42
    {
43
        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
44
        $this->_tmpDirectory = $filesystem->getDirectoryRead(DirectoryList::SYS_TMP);
45
        $this->applePayHelper = $applePayHelper;
46
    }
47
48
    /**
49
     * Process additional data before save config
50
     *
51
     * @return $this
52
     * @throws \Magento\Framework\Exception\LocalizedException
53
     */
54
    public function beforeSave()
55
    {
56
        $value = $this->getValue();
57
58
        if (!empty($value['value'])) {
59
            $this->setValue($value['value']);
60
        } elseif (!isset($value['value'])) {
61
            $this->setValue('');
62
        }
63
64
        if (is_array($value) && !empty($value['delete'])) {
65
            $sCurrentFile = $this->applePayHelper->getApplePayUploadPath().$value['value'];
66
            if (file_exists($sCurrentFile)) {
67
                unlink($sCurrentFile);
68
            }
69
            $this->setValue('');
70
            return $this;
71
        }
72
73
        if (empty($value['tmp_name'])) {
74
            return $this;
75
        }
76
77
        $tmpPath = $this->_tmpDirectory->getRelativePath($value['tmp_name']);
78
        if ($tmpPath && $this->_tmpDirectory->isExist($tmpPath)) {
79
            if (!$this->_tmpDirectory->stat($tmpPath)['size']) {
80
                throw new \Magento\Framework\Exception\LocalizedException(__('The certificate file is empty.'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

80
                throw new \Magento\Framework\Exception\LocalizedException(/** @scrutinizer ignore-call */ __('The certificate file is empty.'));
Loading history...
Bug introduced by
The type Magento\Framework\Exception\LocalizedException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
81
            }
82
            $this->setValue($value['name']);
83
84
            $this->moveFile($value['tmp_name'], $value['name']);
85
        }
86
        return $this;
87
    }
88
89
    /**
90
     * Copies file to upload path
91
     *
92
     * @param  string $sFilePath
93
     * @param  string $sFileName
94
     * @return void
95
     */
96
    protected function moveFile($sFilePath, $sFileName)
97
    {
98
        $sUploadPath = $this->applePayHelper->getApplePayUploadPath();
99
        if (!file_exists($sUploadPath)) {
100
            mkdir($sUploadPath);
101
        }
102
        move_uploaded_file($sFilePath, $sUploadPath.$sFileName);
103
    }
104
}
105