Completed
Push — feature/6.x ( db50a0...aa9894 )
by Schlaefer
03:28
created

File   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 105
rs 10
wmc 11

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getPath() 0 3 1
A read() 0 3 1
A getSize() 0 3 1
A getFs() 0 3 1
A getMime() 0 3 1
A delete() 0 3 1
A getDirname() 0 3 1
A __construct() 0 4 2
A getBasename() 0 3 1
A exists() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * Saito - The Threaded Web Forum
6
 *
7
 * @copyright Copyright (c) the Saito Project Developers
8
 * @link https://github.com/Schlaefer/Saito
9
 * @license http://opensource.org/licenses/MIT
10
 */
11
12
namespace ImageUploader\Lib;
13
14
use League\Flysystem\Adapter\Local;
15
use League\Flysystem\Filesystem;
16
use League\Flysystem\FilesystemInterface;
17
18
class File
19
{
20
    /**
21
     * Path to file
22
     * @var string
23
     */
24
    protected string $path;
25
26
    /**
27
     * Filesystem
28
     * @var \League\Flysystem\FilesystemInterface
29
     */
30
    protected FilesystemInterface $fs;
31
32
    /**
33
     * Constructor
34
     * @param string $path path to file
35
     * @param null|\League\Flysystem\FilesystemInterface $filesystem filesystem (default: local from path)
36
     * @return void
37
     */
38
    public function __construct(string $path, ?FilesystemInterface $filesystem = null)
39
    {
40
        $this->path = $path;
41
        $this->fs = $filesystem ?: new Filesystem(new Local($this->getDirname()));
42
    }
43
44
    /**
45
     * Get Filesystem
46
     * @return \League\Flysystem\FilesystemInterface
47
     */
48
    public function getFs(): FilesystemInterface
49
    {
50
        return $this->fs;
51
    }
52
53
    /**
54
     * Get dirname of path
55
     * @return string dirname
56
     */
57
    public function getDirname(): string
58
    {
59
        return dirname($this->getPath());
60
    }
61
62
    /**
63
     * Path getter
64
     * @return string path
65
     */
66
    public function getPath(): string
67
    {
68
        return $this->path;
69
    }
70
71
    /**
72
     * Get mime type from physical file
73
     * @return string Mime Type
74
     */
75
    public function getMime(): string
76
    {
77
        return $this->fs->getMimetype($this->getBasename());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fs->getMim...e($this->getBasename()) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
78
    }
79
80
    /**
81
     * Get size from storage
82
     * @return int size
83
     */
84
    public function getSize(): int
85
    {
86
        return $this->fs->getSize($this->getBasename());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fs->getSize($this->getBasename()) could return the type false which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
89
    /**
90
     * Get basename of path
91
     * @return string basename
92
     */
93
    public function getBasename(): string
94
    {
95
        return basename($this->getPath());
96
    }
97
98
    /**
99
     * Read content from storage
100
     * @return string
101
     */
102
    public function read(): string
103
    {
104
        return $this->fs->read($this->getBasename());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->fs->read($this->getBasename()) could return the type false which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
105
    }
106
107
    /**
108
     * Check if item exists in storage
109
     * @return bool
110
     */
111
    public function exists(): bool
112
    {
113
        return $this->fs->has($this->getBasename());
114
    }
115
116
    /**
117
     * Delete a file in path
118
     * @return bool True on success, False otherwise
119
     */
120
    public function delete(): bool
121
    {
122
        return $this->fs->delete($this->getBasename());
123
    }
124
}
125