Completed
Pull Request — master (#364)
by Luc
09:08
created

polyFillMultilingualFields()   D

Complexity

Conditions 9
Paths 16

Size

Total Lines 27
Code Lines 14

Duplication

Lines 12
Ratio 44.44 %

Importance

Changes 0
Metric Value
dl 12
loc 27
rs 4.909
c 0
b 0
f 0
cc 9
eloc 14
nc 16
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
                'priceInfo.[].name',
19
            ]
20
        );
21
    }
22
23
    /**
24
     * @todo Remove when full replay is done.
25
     * @replay_i18n
26
     * @see https://jira.uitdatabank.be/browse/III-2201
27
     *
28
     * @param JsonDocument $jsonDocument
29
     * @return \CultuurNet\UDB3\Language[]
30
     */
31
    public function determineAvailableLanguages(JsonDocument $jsonDocument)
32
    {
33
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
34
        return parent::determineAvailableLanguages($jsonDocument);
35
    }
36
37
    /**
38
     * @todo Remove when full replay is done.
39
     * @replay_i18n
40
     * @see https://jira.uitdatabank.be/browse/III-2201
41
     *
42
     * @param JsonDocument $jsonDocument
43
     * @return \CultuurNet\UDB3\Language[]
44
     */
45
    public function determineCompletedLanguages(JsonDocument $jsonDocument)
46
    {
47
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
48
        return parent::determineCompletedLanguages($jsonDocument);
49
    }
50
51
    /**
52
     * @todo Remove when full replay is done.
53
     * @replay_i18n
54
     * @see https://jira.uitdatabank.be/browse/III-2201
55
     *
56
     * @param JsonDocument $jsonDocument
57
     * @return JsonDocument
58
     */
59
    private function polyFillMultilingualFields(JsonDocument $jsonDocument)
60
    {
61
        $body = $jsonDocument->getBody();
62
        $mainLanguage = isset($body->mainLanguage) ? $body->mainLanguage : 'nl';
63
64
        if (isset($body->address->streetAddress)) {
65
            $body->address = (object) [
66
                $mainLanguage => $body->address,
67
            ];
68
        }
69
70 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...
71
            $body->bookingInfo->urlLabel = (object) [
72
                $mainLanguage => $body->bookingInfo->urlLabel,
73
            ];
74
        }
75
76 View Code Duplication
        if (isset($body->priceInfo) && is_array($body->priceInfo) && is_string($body->priceInfo[0]->name)) {
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...
77
            foreach ($body->priceInfo as $priceInfo) {
78
                $priceInfo->name = (object) [
79
                    $mainLanguage => $priceInfo->name,
80
                ];
81
            }
82
        }
83
84
        return $jsonDocument->withBody($body);
85
    }
86
}
87