Completed
Push — master ( 1b2800...4fe10b )
by Jan
04:09
created

AttachmentURLGenerator::getViewURL()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 18
rs 9.9666
c 1
b 0
f 0
1
<?php
2
/**
3
 *
4
 * part-db version 0.1
5
 * Copyright (C) 2005 Christoph Lechner
6
 * http://www.cl-projects.de/
7
 *
8
 * part-db version 0.2+
9
 * Copyright (C) 2009 K. Jacobs and others (see authors.php)
10
 * http://code.google.com/p/part-db/
11
 *
12
 * Part-DB Version 0.4+
13
 * Copyright (C) 2016 - 2019 Jan Böhmer
14
 * https://github.com/jbtronics
15
 *
16
 * This program is free software; you can redistribute it and/or
17
 * modify it under the terms of the GNU General Public License
18
 * as published by the Free Software Foundation; either version 2
19
 * of the License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU General Public License
27
 * along with this program; if not, write to the Free Software
28
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
29
 *
30
 */
31
32
namespace App\Services\Attachments;
33
34
35
use App\Entity\Attachments\Attachment;
36
use App\Services\AttachmentHelper;
37
use Liip\ImagineBundle\Service\FilterService;
38
use Symfony\Component\Asset\Package;
39
use Symfony\Component\Asset\Packages;
40
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
41
42
class AttachmentURLGenerator
43
{
44
    protected $assets;
45
    protected $public_path;
46
    protected $pathResolver;
47
    protected $urlGenerator;
48
    protected $attachmentHelper;
49
    protected $filterService;
50
51
    public function __construct(Packages $assets, AttachmentPathResolver $pathResolver,
52
                                UrlGeneratorInterface $urlGenerator, AttachmentHelper $attachmentHelper,
53
                                FilterService $filterService)
54
    {
55
        $this->assets = $assets;
56
        $this->pathResolver = $pathResolver;
57
        $this->urlGenerator = $urlGenerator;
58
        $this->attachmentHelper = $attachmentHelper;
59
        $this->filterService = $filterService;
60
61
62
        //Determine a normalized path to the public folder (assets are relative to this folder)
63
        $this->public_path = $this->pathResolver->parameterToAbsolutePath('public');
64
    }
65
66
    /**
67
     * Converts the absolute file path to a version relative to the public folder, that can be passed to asset
68
     * Asset Component functions.
69
     * @param string $absolute_path The absolute path that should be converted.
70
     * @param string|null $public_path The public path to which the relative pathes should be created.
71
     * The path must NOT have a trailing slash!
72
     * If this is set to null, the global public/ folder is used.
73
     * @return string|null The relative version of the string. Null if the absolute path was not a child folder
74
     * of public path
75
     */
76
    public function absolutePathToAssetPath(string $absolute_path, ?string $public_path = null) : ?string
77
    {
78
        if ($public_path === null) {
79
            $public_path = $this->public_path;
80
        }
81
82
        //Our absolute path must begin with public path or we can not use it for asset pathes.
83
        if (strpos($absolute_path, $public_path) !== 0) {
84
            return null;
85
        }
86
87
        //Return the part relative after public path.
88
        return substr($absolute_path, strlen($public_path) + 1);
89
    }
90
91
    /**
92
     * Returns a URL under which the attachment file can be viewed.
93
     * @param Attachment $attachment
94
     * @return string
95
     */
96
    public function getViewURL(Attachment $attachment) : string
97
    {
98
        $absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
99
        if ($absolute_path === null) {
100
            throw new \RuntimeException(
101
                'The given attachment is external or has no valid file, so no URL can get generated for it!
102
                Use Attachment::getURL() to get the external URL!'
103
            );
104
        }
105
106
        $asset_path = $this->absolutePathToAssetPath($absolute_path);
107
        //If path is not relative to public path or marked as secure, serve it via controller
108
        if ($asset_path === null || $attachment->isSecure()) {
109
            return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
110
        }
111
112
        //Otherwise we can serve the relative path via Asset component
113
        return $this->assets->getUrl($asset_path);
114
    }
115
116
    /**
117
     * Returns a URL to an thumbnail of the attachment file.
118
     * @param Attachment $attachment
119
     * @param string $filter_name
120
     * @return string
121
     */
122
    public function getThumbnailURL(Attachment $attachment, string $filter_name = 'thumbnail_sm') : string
0 ignored issues
show
Unused Code introduced by
The parameter $filter_name is not used and could be removed. ( Ignorable by Annotation )

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

122
    public function getThumbnailURL(Attachment $attachment, /** @scrutinizer ignore-unused */ string $filter_name = 'thumbnail_sm') : string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
123
    {
124
        if (!$attachment->isPicture()) {
125
            throw new \InvalidArgumentException('Thumbnail creation only works for picture attachments!');
126
        }
127
128
        $absolute_path = $this->attachmentHelper->toAbsoluteFilePath($attachment);
129
        if ($absolute_path === null) {
130
            throw new \RuntimeException(
131
                'The given attachment is external or has no valid file, so no URL can get generated for it!
132
                Use Attachment::getURL() to get the external URL!'
133
            );
134
        }
135
136
        $asset_path = $this->absolutePathToAssetPath($absolute_path);
137
        //If path is not relative to public path or marked as secure, serve it via controller
138
        if ($asset_path === null || $attachment->isSecure()) {
139
            return $this->urlGenerator->generate('attachment_view', ['id' => $attachment->getID()]);
140
        }
141
142
        //Otherwise we can serve the relative path via Asset component
143
        return $this->filterService->getUrlOfFilteredImage($asset_path, 'thumbnail_sm');
144
    }
145
146
    /**
147
     * Returns a download link to the file associated with the attachment
148
     * @param Attachment $attachment
149
     * @return string
150
     */
151
    public function getDownloadURL(Attachment $attachment) : string
152
    {
153
        //Redirect always to download controller, which sets the correct headers for downloading:
154
        $this->urlGenerator->generate('attachment_download', ['id' => $attachment->getID()]);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
155
    }
156
}