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

AddMediaTrait::addMedia()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\HasMedia\Traits;
4
5
use ByTIC\MediaLibrary\FileAdder\FileAdder;
6
use ByTIC\MediaLibrary\FileAdder\FileAdderFactory;
7
8
/**
9
 * Trait AddMediaTrait.
10
 */
11
trait AddMediaTrait
12
{
13
    /**
14
     * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file
15
     * @param string                                                     $collection
16
     *
17
     * @return FileAdder
18
     */
19 2
    public function addMediaToCollection($file, $collection)
20
    {
21 2
        $fileAdder = $this->addMedia($file);
22 2
        $fileAdder->toMediaCollection($collection);
23
24 2
        return $fileAdder;
25
    }
26
27
    /**
28
     * Add a file to the medialibrary.
29
     *
30
     * @param string|\Symfony\Component\HttpFoundation\File\UploadedFile $file
31
     *
32
     * @return FileAdder
33
     */
34 2
    public function addMedia($file)
35
    {
36 2
        return call_user_func_array(
37 2
            [self::getFileAdderFactory(), 'create'],
38 2
            [$this, $file]
39
        );
40
    }
41
42
    /**
43
     * @return FileAdderFactory|string
44
     */
45 2
    public static function getFileAdderFactory()
46
    {
47 2
        if (function_exists('app')) {
48
            return app(FileAdderFactory::class);
49
        }
50
51
        return FileAdderFactory::class;
52
    }
53
}
54