Completed
Pull Request — master (#239)
by Luc
04:59
created

ConstraintAwareLabelService   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 0
loc 45
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createLabelAggregateIfNew() 0 17 3
1
<?php
2
3
namespace CultuurNet\UDB3\Label;
4
5
use Broadway\Repository\RepositoryInterface;
6
use Broadway\UuidGenerator\UuidGeneratorInterface;
7
use CultuurNet\UDB3\EventSourcing\DBAL\UniqueConstraintException;
8
use CultuurNet\UDB3\Label\ValueObjects\LabelName;
9
use CultuurNet\UDB3\Label\ValueObjects\Privacy;
10
use CultuurNet\UDB3\Label\ValueObjects\Visibility;
11
use ValueObjects\Identity\UUID;
12
13
class ConstraintAwareLabelService implements LabelServiceInterface
14
{
15
    /**
16
     * @var RepositoryInterface
17
     */
18
    private $labelRepository;
19
20
    /**
21
     * @var UuidGeneratorInterface
22
     */
23
    private $uuidGenerator;
24
25
    /**
26
     * @param RepositoryInterface $labelRepository
27
     * @param UuidGeneratorInterface $uuidGenerator
28
     */
29
    public function __construct(
30
        RepositoryInterface $labelRepository,
31
        UuidGeneratorInterface $uuidGenerator
32
    ) {
33
        $this->labelRepository = $labelRepository;
34
        $this->uuidGenerator = $uuidGenerator;
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function createLabelAggregateIfNew(LabelName $labelName, $visible)
41
    {
42
        try {
43
            $labelAggregate = Label::create(
44
                new UUID($this->uuidGenerator->generate()),
45
                $labelName,
46
                $visible ? Visibility::VISIBLE() : Visibility::INVISIBLE(),
47
                Privacy::PRIVACY_PUBLIC()
48
            );
49
50
            $this->labelRepository->save($labelAggregate);
51
52
            return $labelAggregate->getAggregateRootId();
53
        } catch (UniqueConstraintException $exception) {
54
            return null;
55
        }
56
    }
57
}
58