Passed
Push — master ( a040f6...1671cd )
by Jan
04:44
created

ExtendedFileSystemStorage   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 78
rs 10
c 3
b 0
f 0
wmc 21

6 Methods

Rating   Name   Duplication   Size   Complexity  
A doResolvePath() 0 9 3
C resolveUri() 0 38 13
A __construct() 0 4 1
A convertWindowsDirectorySeparator() 0 3 1
A doUpload() 0 5 1
A doRemove() 0 5 2
1
<?php
2
/*
3
 * Copyright (C) 2020  Jan Böhmer
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published
7
 * by the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace App\Services\Upload;
20
21
use App\Entity\PaymentOrder;
22
use App\Entity\SEPAExport;
23
use Symfony\Component\HttpFoundation\File\File;
24
use Symfony\Component\HttpFoundation\File\UploadedFile;
25
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26
use Vich\UploaderBundle\Mapping\PropertyMapping;
27
use Vich\UploaderBundle\Mapping\PropertyMappingFactory;
28
use Vich\UploaderBundle\Storage\AbstractStorage;
29
use Vich\UploaderBundle\Storage\FileSystemStorage;
30
31
/**
32
 * We need to extend the original filesystemstorage as we have stored our payment_order files privately and want to use
33
 * the normal interface to access them via URI...
34
 * Based on Vich FileSystemStorage class
35
 */
36
class ExtendedFileSystemStorage extends AbstractStorage
37
{
38
    private $router;
39
40
    public function __construct(PropertyMappingFactory $factory, UrlGeneratorInterface $router)
41
    {
42
        parent::__construct($factory);
43
        $this->router = $router;
44
    }
45
46
    public function resolveUri($obj, ?string $fieldName = null, ?string $className = null): ?string
47
    {
48
49
        [$mapping, $name] = $this->getFilename($obj, $fieldName, $className);
50
51
        if (empty($name)) {
52
            return null;
53
        }
54
55
        $uploadDir = $this->convertWindowsDirectorySeparator($mapping->getUploadDir($obj));
56
        $uploadDir = empty($uploadDir) ? '' : $uploadDir.'/';
57
58
        $tmp = \sprintf('%s/%s', $mapping->getUriPrefix(), $uploadDir.$name);
59
60
        if (null !== $tmp && $obj instanceof PaymentOrder) {
61
            if ('printed_form' === $fieldName || 'printed_form_file' === $fieldName) {
62
                return $this->router->generate('file_payment_order_form', [
63
                    'id' => $obj->getId(),
64
                ]);
65
            }
66
67
            if ('references' === $fieldName || 'references_file' === $fieldName) {
68
                return $this->router->generate('file_payment_order_references', [
69
                    'id' => $obj->getId(),
70
                ]);
71
            }
72
        }
73
74
        if (null !== $tmp && $obj instanceof SEPAExport)
75
        {
76
            if ('xml' === $fieldName || 'xml_file' === $fieldName) {
77
                return $this->router->generate('file_sepa_export_xml', [
78
                    'id' => $obj->getId(),
79
                ]);
80
            }
81
        }
82
83
        return $tmp;
84
    }
85
86
    protected function doUpload(PropertyMapping $mapping, UploadedFile $file, ?string $dir, string $name): ?File
87
    {
88
        $uploadDir = $mapping->getUploadDestination().\DIRECTORY_SEPARATOR.$dir;
89
90
        return $file->move($uploadDir, $name);
91
    }
92
93
    protected function doRemove(PropertyMapping $mapping, ?string $dir, string $name): ?bool
94
    {
95
        $file = $this->doResolvePath($mapping, $dir, $name);
96
97
        return \file_exists($file) && \unlink($file);
98
    }
99
100
    protected function doResolvePath(PropertyMapping $mapping, ?string $dir, string $name, ?bool $relative = false): string
101
    {
102
        $path = !empty($dir) ? $dir.\DIRECTORY_SEPARATOR.$name : $name;
103
104
        if ($relative) {
105
            return $path;
106
        }
107
108
        return $mapping->getUploadDestination().\DIRECTORY_SEPARATOR.$path;
109
    }
110
111
    private function convertWindowsDirectorySeparator(string $string): string
112
    {
113
        return \str_replace('\\', '/', $string);
114
    }
115
}
116