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

determineAvailableLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use CultuurNet\UDB3\ReadModel\ConfigurableJsonDocumentLanguageAnalyzer;
6
use CultuurNet\UDB3\ReadModel\JsonDocument;
7
8
class EventJsonDocumentLanguageAnalyzer extends ConfigurableJsonDocumentLanguageAnalyzer
9
{
10
    public function __construct()
11
    {
12
        parent::__construct(
13
            [
14
                'name',
15
                'description',
16
                'bookingInfo.urlLabel',
17
                'priceInfo.[].name',
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 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...
64
            $body->bookingInfo->urlLabel = (object) [
65
                $mainLanguage => $body->bookingInfo->urlLabel,
66
            ];
67
        }
68
69 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...
70
            foreach ($body->priceInfo as $priceInfo) {
71
                $priceInfo->name = (object) [
72
                    $mainLanguage => $priceInfo->name,
73
                ];
74
            }
75
        }
76
77
        return $jsonDocument->withBody($body);
78
    }
79
}
80