Passed
Push — master ( e779bf...8d157f )
by Gabriel
03:58
created

File::initUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
eloc 1
c 2
b 1
f 1
dl 0
loc 3
rs 10
ccs 0
cts 2
cp 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
namespace Nip\Filesystem;
3
4
use League\Flysystem\FilesystemInterface;
5
6
/**
7
 * Class File
8
 * @package Nip\Filesystem
9
 *
10
 * @method FilesystemInterface|FileDisk getFilesystem
11
 */
12
class File extends \League\Flysystem\File
0 ignored issues
show
Deprecated Code introduced by
The class League\Flysystem\File has been deprecated. ( Ignorable by Annotation )

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

12
class File extends /** @scrutinizer ignore-deprecated */ \League\Flysystem\File
Loading history...
13
{
14
    /**
15
     * @var
16
     */
17
    protected $name;
18
    /**
19
     * @var
20
     */
21
    protected $url;
22
    /**
23
     * @inheritdoc
24
     */
25
    public function __construct(FilesystemInterface $filesystem = null, $path = null)
26
    {
27
        $this->parseNameFromPath($path);
28
        return parent::__construct($filesystem, $path);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::__construct($filesystem, $path) targeting League\Flysystem\Handler::__construct() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
29
    }
30
    /**
31
     * @param $path
32
     */
33
    protected function parseNameFromPath($path)
34
    {
35
        $name = pathinfo($path, PATHINFO_BASENAME);
36
        $this->setName($name);
37
    }
38
    
39
    /**
40
     * @param string $name
41
     * @return $this
42
     */
43
    public function setFileName($name)
44
    {
45
        $path_parts = pathinfo($this->getPath());
46
        $path_parts['filename'] = $name;
47
        $this->setPath(
48
            $path_parts['dirname']
49
            . '/' . $path_parts['filename'] . '.' . $path_parts['extension']
50
        );
51
        return $this;
52
    }
53
54
    /**
55
     * Get File path with init check
56
     *
57
     * @return string
58
     */
59
    public function getPath()
60
    {
61
        if (!$this->path) {
62
            $this->initPath();
63
        }
64
        return parent::getPath();
65
    }
66
    /**
67
     * @return void
68
     */
69
    protected function initPath()
70
    {
71
        $this->setPath($this->getPathFolder() . $this->getName());
72
    }
73
74
    /**
75
     * @inheritdoc
76
     * @param string $path
77
     */
78
    public function setPath($path)
79
    {
80
        $this->parseNameFromPath($path);
81
        return parent::setPath($path);
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getPathFolder()
88
    {
89
        return '/';
90
    }
91
92
    /**
93
     * @return mixed
94
     */
95
    public function getName()
96
    {
97
        if (!$this->name) {
98
            $this->initName();
99
        }
100
        return $this->name;
101
    }
102
    /**
103
     * @param string $name
104
     * @return $this
105
     */
106
    public function setName($name)
107
    {
108
        $this->name = $name;
109
        return $this;
110
    }
111
112
    protected function initName()
113
    {
114
        $this->name = $this->getDefaultName();
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getDefaultName()
121
    {
122
        return 'file';
123
    }
124
125
    public function download(): \Symfony\Component\HttpFoundation\StreamedResponse
126
    {
127
        return $this->getFilesystem()->download($this->getPath(), $this->getName());
128
    }
129
130
    /**
131
     * @return mixed
132
     */
133
    public function getUrl()
134
    {
135
        if (!$this->url) {
136
            $this->initUrl();
137
        }
138
        return $this->url;
139
    }
140
141
    protected function initUrl()
142
    {
143
        $this->url = $this->getFilesystem()->getUrl($this->getPath());
144
    }
145
}
146