CreateImportForMediaGroupDataTransferObject   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A add() 0 8 2
A getMediaGroupEntity() 0 4 1
A hasExistingMediaGroup() 0 4 1
A setMediaGroupEntity() 0 4 1
1
<?php
2
3
namespace Backend\Modules\MediaLibraryImporter\Domain\MediaGroupImport\Command\Base;
4
5
use Backend\Modules\MediaLibrary\Domain\MediaGroup\MediaGroup;
6
use Backend\Modules\MediaLibrary\Domain\MediaGroup\Type;
7
use Backend\Modules\MediaLibraryImporter\Domain\MediaItemImport\MediaItemImportDataTransferObject;
8
9
class CreateImportForMediaGroupDataTransferObject
10
{
11
    /** @var MediaGroup|null */
12
    private $mediaGroupEntity;
13
14
    /** @var array|MediaItemImportDataTransferObject[] */
15
    public $mediaItemImportDataTransferObjects = [];
16
17
    /** @var Type */
18
    public $type;
19
20
    public function __construct(MediaGroup $mediaGroup = null)
21
    {
22
        $this->mediaGroupEntity = $mediaGroup;
23
24
        if (!$this->hasExistingMediaGroup()) {
25
            return;
26
        }
27
28
        $this->type = $mediaGroup->getType();
29
    }
30
31
    public function add(MediaItemImportDataTransferObject $mediaItemImportDataTransferObject): void
32
    {
33
        if ($this->hasExistingMediaGroup()) {
34
            $mediaItemImportDataTransferObject->setMediaGroup($this->mediaGroupEntity);
0 ignored issues
show
Bug introduced by
It seems like $this->mediaGroupEntity can be null; however, setMediaGroup() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
35
        }
36
37
        $this->mediaItemImportDataTransferObjects[] = $mediaItemImportDataTransferObject;
38
    }
39
40
    public function getMediaGroupEntity(): MediaGroup
41
    {
42
        return $this->mediaGroupEntity;
43
    }
44
45
    public function hasExistingMediaGroup(): bool
46
    {
47
        return $this->mediaGroupEntity instanceof MediaGroup;
48
    }
49
50
    public function setMediaGroupEntity(MediaGroup $mediaGroup): void
51
    {
52
        $this->mediaGroupEntity = $mediaGroup;
53
    }
54
}
55