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

PlaceJsonDocumentLanguageAnalyzer   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 79
Duplicated Lines 15.19 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 2
dl 12
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A determineAvailableLanguages() 0 5 1
A determineCompletedLanguages() 0 5 1
D polyFillMultilingualFields() 12 27 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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