Completed
Pull Request — master (#359)
by Luc
07:09
created

JSONLDMainLanguageQuery::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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