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

EventJsonDocumentLanguageAnalyzer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 7.94 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 5
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A determineAvailableLanguages() 0 5 1
A determineCompletedLanguages() 0 5 1
A polyFillMultilingualFields() 5 13 4

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\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
            ]
18
        );
19
    }
20
21
    /**
22
     * @todo Remove when full replay is done.
23
     * @replay_i18n
24
     * @see https://jira.uitdatabank.be/browse/III-2201
25
     *
26
     * @param JsonDocument $jsonDocument
27
     * @return \CultuurNet\UDB3\Language[]
28
     */
29
    public function determineAvailableLanguages(JsonDocument $jsonDocument)
30
    {
31
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
32
        return parent::determineAvailableLanguages($jsonDocument);
33
    }
34
35
    /**
36
     * @todo Remove when full replay is done.
37
     * @replay_i18n
38
     * @see https://jira.uitdatabank.be/browse/III-2201
39
     *
40
     * @param JsonDocument $jsonDocument
41
     * @return \CultuurNet\UDB3\Language[]
42
     */
43
    public function determineCompletedLanguages(JsonDocument $jsonDocument)
44
    {
45
        $jsonDocument = $this->polyFillMultilingualFields($jsonDocument);
46
        return parent::determineCompletedLanguages($jsonDocument);
47
    }
48
49
    /**
50
     * @todo Remove when full replay is done.
51
     * @replay_i18n
52
     * @see https://jira.uitdatabank.be/browse/III-2201
53
     *
54
     * @param JsonDocument $jsonDocument
55
     * @return JsonDocument
56
     */
57
    private function polyFillMultilingualFields(JsonDocument $jsonDocument)
58
    {
59
        $body = $jsonDocument->getBody();
60
        $mainLanguage = isset($body->mainLanguage) ? $body->mainLanguage : 'nl';
61
62 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...
63
            $body->bookingInfo->urlLabel = (object) [
64
                $mainLanguage => $body->bookingInfo->urlLabel,
65
            ];
66
        }
67
68
        return $jsonDocument->withBody($body);
69
    }
70
}
71