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

guardAgainstDisallowedFileAdditions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 2
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\FileAdder\Traits;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded;
7
use ByTIC\MediaLibrary\Exceptions\FileCannotBeAdded\FileUnacceptableForCollection;
8
use ByTIC\MediaLibrary\Media\Manipulators\ManipulatorFactory;
9
use ByTIC\MediaLibrary\Media\Media;
10
use ByTIC\MediaLibrary\PathGenerator\PathGeneratorFactory;
11
use ByTIC\MediaLibrary\Validation\Violations\ViolationsBag;
12
use Nip\Filesystem\File;
13
14
/**
15
 * Trait FileAdderProcessesTrait.
16
 */
17
trait FileAdderProcessesTrait
18
{
19
    /**
20
     * @param string|Collection $collection
21
     */
22 3
    public function toMediaCollection($collection)
23
    {
24 3
        if (is_string($collection)) {
25 2
            $collection = $this->getMediaRepository()->getCollection($collection);
0 ignored issues
show
Bug introduced by
It seems like getMediaRepository() 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

25
            $collection = $this->/** @scrutinizer ignore-call */ getMediaRepository()->getCollection($collection);
Loading history...
26
        }
27 3
        $this->processMediaItem($collection);
28 2
    }
29
30
    /**
31
     * @param Collection $collection
32
     * @param Media      $media
33
     */
34 3
    protected function processMediaItem(Collection $collection, Media $media = null)
35
    {
36 3
        $this->guardAgainstDisallowedFileAdditions($collection, $this->getFile());
0 ignored issues
show
Bug introduced by
It seems like getFile() 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

36
        $this->guardAgainstDisallowedFileAdditions($collection, $this->/** @scrutinizer ignore-call */ getFile());
Loading history...
37
38 2
        $media = $media ? $media : $this->getMedia();
0 ignored issues
show
Bug introduced by
It seems like getMedia() 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

38
        $media = $media ? $media : $this->/** @scrutinizer ignore-call */ getMedia();
Loading history...
39 2
        $media->setCollection($collection);
40
41 2
        $this->copyMediaToFilesystem();
42 2
        $this->createMediaConversions();
43
44 2
        $collection->appendMedia($media);
45 2
    }
46
47 2
    protected function copyMediaToFilesystem()
48
    {
49 2
        $media = $this->getMedia();
50 2
        $destination = PathGeneratorFactory::create()::getBasePathForMediaOriginal($media);
51 2
        $destination .= DIRECTORY_SEPARATOR . $media->getCollection()->getStrategy()::makeFileName($this);
52
53 2
        $media->generateFileFromContent($destination, fopen($this->getPathToFile(), 'r'));
0 ignored issues
show
Bug introduced by
It seems like getPathToFile() 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

53
        $media->generateFileFromContent($destination, fopen($this->/** @scrutinizer ignore-call */ getPathToFile(), 'r'));
Loading history...
54
55 2
        $file = new File($media->getCollection()->getFilesystem(), $destination);
56 2
        $media->setFile($file);
57 2
    }
58
59 2
    protected function createMediaConversions()
60
    {
61 2
        $media = $this->getMedia();
62 2
        ManipulatorFactory::createForMedia($media)->performConversions(
63 2
            $this->getSubject()->getMediaConversions()->forCollection($media->getCollection()->getName()),
0 ignored issues
show
Bug introduced by
It seems like getSubject() 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

63
            $this->/** @scrutinizer ignore-call */ 
64
                   getSubject()->getMediaConversions()->forCollection($media->getCollection()->getName()),
Loading history...
64 2
            $media
65
        );
66 2
    }
67
68
    /**
69
     * @param Collection $collection
70
     * @param mixed      $file
71
     */
72 3
    protected function guardAgainstDisallowedFileAdditions(Collection $collection, $file)
73
    {
74 3
        $guardTest = ($collection->acceptsMedia)($file);
75 3
        if ($guardTest instanceof ViolationsBag) {
76
            throw FileUnacceptableForCollection::createFromViolationsBag($file, $collection, $guardTest);
77
        }
78 3
        if ($guardTest === false) {
79 1
            throw new FileCannotBeAdded();
80
        }
81 2
    }
82
}
83