Completed
Pull Request — master (#359)
by Luc
08:30
created

JSONLDMainLanguageQuery   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A execute() 0 16 3
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\ReadModel\MainLanguage;
4
5
use CultuurNet\UDB3\EntityNotFoundException;
6
use CultuurNet\UDB3\Event\ReadModel\DocumentRepositoryInterface;
7
use CultuurNet\UDB3\Language;
8
9
class JSONLDMainLanguageQuery implements MainLanguageQueryInterface
10
{
11
    /**
12
     * @var DocumentRepositoryInterface
13
     */
14
    private $documentRepository;
15
16
    /**
17
     * @var Language|null
18
     */
19
    private $fallbackLanguage;
20
21
    /**
22
     * @param DocumentRepositoryInterface $documentRepository
23
     * @param Language $fallbackLanguage
24
     */
25
    public function __construct(
26
        DocumentRepositoryInterface $documentRepository,
27
        Language $fallbackLanguage
28
    ) {
29
        $this->documentRepository = $documentRepository;
30
        $this->fallbackLanguage = $fallbackLanguage;
31
    }
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function execute($cdbid)
37
    {
38
        $document = $this->documentRepository->get($cdbid);
39
40
        if (!$document) {
41
            throw new EntityNotFoundException('Could not load JSON-LD document for cdbid ' . $cdbid);
42
        }
43
44
        $json = $document->getBody();
45
46
        if (isset($json->mainLanguage)) {
47
            return new Language($json->mainLanguage);
48
        } else {
49
            return $this->fallbackLanguage;
50
        }
51
    }
52
}
53