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

ConstraintAwareLabelService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
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