Passed
Push — master ( 5b171c...f866ad )
by Gabriel
07:32
created

MediaOperationsTraits::rehydrateFromSibling()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ByTIC\MediaLibrary\Collections\Traits;
4
5
use ByTIC\MediaLibrary\Collections\Collection;
6
use ByTIC\MediaLibrary\Media\Media;
7
8
/**
9
 * Trait MediaOperationsTraits.
10
 */
11
trait MediaOperationsTraits
12
{
13
    /** @inheritDoc
14
     */
15
    public function filter(callable $callback = null)
16
    {
17
        $filtered = parent::filter($callback);
18
        $filtered->rehydrateFromSibling($this);
19
        return $filtered;
20
    }
21
22
    /**
23
     * @param string $key
24
     */
25
    public function deleteMediaByKey($key)
26
    {
27
        $this->get($key)->delete();
0 ignored issues
show
Bug introduced by
It seems like get() 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

27
        $this->/** @scrutinizer ignore-call */ 
28
               get($key)->delete();
Loading history...
28
        $this->unset($key);
0 ignored issues
show
Bug introduced by
It seems like unset() 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

28
        $this->/** @scrutinizer ignore-call */ 
29
               unset($key);
Loading history...
29
    }
30
31
    public function delete()
32
    {
33
        foreach ($this as $key => $file) {
34
            /* @var Media $file */
35
            $file->delete();
36
            $this->unset($key);
37
        }
38
        if (isset($file)) {
39
            $directory = dirname($file->getPath());
40
            $this->deleteDirIfEmpty($directory);
41
        }
42
    }
43
44
    /**
45
     * @param $directory
46
     */
47
    protected function deleteDirIfEmpty($directory)
48
    {
49
        $contents = $this->getFilesystem()->listContents($directory);
0 ignored issues
show
Bug introduced by
It seems like getFilesystem() 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
        $contents = $this->/** @scrutinizer ignore-call */ getFilesystem()->listContents($directory);
Loading history...
50
        if (empty($contents)) {
51
            $this->getFilesystem()->deleteDir($directory);
52
        }
53
    }
54
55
    /**
56
     * @param static $sibling
57
     */
58
    protected function rehydrateFromSibling($sibling)
59
    {
60
        $this->setName($sibling->getName());
0 ignored issues
show
Bug introduced by
It seems like setName() 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

60
        $this->/** @scrutinizer ignore-call */ 
61
               setName($sibling->getName());
Loading history...
Bug introduced by
It seems like getName() 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

60
        $this->setName($sibling->/** @scrutinizer ignore-call */ getName());
Loading history...
61
        $this->setMediaRepository($sibling->getMediaRepository());
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

61
        $this->setMediaRepository($sibling->/** @scrutinizer ignore-call */ getMediaRepository());
Loading history...
Bug introduced by
It seems like setMediaRepository() 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

61
        $this->/** @scrutinizer ignore-call */ 
62
               setMediaRepository($sibling->getMediaRepository());
Loading history...
62
        $this->setMediaType($sibling->getMediaType());
0 ignored issues
show
Bug introduced by
It seems like setMediaType() 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

62
        $this->/** @scrutinizer ignore-call */ 
63
               setMediaType($sibling->getMediaType());
Loading history...
Bug introduced by
It seems like getMediaType() 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

62
        $this->setMediaType($sibling->/** @scrutinizer ignore-call */ getMediaType());
Loading history...
63
    }
64
}
65