Completed
Pull Request — master (#239)
by Luc
05:42
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\Offer\Events\AbstractLabelEvent as OfferAbstractLabelEvent;
11
use CultuurNet\UDB3\Organizer\Events\AbstractLabelEvent as OrganizerAbstractLabelEvent;
12
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
13
use ValueObjects\Identity\UUID;
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 View Code Duplication
    public function applyLabelAdded($labelAdded, Metadata $metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
46
    {
47
        $LabelRelation = $this->createLabelRelation($labelAdded, $metadata);
48
49
        try {
50
            if (!is_null($LabelRelation)) {
51
                $this->writeRepository->save(
52
                    $LabelRelation->getUuid(),
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 View Code Duplication
    public function applyLabelDeleted($labelDeleted, Metadata $metadata)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $labelRelation = $this->createLabelRelation($labelDeleted, $metadata);
68
69
        if (!is_null($labelRelation)) {
70
            $this->writeRepository->deleteByUuidAndRelationId(
71
                $labelRelation->getUuid(),
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
     * @param Metadata $metadata
80
     * @return LabelRelation
81
     */
82
    private function createLabelRelation($labelEvent, Metadata $metadata)
83
    {
84
        $labelRelation = null;
85
86
        $metadataArray = $metadata->serialize();
87
88
        $uuid = isset($metadataArray['labelUuid']) ? new UUID($metadataArray['labelUuid']) : null;
89
        $relationType = $this->offerTypeResolver->getRelationType($labelEvent);
90
        $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...
91
92
        if (!is_null($uuid)) {
93
            $labelRelation = new LabelRelation(
94
                $uuid,
95
                $relationType,
96
                $relationId
97
            );
98
        }
99
100
        return $labelRelation;
101
    }
102
}
103