MediaOperationsTraits::filter()   A
last analyzed

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 1
Bugs 0 Features 1
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 1
b 0
f 1
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
 * @method Media get($key)
12
 */
13
trait MediaOperationsTraits
14
{
15
    /** @inheritDoc
16
     */
17 7
    public function filter(callable $callback = null): \Nip\Collections\CollectionInterface
18
    {
19 7
        $filtered = parent::filter($callback);
20 7
        $filtered->rehydrateFromSibling($this);
21 7
        return $filtered;
22
    }
23
24
    /**
25
     * @param string $key
26
     */
27 2
    public function deleteMediaByKey($key)
28
    {
29 2
        $this->get($key)->delete();
30 2
        $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

30
        $this->/** @scrutinizer ignore-call */ 
31
               unset($key);
Loading history...
31 2
    }
32
33
    public function delete()
34
    {
35
        foreach ($this as $key => $file) {
36
            $this->deleteMediaByKey($key);
37
        }
38
39
        if (isset($file)) {
40
            $directory = dirname($file->getPath());
41
            $this->deleteDirIfEmpty($directory);
42
        }
43
    }
44
45
    /**
46
     * @param $directory
47
     */
48
    protected function deleteDirIfEmpty($directory)
49
    {
50
        $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

50
        $contents = $this->/** @scrutinizer ignore-call */ getFilesystem()->listContents($directory);
Loading history...
51
        if (empty($contents)) {
52
            $this->getFilesystem()->deleteDir($directory);
53
        }
54
    }
55
56
    /**
57
     * @param static $sibling
58
     */
59 7
    protected function rehydrateFromSibling($sibling)
60
    {
61 7
        $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

61
        $this->/** @scrutinizer ignore-call */ 
62
               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

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

62
        $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

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

63
        $this->setMediaType($sibling->/** @scrutinizer ignore-call */ getMediaType());
Loading history...
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

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

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

64
        $this->/** @scrutinizer ignore-call */ 
65
               setLoader($sibling->getLoader());
Loading history...
65 7
    }
66
}
67