Passed
Push — master ( c6f9a3...c9f7be )
by Gabriel
03:32
created

FileMethodsTrait::getFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

49
        $this->/** @scrutinizer ignore-call */ 
50
               getCollection()->getFilesystem()->put(
Loading history...
50 2
            $path,
51 2
            $contents
52
        );
53
54 2
        $file = new File($this->getCollection()->getFilesystem(), $path);
55 2
        $this->setFile($file);
56 2
    }
57
58
    /**
59
     * @return string
60
     */
61 7
    public function getName()
62
    {
63 7
        return $this->getFile()->getName();
64
    }
65
66
    /**
67
     * @return string
68
     */
69 1
    public function read()
70
    {
71 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...
72
    }
73
74
    /**
75
     * @return bool
76
     */
77 2
    public function delete()
78
    {
79 2
        $converstions = $this->getConversionNames();
0 ignored issues
show
Bug introduced by
It seems like getConversionNames() 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

79
        /** @scrutinizer ignore-call */ 
80
        $converstions = $this->getConversionNames();
Loading history...
80 2
        $converstions[] = 'full';
81 2
        $filesystem = $this->getFile()->getFilesystem();
82 2
        foreach ($converstions as $converstion) {
83 2
            $path = $this->getPath($converstion);
84 2
            if ($filesystem->has($path)) {
85
                $filesystem->delete($path);
86
            }
87
        }
88 2
        if ($this->getFile()->exists()) {
89 2
            $this->getFile()->delete();
90
        }
91 2
        return true;
92
    }
93
94
    /**
95
     * Get the path to the original media file.
96
     *
97
     * @param string $conversionName
98
     *
99
     * @throws Exception
100
     *
101
     * @return string
102
     */
103 2
    public function getPath(string $conversionName = ''): string
104
    {
105 2
        if (!$this->hasFile()) {
106
            throw  new Exception('Error getting path for media with no file');
107
        }
108
109 2
        if ($conversionName) {
110 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

110
            return $this->/** @scrutinizer ignore-call */ getBasePath($conversionName) . DIRECTORY_SEPARATOR . $this->getName();
Loading history...
111
        }
112
113
        return $this->getFile()->getPath();
114
    }
115
}
116