Passed
Push — master ( 209f8f...1618ee )
by Gabriel
06:05
created

HasFileLoader::setPrivateKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Providers\Mobilpay\FileLoader;
4
5
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethodRecord;
6
use Nip\Filesystem\Filesystem;
7
8
/**
9
 * Trait HasFileLoader
10
 * @package ByTIC\Payments\Gateways\Providers\Mobilpay\FileLoader
11
 *
12
 * @method PaymentMethodRecord getPaymentMethod
13
 */
14
trait HasFileLoader
15
{
16
    /**
17
     * @inheritDoc
18
     */
19 11
    public function initialize(array $parameters = [])
20
    {
21 11
        $this->initParamsForKeys($parameters);
22
23 11
        return parent::initialize($parameters);
24
    }
25
26
    /**
27
     * @param $parameters
28
     */
29 11
    protected function initParamsForKeys(&$parameters)
30
    {
31 11
        if (isset($parameters['private-key'])) {
32 9
            $parameters['privateKey'] = $parameters['private-key'];
33 9
            unset($parameters['private-key']);
34
        }
35
36 11
        if (isset($parameters['file']) && is_array($parameters['file'])) {
37
            $parameters['file'] = 'public.cer';
38
        }
39
40 11
        if (isset($parameters['privateKey']) && is_array($parameters['privateKey'])) {
41
            $parameters['privateKey'] = 'private.key';
42
        }
43 11
    }
44
45
    /**
46
     * @param $certificate
47
     * @throws \Exception
48
     */
49 9
    public function setFile($certificate)
50
    {
51 9
        if ($certificate == 'public.cer') {
52 1
            $certificate = $this->loadFileIntoModel('certificate');
53
        }
54
55 9
        return parent::setCertificate($certificate);
56
    }
57
58
    /**
59
     * @param $key
60
     * @return mixed
61
     */
62 9
    public function setPrivateKey(string $key)
63
    {
64 9
        if ($key == 'private.key') {
65 1
            $key = $this->loadFileIntoModel('privateKey');
66
        }
67
68 9
        return parent::setPrivateKey($key);
69
    }
70
71
    /**
72
     * @param $type
73
     * @return false|string
74
     */
75 1
    protected function loadFileIntoModel($type)
76
    {
77 1
        $filename = $this->getFilePath($type);
78 1
        $filesystem = new Filesystem();
79 1
        if (!file_exists($filename)) {
80
            return $filename;
81
        }
82
83 1
        $options = $this->getPaymentMethod()->getPaymentGatewayOptions();
84 1
        $content = file_get_contents($filename);
85 1
        $options[$type] = $content;
86 1
        if ($type == 'certificate') {
87 1
            unset($options['file']);
88 1
        } elseif ($type == 'privateKey') {
89 1
            unset($options['private-key']);
90
        }
91 1
        $this->getPaymentMethod()->setPaymentGatewayOptions($options);
92 1
        $this->getPaymentMethod()->save();
93
94 1
        $filesystem->remove($filename);
95 1
        @rmdir($this->getFileDirectoryPath());
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

95
        /** @scrutinizer ignore-unhandled */ @rmdir($this->getFileDirectoryPath());

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
96 1
        return $content;
97
    }
98
99
    /**
100
     * @param $type
101
     * @return bool|string
102
     * @throws \Exception
103
     */
104
    protected function validateFilePath($type)
105
    {
106
        $path = $this->getParameter($type);
0 ignored issues
show
Bug introduced by
It seems like getParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

106
        /** @scrutinizer ignore-call */ 
107
        $path = $this->getParameter($type);
Loading history...
107
        if (strpos($path, DIRECTORY_SEPARATOR) === false) {
108
            $path = $this->getFilePath($type);
109
        }
110
111
        return $this->setParameter($type, $path);
0 ignored issues
show
Bug introduced by
It seems like setParameter() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

111
        return $this->/** @scrutinizer ignore-call */ setParameter($type, $path);
Loading history...
112
    }
113
114
    /**
115
     * @param $type
116
     * @return string
117
     */
118 1
    protected function getFilePath($type)
119
    {
120
        $fileName = [
121 1
            'certificate' => 'public.cer',
122
            'privateKey' => 'private.key',
123
        ];
124
125 1
        return $this->getFileDirectoryPath() . $fileName[$type];
126
    }
127
128
    /**
129
     * @return string
130
     */
131 1
    protected function getFileDirectoryPath()
132
    {
133 1
        return $this->getPaymentMethod() ? $this->getPaymentMethod()->getFilesDirectory() : null;
0 ignored issues
show
Bug introduced by
The method getFilesDirectory() does not exist on ByTIC\Payments\Models\Methods\Traits\RecordTrait. Did you maybe mean getFiles()? ( Ignorable by Annotation )

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

133
        return $this->getPaymentMethod() ? $this->getPaymentMethod()->/** @scrutinizer ignore-call */ getFilesDirectory() : null;

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
134
    }
135
}
136