Passed
Push — master ( 859573...a81ea2 )
by Gabriel
03:21
created

FileMethodsTrait::generateFileFromContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Media\Traits;
4
5
use ByTIC\MediaLibrary\Loaders\Database;
6
use ByTIC\MediaLibrary\Support\MediaModels;
7
use Exception;
8
use League\Flysystem\File as FileLeague;
9
use Nip\Filesystem\File;
10
11
/**
12
 * Trait FileMethodsTrait.
13
 */
14
trait FileMethodsTrait
15
{
16
    /**
17
     * @var File|FileLeague
18
     */
19
    protected $file;
20
21
    /**
22
     * @return File
23
     */
24 7
    public function getFile()
25
    {
26 7
        return $this->file;
27
    }
28
29
    /**
30
     * @param File $file
31
     */
32 7
    public function setFile(File $file)
33
    {
34 7
        $this->file = $file;
35 7
    }
36
37
    /**
38
     * @return bool
39
     */
40 2
    public function hasFile()
41
    {
42 2
        return $this->getFile() instanceof File;
43
    }
44
45
    /**
46
     * @param $path
47
     * @param $contents
48
     */
49 2
    public function generateFileFromContent($path, $contents)
50
    {
51 2
        $this->getCollection()->getFilesystem()->put(
0 ignored issues
show
Bug introduced by
It seems like getCollection() 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

51
        $this->/** @scrutinizer ignore-call */ 
52
               getCollection()->getFilesystem()->put(
Loading history...
52 2
            $path,
53
            $contents
54
        );
55
56 2
        $file = new File($this->getCollection()->getFilesystem(), $path);
57 2
        $this->setFile($file);
58 2
    }
59
60
    /**
61
     * @return string
62
     */
63 7
    public function getName()
64
    {
65 7
        return $this->getFile()->getName();
66
    }
67
68
    /**
69
     * @return string
70
     */
71 1
    public function read()
72
    {
73 1
        return $this->getFile()->read();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getFile()->read() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
74
    }
75
76
    /**
77
     * @return bool
78
     */
79 2
    public function delete()
80
    {
81 2
        $this->deleteMediaFromDatabase();
82 2
        $this->deleteMediaFromFilesystem();
83 2
        return true;
84
    }
85
86 2
    protected function deleteMediaFromDatabase()
87
    {
88 2
        $loader = $this->getCollection()->getLoader();
89 2
        if ($loader instanceof Database) {
90
            MediaModels::records()->deteleMedia($this);
91
        }
92 2
    }
93
94 2
    protected function deleteMediaFromFilesystem()
95
    {
96 2
        $this->removeConversions();
0 ignored issues
show
Bug introduced by
It seems like removeConversions() 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

96
        $this->/** @scrutinizer ignore-call */ 
97
               removeConversions();
Loading history...
97 2
        if ($this->getFile()->exists()) {
98 2
            $this->getFile()->delete();
99
        }
100 2
    }
101
102
    /**
103
     * Get the path to the original media file.
104
     *
105
     * @param string $conversionName
106
     *
107
     * @throws Exception
108
     *
109
     * @return string
110
     */
111 2
    public function getPath(string $conversionName = ''): string
112
    {
113 2
        if (!$this->hasFile()) {
114
            throw  new Exception('Error getting path for media with no file');
115
        }
116
117 2
        if ($conversionName) {
118 2
            return $this->getBasePath($conversionName) . DIRECTORY_SEPARATOR . $this->getName();
0 ignored issues
show
Bug introduced by
It seems like getBasePath() 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

118
            return $this->/** @scrutinizer ignore-call */ getBasePath($conversionName) . DIRECTORY_SEPARATOR . $this->getName();
Loading history...
119
        }
120
121
        return $this->getFile()->getPath();
122
    }
123
}
124