Completed
Push — master ( e44baf...dee4cd )
by Gabriel
12:09 queued 11s
created

FileDisk::concatPathToUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Nip\Filesystem;
4
5
use League\Flysystem\Adapter\Local as LocalAdapter;
6
use League\Flysystem\AwsS3v3\AwsS3Adapter;
7
use League\Flysystem\Filesystem as Flysystem;
8
use Nip\Utility\Str;
9
use RuntimeException;
10
use Symfony\Component\HttpFoundation\File\UploadedFile;
11
12
//use League\Flysystem\AwsS3v3\AwsS3Adapter;
13
14
/**
15
 * Class FlysystemAdapter
16
 * @package Nip\Filesystem
17
 */
18
class FileDisk extends Flysystem
19
{
20
21
    /**
22
     * Store the uploaded file on the disk with a given name.
23
     *
24
     * @param string $path
25
     * @param UploadedFile $file
26
     * @param string $name
27
     * @param array $options
28
     * @return string|false
29
     */
30
    public function putFileAs($path, $file, $name, $options = [])
31
    {
32
        $stream = fopen($file->getRealPath(), 'r+');
33
34
        // Next, we will format the path of the file and store the file using a stream since
35
        // they provide better performance than alternatives. Once we write the file this
36
        // stream will get closed automatically by us so the developer doesn't have to.
37
        $result = $this->put(
38
            $path = trim($path . '/' . $name, '/'),
39
            $stream,
0 ignored issues
show
Documentation introduced by
$stream is of type resource, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
40
            $options
41
        );
42
43
        if (is_resource($stream)) {
44
            fclose($stream);
45
        }
46
        return $result ? $path : false;
47
    }
48
49
    /**
50
     * Get the URL for the file at the given path.
51
     *
52
     * @param string $path
53
     * @return string
54
     */
55
    public function getUrl($path)
56
    {
57
        $adapter = $this->getAdapter();
58
59
        if (method_exists($adapter, 'getUrl')) {
60
            return $adapter->getUrl($path);
0 ignored issues
show
Bug introduced by
The method getUrl() does not seem to exist on object<League\Flysystem\AdapterInterface>.

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...
61
        } elseif ($adapter instanceof AwsS3Adapter) {
62
            return $this->getAwsUrl($adapter, $path);
63
        } elseif ($adapter instanceof LocalAdapter) {
64
            return $this->getLocalUrl($path);
65
        } else {
66
            throw new RuntimeException('This driver does not support retrieving URLs.');
67
        }
68
    }
69
70
    /**
71
     * Get the URL for the file at the given path.
72
     *
73
     * @param string $path
74
     * @return string
75
     */
76
    protected function getLocalUrl($path)
77
    {
78
        $config = $this->getConfig();
79
80
        // If an explicit base URL has been set on the disk configuration then we will use
81
        // it as the base URL instead of the default path. This allows the developer to
82
        // have full control over the base path for this filesystem's generated URLs.
83
        if ($config->has('url')) {
84
            return $this->concatPathToUrl($config->get('url'), $path);
85
        }
86
        $path = '/storage/' . $path;
87
        // If the path contains "storage/public", it probably means the developer is using
88
        // the default disk to generate the path instead of the "public" disk like they
89
        // are really supposed to use. We will remove the public from this path here.
90
        if (Str::contains($path, '/storage/public/')) {
91
            return Str::replaceFirst('/public/', '/', $path);
92
        } else {
93
            return $path;
94
        }
95
    }
96
97
    /**
98
     * Get the URL for the file at the given path.
99
     *
100
     * @param \League\Flysystem\AwsS3v3\AwsS3Adapter $adapter
101
     * @param string $path
102
     * @return string
103
     */
104
    protected function getAwsUrl($adapter, $path)
105
    {
106
        // If an explicit base URL has been set on the disk configuration then we will use
107
        // it as the base URL instead of the default path. This allows the developer to
108
        // have full control over the base path for this filesystem's generated URLs.
109
        if (!is_null($url = $this->getConfig()->get('url'))) {
110
            return $this->concatPathToUrl($url, $adapter->getPathPrefix() . $path);
111
        }
112
113
        return $adapter->getClient()->getObjectUrl(
114
            $adapter->getBucket(),
115
            $adapter->getPathPrefix() . $path
116
        );
117
    }
118
119
    /**
120
     * Concatenate a path to a URL.
121
     *
122
     * @param string $url
123
     * @param string $path
124
     * @return string
125
     */
126
    protected function concatPathToUrl($url, $path)
127
    {
128
        return rtrim($url, '/') . '/' . ltrim($path, '/');
129
    }
130
}
131