Completed
Pull Request — master (#239)
by Luc
05:12
created

Projector::createOfferLabelRelation()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Label\ReadModels\Relations;
4
5
use Broadway\Domain\Metadata;
6
use CultuurNet\UDB3\Label\LabelEventRelationTypeResolverInterface;
7
use CultuurNet\UDB3\Label\ReadModels\AbstractProjector;
8
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\LabelRelation;
9
use CultuurNet\UDB3\Label\ReadModels\Relations\Repository\WriteRepositoryInterface;
10
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
11
use CultuurNet\UDB3\Offer\Events\AbstractLabelEvent as OfferAbstractLabelEvent;
12
use CultuurNet\UDB3\Organizer\Events\AbstractLabelEvent as OrganizerAbstractLabelEvent;
13
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
14
use ValueObjects\String\String as StringLiteral;
15
16
class Projector extends AbstractProjector
17
{
18
    /**
19
     * @var WriteRepositoryInterface
20
     */
21
    private $writeRepository;
22
23
    /**
24
     * @var LabelEventRelationTypeResolverInterface
25
     */
26
    private $offerTypeResolver;
27
28
    /**
29
     * Projector constructor.
30
     * @param WriteRepositoryInterface $writeRepository
31
     * @param LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver
32
     */
33
    public function __construct(
34
        WriteRepositoryInterface $writeRepository,
35
        LabelEventRelationTypeResolverInterface $labelEventOfferTypeResolver
36
    ) {
37
        $this->writeRepository = $writeRepository;
38
        $this->offerTypeResolver = $labelEventOfferTypeResolver;
39
40
    }
41
42
    /**
43
     * @inheritdoc
44
     */
45
    public function applyLabelAdded($labelAdded, Metadata $metadata)
46
    {
47
        $LabelRelation = $this->createLabelRelation($labelAdded);
48
49
        try {
50
            if (!is_null($LabelRelation)) {
51
                $this->writeRepository->save(
52
                    $LabelRelation->getLabelName(),
53
                    $LabelRelation->getRelationType(),
54
                    $LabelRelation->getRelationId()
55
                );
56
            }
57
        } catch (UniqueConstraintViolationException $exception) {
58
            // By design to catch unique exception.
59
        }
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function applyLabelDeleted($labelDeleted, Metadata $metadata)
66
    {
67
        $labelRelation = $this->createLabelRelation($labelDeleted);
68
69
        if (!is_null($labelRelation)) {
70
            $this->writeRepository->deleteByLabelNameAndRelationId(
71
                $labelRelation->getLabelName(),
72
                new StringLiteral($labelDeleted->getItemId())
0 ignored issues
show
Bug introduced by
The method getItemId does only exist in CultuurNet\UDB3\Offer\Events\AbstractLabelDeleted, but not in CultuurNet\UDB3\Organizer\Events\LabelRemoved.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
73
            );
74
        }
75
    }
76
77
    /**
78
     * @param OfferAbstractLabelEvent|OrganizerAbstractLabelEvent $labelEvent
79
     * @return LabelRelation
80
     */
81
    private function createLabelRelation($labelEvent)
82
    {
83
        $labelRelation = null;
0 ignored issues
show
Unused Code introduced by
$labelRelation is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
84
85
        $labelName = new LabelName((string) $labelEvent->getLabel());
86
        $relationType = $this->offerTypeResolver->getRelationType($labelEvent);
87
        $relationId = new StringLiteral($labelEvent->getItemId());
0 ignored issues
show
Bug introduced by
The method getItemId does only exist in CultuurNet\UDB3\Offer\Events\AbstractLabelEvent, but not in CultuurNet\UDB3\Organize...ents\AbstractLabelEvent.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
88
89
        $labelRelation = new LabelRelation(
90
            $labelName,
91
            $relationType,
92
            $relationId
93
        );
94
95
        return $labelRelation;
96
    }
97
}
98