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

AddMediaTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 10
c 1
b 1
f 0
dl 0
loc 41
ccs 10
cts 10
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addMediaToCollection() 0 6 1
A addMedia() 0 5 1
A getFileAdderFactory() 0 7 2
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