HasPath::getUrl()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Nip\Filesystem\File;
4
5
/**
6
 * Trait HasPath
7
 * @package Nip\Filesystem\File
8
 */
9
trait HasPath
10
{
11
12
    /**
13
     * @var
14
     */
15
    protected $name;
16
17
    /**
18
     * @var string
19
     */
20
    protected $path;
21
22
    /**
23
     * @var
24
     */
25
    protected $url;
26
27
28
    /**
29
     * Set the entree path.
30
     *
31
     * @param string $path
32
     *
33
     * @return $this
34
     */
35
    public function setPath(string $path)
36
    {
37
        $this->parseNameFromPath($path);
38
        $this->path = $path;
39
40
        return $this;
41
    }
42
43
    /**
44
     * Retrieve the entree path.
45
     *
46
     * @return string path
47
     */
48
    public function getPath(): string
49
    {
50
        if (!$this->path) {
51
            $this->initPath();
52
        }
53
        return $this->path;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return $this
59
     */
60
    public function setFileName(string $name): self
61
    {
62
        $path_parts = pathinfo($this->getPath());
63
        $path_parts['filename'] = $name;
64
        $this->setPath(
65
            $path_parts['dirname']
66
            . '/' . $path_parts['filename'] . '.' . $path_parts['extension']
67
        );
68
        return $this;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getPathFolder(): string
75
    {
76
        return '/';
77
    }
78
79
    /**
80
     * @return mixed
81
     */
82
    public function getName()
83
    {
84
        if (!$this->name) {
85
            $this->initName();
86
        }
87
        return $this->name;
88
    }
89
90
    /**
91
     * @param string $name
92
     * @return $this
93
     */
94
    public function setName(string $name) : self
95
    {
96
        $this->name = $name;
97
        return $this;
98
    }
99
100
    protected function initName()
101
    {
102
        $this->name = $this->getDefaultName();
103
    }
104
105
    /**
106
     * @return string
107
     */
108
    public function getDefaultName(): string
109
    {
110
        return 'file';
111
    }
112
113
114
    /**
115
     * @return mixed
116
     */
117
    public function getUrl()
118
    {
119
        if (!$this->url) {
120
            $this->initUrl();
121
        }
122
        return $this->url;
123
    }
124
125
    protected function initUrl()
126
    {
127
        $this->url = $this->getFilesystem()->getUrl($this->getPath());
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

127
        $this->url = $this->/** @scrutinizer ignore-call */ getFilesystem()->getUrl($this->getPath());
Loading history...
128
    }
129
130
    /**
131
     * @param $path
132
     */
133
    protected function parseNameFromPath($path)
134
    {
135
        $name = pathinfo($path, PATHINFO_BASENAME);
136
        $this->setName($name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Nip\Filesystem\File\HasPath::setName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

136
        $this->setName(/** @scrutinizer ignore-type */ $name);
Loading history...
137
    }
138
139
    /**
140
     * @return void
141
     */
142
    protected function initPath()
143
    {
144
        $this->setPath($this->getPathFolder() . $this->getName());
145
    }
146
}
147