Passed
Push — master ( 240c83...03f39b )
by Mathias
06:41
created

ImageSet::__call()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 21
c 0
b 0
f 0
ccs 13
cts 13
cp 1
rs 9.5555
cc 5
nc 5
nop 2
crap 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Core\Entity;
6
7
use Core\Entity\Collection\ArrayCollection;
8
use Doctrine\Common\Collections\Collection;
9
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
10
use MongoDB\BSON\ObjectId;
11
12
/**
13
 * Class ImageSet
14
 *
15
 * @ODM\EmbeddedDocument
16
 *
17
 * @package Core\Entity
18
 */
19
class ImageSet implements ImageSetInterface
20
{
21
    use EntityTrait, IdentifiableEntityTrait;
22
23
    /**
24
     * Images in this set.
25
     *
26
     * @ODM\ReferenceMany(discriminatorField="_entity", cascade={"all"}, orphanRemoval=true)
27
     */
28
    protected Collection $images;
29
30
    public function __construct()
31
    {
32
        $this->images = new ArrayCollection();
33
        $this->id = (string) new ObjectId();
34
    }
35
36
    public function getId(): ?string
37
    {
38
        return $this->id;
39
    }
40
41
    /**
42
     * @return ArrayCollection|Collection
43
     */
44
    public function getImages(): Collection
45
    {
46
        return $this->images;
47
    }
48 14
49
    public function setImages($images): self
50 14
    {
51
        $this->images = $images;
52
        return $this;
53
    }
54
55
    public function clear(): self
56
    {
57
        $this->images->clear();
58
59
        return $this;
60
    }
61
62
    public function add(ImageInterface $image): self
63
    {
64
        $images = $this->images;
65 4
        $metadata = $image->getMetadata();
66
        if(!is_null($current = $this->get($metadata->getKey(), false))){
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $current is correct as $this->get($metadata->getKey(), false) targeting Core\Entity\ImageSet::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug introduced by
The method getKey() does not exist on Core\Entity\FileMetadataInterface. It seems like you code against a sub-type of Core\Entity\FileMetadataInterface such as Core\Entity\ImageMetadata. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if(!is_null($current = $this->get($metadata->/** @scrutinizer ignore-call */ getKey(), false))){
Loading history...
introduced by
The condition is_null($current = $this...data->getKey(), false)) is always true.
Loading history...
67 4
            $index = $images->indexOf($current);
68 1
            $images->remove($index);
69 1
        }
70
        $images->add($image);
71 1
        return $this;
72
    }
73
74 3
    public function get(string $key, bool $fallback = true): ?ImageInterface
75 2
    {
76 1
        foreach($this->images as $image){
77 1
            try{
78
                if(!is_null($metadata = $image->getMetadata())){
79
                    if($metadata->getKey() === $key){
80 1
                        return $image;
81 1
                    }
82
                }
83
            }catch (\Exception $e){
84 1
                return null;
85 1
            }
86
        }
87
        return !$fallback || self::ORIGINAL == $key ? null : $this->get(self::ORIGINAL);
88
    }
89
90
    public function getOriginal(): ?ImageInterface
91
    {
92
        return $this->get(self::ORIGINAL, false);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->get(self::ORIGINAL, false) targeting Core\Entity\ImageSet::get() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
93
    }
94 6
95
    public function getThumbnail()
96 6
    {
97 3
        return $this->get(self::THUMBNAIL);
98
    }
99
}