| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace ByTIC\Payments\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\Mobilpay\FileLoader |
||||||
| 11 | * |
||||||
| 12 | * @method PaymentMethodRecord getPaymentMethod |
||||||
| 13 | */ |
||||||
| 14 | trait HasFileLoader |
||||||
| 15 | { |
||||||
| 16 | /** |
||||||
| 17 | * @inheritDoc |
||||||
| 18 | */ |
||||||
| 19 | public function initialize(array $parameters = []) |
||||||
| 20 | { |
||||||
| 21 | $this->initParamsForKeys($parameters); |
||||||
| 22 | |||||||
| 23 | return parent::initialize($parameters); |
||||||
| 24 | } |
||||||
| 25 | |||||||
| 26 | /** |
||||||
| 27 | * @param $parameters |
||||||
| 28 | */ |
||||||
| 29 | protected function initParamsForKeys(&$parameters) |
||||||
| 30 | { |
||||||
| 31 | if (isset($parameters['private-key'])) { |
||||||
| 32 | $parameters['privateKey'] = $parameters['private-key']; |
||||||
| 33 | unset($parameters['private-key']); |
||||||
| 34 | } |
||||||
| 35 | |||||||
| 36 | if (isset($parameters['file']) && is_array($parameters['file'])) { |
||||||
| 37 | $parameters['file'] = 'public.cer'; |
||||||
| 38 | } |
||||||
| 39 | |||||||
| 40 | if (isset($parameters['privateKey']) && is_array($parameters['privateKey'])) { |
||||||
| 41 | $parameters['privateKey'] = 'private.key'; |
||||||
| 42 | } |
||||||
| 43 | } |
||||||
| 44 | |||||||
| 45 | /** |
||||||
| 46 | * @param $certificate |
||||||
| 47 | * @throws \Exception |
||||||
| 48 | */ |
||||||
| 49 | public function setFile($certificate) |
||||||
| 50 | { |
||||||
| 51 | if ($certificate == 'public.cer') { |
||||||
| 52 | $certificate = $this->loadFileIntoModel('certificate'); |
||||||
| 53 | } |
||||||
| 54 | |||||||
| 55 | return parent::setCertificate($certificate); |
||||||
| 56 | } |
||||||
| 57 | |||||||
| 58 | /** |
||||||
| 59 | * @param $key |
||||||
| 60 | * @return mixed |
||||||
| 61 | */ |
||||||
| 62 | public function setPrivateKey($key) |
||||||
| 63 | { |
||||||
| 64 | if ($key == 'private.key') { |
||||||
| 65 | $key = $this->loadFileIntoModel('privateKey'); |
||||||
| 66 | } |
||||||
| 67 | |||||||
| 68 | return parent::setPrivateKey($key); |
||||||
| 69 | } |
||||||
| 70 | |||||||
| 71 | /** |
||||||
| 72 | * @param $type |
||||||
| 73 | * @return false|string |
||||||
| 74 | */ |
||||||
| 75 | protected function loadFileIntoModel($type) |
||||||
| 76 | { |
||||||
| 77 | $filename = $this->getFilePath($type); |
||||||
| 78 | $filesystem = new Filesystem(); |
||||||
| 79 | if (!file_exists($filename)) { |
||||||
| 80 | return $filename; |
||||||
| 81 | } |
||||||
| 82 | |||||||
| 83 | $options = $this->getPaymentMethod()->getPaymentGatewayOptions(); |
||||||
| 84 | $content = file_get_contents($filename); |
||||||
| 85 | $options[$type] = $content; |
||||||
| 86 | if ($type == 'certificate') { |
||||||
| 87 | unset($options['file']); |
||||||
| 88 | } elseif ($type == 'privateKey') { |
||||||
| 89 | unset($options['private-key']); |
||||||
| 90 | } |
||||||
| 91 | $this->getPaymentMethod()->setPaymentGatewayOptions($options); |
||||||
| 92 | $this->getPaymentMethod()->save(); |
||||||
| 93 | |||||||
| 94 | $filesystem->remove($filename); |
||||||
| 95 | @rmdir($this->getFileDirectoryPath()); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 96 | 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
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
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
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
Loading history...
|
|||||||
| 112 | } |
||||||
| 113 | |||||||
| 114 | /** |
||||||
| 115 | * @param $type |
||||||
| 116 | * @return string |
||||||
| 117 | */ |
||||||
| 118 | protected function getFilePath($type) |
||||||
| 119 | { |
||||||
| 120 | $fileName = [ |
||||||
| 121 | 'certificate' => 'public.cer', |
||||||
| 122 | 'privateKey' => 'private.key', |
||||||
| 123 | ]; |
||||||
| 124 | |||||||
| 125 | return $this->getFileDirectoryPath() . $fileName[$type]; |
||||||
| 126 | } |
||||||
| 127 | |||||||
| 128 | /** |
||||||
| 129 | * @return string |
||||||
| 130 | */ |
||||||
| 131 | protected function getFileDirectoryPath() |
||||||
| 132 | { |
||||||
| 133 | return $this->getPaymentMethod() ? |
||||||
| 134 | $this->getPaymentMethod()->getMediaRepository()->getCollection('files')->getBasePathForMedia() |
||||||
| 135 | : null; |
||||||
| 136 | } |
||||||
| 137 | } |
||||||
| 138 |
If you suppress an error, we recommend checking for the error condition explicitly: