Completed
Pull Request — master (#364)
by Luc
15:39 queued 06:04
created

polyFillMultilingualFields()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 19
Code Lines 10

Duplication

Lines 5
Ratio 26.32 %

Importance

Changes 0
Metric Value
dl 5
loc 19
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 8
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Place\ReadModel\JSONLD;
4
5
use CultuurNet\UDB3\ReadModel\ConfigurableJsonDocumentLanguageAnalyzer;
6
use CultuurNet\UDB3\ReadModel\JsonDocument;
7
8
class PlaceJsonDocumentLanguageAnalyzer extends ConfigurableJsonDocumentLanguageAnalyzer
9
{
10
    public function __construct()
11
    {
12
        parent::__construct(
13
            [
14
                'name',
15
                'description',
16
                'address',
17
                'bookingInfo.urlLabel',
18
            ]
19
        );
20
    }
21
22
    /**
23
     * @todo Remove when full replay is done.
24
     * @replay_i18n
25
     * @see https://jira.uitdatabank.be/browse/III-2201
26
     *
27
     * @param JsonDocument $jsonDocument
28
     * @return \CultuurNet\UDB3\Language[]
29
     */
30
    public function determineAvailableLanguages(JsonDocument $jsonDocument)
31
    {
32
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
33
        return parent::determineAvailableLanguages($jsonDocument);
34
    }
35
36
    /**
37
     * @todo Remove when full replay is done.
38
     * @replay_i18n
39
     * @see https://jira.uitdatabank.be/browse/III-2201
40
     *
41
     * @param JsonDocument $jsonDocument
42
     * @return \CultuurNet\UDB3\Language[]
43
     */
44
    public function determineCompletedLanguages(JsonDocument $jsonDocument)
45
    {
46
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
47
        return parent::determineCompletedLanguages($jsonDocument);
48
    }
49
50
    /**
51
     * @todo Remove when full replay is done.
52
     * @replay_i18n
53
     * @see https://jira.uitdatabank.be/browse/III-2201
54
     *
55
     * @param JsonDocument $jsonDocument
56
     * @return JsonDocument
57
     */
58
    private function polyFillMultilingualFields(JsonDocument $jsonDocument)
59
    {
60
        $body = $jsonDocument->getBody();
61
        $mainLanguage = isset($body->mainLanguage) ? $body->mainLanguage : 'nl';
62
63
        if (isset($body->address->streetAddress)) {
64
            $body->address = (object) [
65
                $mainLanguage => $body->address,
66
            ];
67
        }
68
69 View Code Duplication
        if (isset($body->bookingInfo->urlLabel) && is_string($body->bookingInfo->urlLabel)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
70
            $body->bookingInfo->urlLabel = (object) [
71
                $mainLanguage => $body->bookingInfo->urlLabel,
72
            ];
73
        }
74
75
        return $jsonDocument->withBody($body);
76
    }
77
}
78