Completed
Pull Request — master (#359)
by
unknown
09:13
created

CdbXMLItemBaseImporter::importWorkflowStatus()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Offer\ReadModel\JSONLD;
4
5
use CultureFeed_Cdb_Item_Base;
6
use CultuurNet\UDB3\Cdb\DateTimeFactory;
7
use CultuurNet\UDB3\Cdb\PriceDescriptionParser;
8
use CultuurNet\UDB3\Language;
9
use CultuurNet\UDB3\Offer\WorkflowStatus;
10
use CultuurNet\UDB3\PriceInfo\BasePrice;
11
use CultuurNet\UDB3\PriceInfo\Price;
12
use CultuurNet\UDB3\PriceInfo\Tariff;
13
use CultuurNet\UDB3\ReadModel\MultilingualJsonLDProjectorTrait;
14
use CultuurNet\UDB3\ValueObject\MultilingualString;
15
use ValueObjects\Money\Currency;
16
use ValueObjects\StringLiteral\StringLiteral;
17
18
class CdbXMLItemBaseImporter
19
{
20
    use MultilingualJsonLDProjectorTrait;
21
22
    /**
23
     * @var PriceDescriptionParser
24
     */
25
    private $priceDescriptionParser;
26
27
    /**
28
     * @param PriceDescriptionParser $priceDescriptionParser
29
     */
30
    public function __construct(PriceDescriptionParser $priceDescriptionParser)
31
    {
32
        $this->priceDescriptionParser = $priceDescriptionParser;
33
    }
34
35
    /**
36
     * @param CultureFeed_Cdb_Item_Base $item
37
     * @param \stdClass $jsonLD
38
     */
39
    public function importPublicationInfo(
40
        CultureFeed_Cdb_Item_Base $item,
41
        \stdClass $jsonLD
42
    ) {
43
        $jsonLD->creator = $item->getCreatedBy();
44
45
        $itemCreationDate = $item->getCreationDate();
46
47
        if (!empty($itemCreationDate)) {
48
            // format using ISO-8601 with time zone designator
49
            $creationDate = DateTimeFactory::dateTimeFromDateString(
50
                $itemCreationDate
51
            );
52
53
            $jsonLD->created = $creationDate->format('c');
54
        }
55
56
        $itemLastUpdatedDate = $item->getLastUpdated();
57
58
        if (!empty($itemLastUpdatedDate)) {
59
            $lastUpdatedDate = DateTimeFactory::dateTimeFromDateString(
60
                $itemLastUpdatedDate
61
            );
62
63
            $jsonLD->modified = $lastUpdatedDate->format('c');
64
        }
65
66
        $jsonLD->publisher = $item->getOwner();
67
    }
68
69
    /**
70
     * @param CultureFeed_Cdb_Item_Base $item
71
     * @param \stdClass $jsonLD
72
     */
73
    public function importAvailable(
74
        \CultureFeed_Cdb_Item_Base $item,
75
        \stdClass $jsonLD
76
    ) {
77
        $availableFromString = $item->getAvailableFrom();
78
        if ($availableFromString) {
79
            $jsonLD->availableFrom = $this->formatAvailableString(
80
                $availableFromString
81
            );
82
        }
83
84
        $availableToString = $item->getAvailableTo();
85
        if ($availableToString) {
86
            $jsonLD->availableTo = $this->formatAvailableString(
87
                $availableToString
88
            );
89
        }
90
    }
91
92
    /**
93
     * @param string $availableString
94
     * @return string
95
     */
96
    private function formatAvailableString($availableString)
97
    {
98
        $available = DateTimeFactory::dateTimeFromDateString(
99
            $availableString
100
        );
101
102
        return $available->format('c');
103
    }
104
105
    /**
106
     * @param CultureFeed_Cdb_Item_Base $item
107
     * @param \stdClass $jsonLD
108
     */
109
    public function importExternalId(
110
        \CultureFeed_Cdb_Item_Base $item,
111
        \stdClass $jsonLD
112
    ) {
113
        $externalId = $item->getExternalId();
114
        if (empty($externalId)) {
115
            return;
116
        }
117
118
        $externalIdIsCDB = (strpos($externalId, 'CDB:') === 0);
119
120
        if (!property_exists($jsonLD, 'sameAs')) {
121
            $jsonLD->sameAs = [];
122
        }
123
124
        if (!$externalIdIsCDB) {
125
            if (!in_array($externalId, $jsonLD->sameAs)) {
126
                array_push($jsonLD->sameAs, $externalId);
127
            }
128
        }
129
    }
130
131
    /**
132
     * @param CultureFeed_Cdb_Item_Base $item
133
     * @param \stdClass $jsonLD
134
     */
135
    public function importWorkflowStatus(
136
        CultureFeed_Cdb_Item_Base $item,
137
        \stdClass $jsonLD
138
    ) {
139
        $wfStatus = $item->getWfStatus();
140
141
        $workflowStatus = $wfStatus ? WorkflowStatus::fromNative($wfStatus) : WorkflowStatus::READY_FOR_VALIDATION();
142
143
        $jsonLD->workflowStatus = $workflowStatus->getName();
144
    }
145
146
    /**
147
     * @param \CultureFeed_Cdb_Data_DetailList $details
148
     * @param \stdClass $jsonLD
149
     */
150
    public function importPriceInfo(
151
        \CultureFeed_Cdb_Data_DetailList $details,
152
        $jsonLD
153
    ) {
154
        $mainLanguage = $this->getMainLanguage($jsonLD);
155
156
        $detailsArray = [];
157
        foreach ($details as $detail) {
158
            $detailsArray[] = $detail;
159
        }
160
        $details = $detailsArray;
161
162
        $mainLanguageDetails = array_filter(
163
            $details,
164
            function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) {
165
                return $detail->getLanguage() === $mainLanguage->getCode();
166
            }
167
        );
168
169
        /* @var \CultureFeed_Cdb_Data_EventDetail $mainLanguageDetail */
170
        $mainLanguageDetail = reset($mainLanguageDetails);
171
        if (!$mainLanguageDetail) {
172
            return;
173
        }
174
175
        $mainLanguagePrice = $mainLanguageDetail->getPrice();
176
177
        if (!$mainLanguagePrice) {
178
            return;
179
        }
180
181
        $basePrice = $mainLanguagePrice->getValue();
182
        if ($basePrice !== null) {
183
            $basePrice = floatval($basePrice);
184
        }
185
186
        if (!$basePrice) {
187
            return;
188
        }
189
190
        $basePrice = new BasePrice(
191
            Price::fromFloat($basePrice),
192
            Currency::fromNative('EUR')
193
        );
194
195
        /* @var Tariff[] $tariffs */
196
        $tariffs = [];
197
        foreach ($details as $detail) {
198
            $language = null;
0 ignored issues
show
Unused Code introduced by
$language is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
199
            $price = null;
0 ignored issues
show
Unused Code introduced by
$price is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
200
            $description = null;
0 ignored issues
show
Unused Code introduced by
$description is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
201
202
            $language = $detail->getLanguage();
203
204
            $price = $detail->getPrice();
205
            if (!$price) {
206
                continue;
207
            }
208
209
            $description = $price->getDescription();
210
            if (!$description) {
211
                continue;
212
            }
213
214
            $translatedTariffs = $this->priceDescriptionParser->parse($description);
215
            if (empty($translatedTariffs)) {
216
                continue;
217
            }
218
219
            // Skip the base price.
220
            array_shift($translatedTariffs);
221
222
            $tariffIndex = 0;
223
            foreach ($translatedTariffs as $tariffName => $tariffPrice) {
224
                if (!isset($tariffs[$tariffIndex])) {
225
                    $tariff = new Tariff(
226
                        new MultilingualString(new Language($language), new StringLiteral($tariffName)),
227
                        Price::fromFloat($tariffPrice),
228
                        Currency::fromNative('EUR')
229
                    );
230
                } else {
231
                    $tariff = $tariffs[$tariffIndex];
232
                    $name = $tariff->getName();
233
                    $name = $name->withTranslation(new Language($language), new StringLiteral($tariffName));
234
                    $tariff = new Tariff(
235
                        $name,
236
                        $tariff->getPrice(),
237
                        $tariff->getCurrency()
238
                    );
239
                }
240
241
                $tariffs[$tariffIndex] = $tariff;
242
                $tariffIndex++;
243
            }
244
        }
245
246
        $jsonLD->priceInfo = [
247
            [
248
                'category' => 'base',
249
                'name' => [
250
                    'nl' => 'Basistarief',
251
                ],
252
                'price' => $basePrice->getPrice()->toFloat(),
253
                'priceCurrency' => $basePrice->getCurrency()->getCode()->toNative(),
254
            ]
255
        ];
256
257 View Code Duplication
        foreach ($tariffs as $tariff) {
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...
258
            $jsonLD->priceInfo[] = [
259
                'category' => 'tariff',
260
                'name' => $tariff->getName()->serialize(),
261
                'price' => $tariff->getPrice()->toFloat(),
262
                'priceCurrency' => $tariff->getCurrency()->getCode()->toNative(),
263
            ];
264
        }
265
    }
266
}
267