FilesShortcodes::addFileFromContent()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 7
c 2
b 1
f 0
nc 1
nop 2
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ByTIC\MediaLibrary\HasMedia\StandardCollections;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\FileAdder\FileAdder;
7
use ByTIC\MediaLibrary\Media\Media;
8
9
/**
10
 * Trait StandardCollectionsShortcodes.
11
 */
12
trait FilesShortcodes
13
{
14
    /**
15
     * @return Collection|Media[]
16
     *
17
     * @deprecated Use getFiles
18
     */
19
    public function findFiles()
20
    {
21
        return $this->getFiles();
22
    }
23
24
    /**
25
     * @return Collection|Media[]
26
     */
27
    public function getFiles()
28
    {
29 7
        return $this->getMedia('files');
30
    }
31 7
32
    /**
33
     * @param $file
34
     *
35
     * @return FileAdder
36
     */
37
    public function addFile($file)
38
    {
39 2
        return $this->addMediaToCollection($file, 'files');
0 ignored issues
show
Bug introduced by
It seems like addMediaToCollection() 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

39
        return $this->/** @scrutinizer ignore-call */ addMediaToCollection($file, 'files');
Loading history...
40
    }
41 2
42
    /**
43
     * @param $content
44
     * @param $name
45
     */
46
    public function addFileFromContent($content, $name)
47
    {
48 1
        $tmpFile = tempnam(sys_get_temp_dir(), 'bytic-media-library');
49
        file_put_contents($tmpFile, $content);
50 1
51 1
        $fileAdder = $this->addMedia($tmpFile);
0 ignored issues
show
Bug introduced by
It seems like addMedia() 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
        /** @scrutinizer ignore-call */ 
52
        $fileAdder = $this->addMedia($tmpFile);
Loading history...
52 1
        $fileAdder->setFileName($name);
53 1
        $fileAdder->toMediaCollection('files');
54 1
55
        unlink($tmpFile);
56
        return $fileAdder;
57
    }
58
59
    /**
60
     * @param string $collectionName
61
     * @param array  $filters
62
     *
63
     * @return Collection
64
     */
65
    abstract public function getMedia(string $collectionName = 'default', $filters = []): Collection;
66
}
67