BitBagCommerce /
SyliusMolliePlugin
| 1 | <?php |
||||
| 2 | |||||
| 3 | /* |
||||
| 4 | * This file has been created by developers from BitBag. |
||||
| 5 | * Feel free to contact us once you face any issues or want to start |
||||
| 6 | * You can find more information about us on https://bitbag.io and write us |
||||
| 7 | * an email on [email protected]. |
||||
| 8 | */ |
||||
| 9 | |||||
| 10 | declare(strict_types=1); |
||||
| 11 | |||||
| 12 | namespace BitBag\SyliusMolliePlugin\Uploader; |
||||
| 13 | |||||
| 14 | use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface; |
||||
| 15 | use Doctrine\Common\Collections\Collection; |
||||
| 16 | use Gaufrette\Filesystem; |
||||
| 17 | |||||
| 18 | final class PaymentMethodLogoUploader implements PaymentMethodLogoUploaderInterface |
||||
| 19 | { |
||||
| 20 | /** @var Filesystem */ |
||||
| 21 | private $filesystem; |
||||
| 22 | |||||
| 23 | public function __construct(Filesystem $filesystem) |
||||
| 24 | { |
||||
| 25 | $this->filesystem = $filesystem; |
||||
| 26 | } |
||||
| 27 | |||||
| 28 | public function upload(Collection $mollieGatewayConfigs): void |
||||
| 29 | { |
||||
| 30 | /** @var MollieGatewayConfigInterface $mollieGatewayConfig */ |
||||
| 31 | foreach ($mollieGatewayConfigs as $mollieGatewayConfig) { |
||||
| 32 | if ($mollieGatewayConfig->getCustomizeMethodImage()->hasFile()) { |
||||
| 33 | $this->uploadSingle($mollieGatewayConfig); |
||||
| 34 | } |
||||
| 35 | } |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | public function remove(string $path): bool |
||||
| 39 | { |
||||
| 40 | if ($this->filesystem->has($path)) { |
||||
| 41 | return $this->filesystem->delete($path); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | return false; |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | private function uploadSingle(MollieGatewayConfigInterface $mollieGatewayConfig): void |
||||
| 48 | { |
||||
| 49 | $customizeImage = $mollieGatewayConfig->getCustomizeMethodImage(); |
||||
| 50 | $file = $customizeImage->getFile(); |
||||
| 51 | |||||
| 52 | if (null !== $customizeImage->getPath() && $this->fileExists($customizeImage->getPath())) { |
||||
| 53 | $this->remove($customizeImage->getPath()); |
||||
| 54 | } |
||||
| 55 | |||||
| 56 | do { |
||||
| 57 | $hash = md5(uniqid((string) mt_rand(), true)); |
||||
| 58 | $path = $this->expandPath($hash . '.' . $file->guessExtension()); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 59 | } while ($this->isAdBlockingProne($path) || $this->filesystem->has($path)); |
||||
| 60 | |||||
| 61 | $customizeImage->setPath($path); |
||||
| 62 | $customizeImage->setName($file->getClientOriginalName()); |
||||
|
0 ignored issues
–
show
The method
getClientOriginalName() does not exist on SplFileInfo. It seems like you code against a sub-type of SplFileInfo such as ECSPrefix20210626\Symfon...ation\File\UploadedFile or Symfony\Component\HttpFoundation\File\UploadedFile.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 63 | $this->filesystem->write( |
||||
| 64 | $customizeImage->getPath(), |
||||
| 65 | file_get_contents($file->getPathname()) |
||||
| 66 | ); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | private function fileExists(string $path): bool |
||||
| 70 | { |
||||
| 71 | return $this->filesystem->has($path); |
||||
| 72 | } |
||||
| 73 | |||||
| 74 | private function isAdBlockingProne(string $path): bool |
||||
| 75 | { |
||||
| 76 | return strpos($path, 'ad') !== false; |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | private function expandPath(string $path): string |
||||
| 80 | { |
||||
| 81 | return sprintf( |
||||
| 82 | '%s/%s/%s', |
||||
| 83 | substr($path, 0, 2), |
||||
| 84 | substr($path, 2, 2), |
||||
| 85 | substr($path, 4) |
||||
| 86 | ); |
||||
| 87 | } |
||||
| 88 | } |
||||
| 89 |