Skip to content

Commit

Permalink
Merge pull request #1897 from cultuurnet/III-6310-remove-legacy-base-…
Browse files Browse the repository at this point in the history
…price

III-6310 Remove legacy `BasePrice`
  • Loading branch information
LucWollants authored Nov 25, 2024
2 parents ee0388d + fd972d1 commit 718b990
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 115 deletions.
3 changes: 1 addition & 2 deletions src/Cdb/CdbXmlPriceInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use CultuurNet\UDB3\Model\ValueObject\Price\TranslatedTariffName;
use CultuurNet\UDB3\Model\ValueObject\Translation\Language;
use CultuurNet\UDB3\MoneyFactory;
use CultuurNet\UDB3\PriceInfo\BasePrice;
use CultuurNet\UDB3\PriceInfo\PriceInfo;
use Money\Currency;

Expand Down Expand Up @@ -63,7 +62,7 @@ function (\CultureFeed_Cdb_Data_Detail $detail) use ($mainLanguage) {
return null;
}

$basePrice = new BasePrice(
$basePrice = Tariff::createBasePrice(
MoneyFactory::create($basePrice, new Currency('EUR'))
);

Expand Down
13 changes: 13 additions & 0 deletions src/Model/Serializer/ValueObject/Price/TariffDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@

final class TariffDenormalizer implements DenormalizerInterface
{
private bool $forBasePrice;

public function __construct(bool $forBasePrice)
{
$this->forBasePrice = $forBasePrice;
}

public function denormalize($data, $class, $format = null, array $context = []): Tariff
{
if ($this->forBasePrice) {
return Tariff::createBasePrice(
MoneyFactory::createFromCents($data['price'], new Currency($data['currency']))
);
}

/** @var TranslatedTariffName $tariffName */
$tariffName = (new TranslatedTariffNameDenormalizer())->denormalize(
$data['name'],
Expand Down
16 changes: 14 additions & 2 deletions src/Model/Serializer/ValueObject/Price/TariffNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,28 @@

final class TariffNormalizer implements NormalizerInterface
{
private bool $forBasePrice;

public function __construct(bool $forBasePrice)
{
$this->forBasePrice = $forBasePrice;
}

/**
* @param Tariff $tariff
*/
public function normalize($tariff, $format = null, array $context = []): array
{
return [
'name' => (new TranslatedTariffNameNormalizer())->normalize($tariff->getName()),
$data = [
'price' => $tariff->getPrice()->getAmount(),
'currency' => $tariff->getPrice()->getCurrency()->getName(),
];

if (!$this->forBasePrice) {
$data['name'] = (new TranslatedTariffNameNormalizer())->normalize($tariff->getName());
}

return $data;
}

public function supportsNormalization($data, $format = null): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Offer/ReadModel/JSONLD/CdbXMLItemBaseImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function importPriceInfo(
'category' => 'base',
'name' => $this->basePriceTranslations,
'price' => $priceInfo->getBasePrice()->getPrice()->getAmount() / 100,
'priceCurrency' => $priceInfo->getBasePrice()->getCurrency()->getName(),
'priceCurrency' => $priceInfo->getBasePrice()->getPrice()->getCurrency()->getName(),
],
];

Expand Down
2 changes: 1 addition & 1 deletion src/Offer/ReadModel/JSONLD/OfferLDProjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ protected function applyPriceInfoUpdated(AbstractPriceInfoUpdated $priceInfoUpda
'category' => 'base',
'name' => $this->basePriceTranslations,
'price' => $basePrice->getPrice()->getAmount() / 100,
'priceCurrency' => $basePrice->getCurrency()->getName(),
'priceCurrency' => $basePrice->getPrice()->getCurrency()->getName(),
];

$translatedTariffNameNormalizer = new TranslatedTariffNameNormalizer();
Expand Down
58 changes: 0 additions & 58 deletions src/PriceInfo/BasePrice.php

This file was deleted.

21 changes: 10 additions & 11 deletions src/PriceInfo/PriceInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
class PriceInfo implements Serializable
{
private BasePrice $basePrice;
private Tariff $basePrice;

/**
* @var Tariff[]
Expand All @@ -28,7 +28,7 @@ class PriceInfo implements Serializable
*/
private array $uitpasTariffs;

public function __construct(BasePrice $basePrice)
public function __construct(Tariff $basePrice)
{
$this->basePrice = $basePrice;
$this->tariffs = [];
Expand Down Expand Up @@ -63,7 +63,7 @@ public function withUiTPASTariffs(array $tariffs): PriceInfo
return $c;
}

public function getBasePrice(): BasePrice
public function getBasePrice(): Tariff
{
return $this->basePrice;
}
Expand All @@ -84,12 +84,12 @@ public function getUiTPASTariffs(): array
public function serialize(): array
{
$serialized = [
'base' => $this->basePrice->serialize(),
'base' => (new TariffNormalizer(true))->normalize($this->basePrice),
'tariffs' => [],
'uitpas_tariffs' => [],
];

$tariffNormalizer = new TariffNormalizer();
$tariffNormalizer = new TariffNormalizer(false);

foreach ($this->tariffs as $tariff) {
$serialized['tariffs'][] = $tariffNormalizer->normalize($tariff);
Expand All @@ -104,11 +104,11 @@ public function serialize(): array

public static function deserialize(array $data): PriceInfo
{
$basePriceInfo = BasePrice::deserialize($data['base']);
$priceInfo = new PriceInfo(
(new TariffDenormalizer(true))->denormalize($data['base'], Tariff::class)
);

$priceInfo = new PriceInfo($basePriceInfo);

$tariffDenormalizer = new TariffDenormalizer();
$tariffDenormalizer = new TariffDenormalizer(false);

foreach ($data['tariffs'] as $tariffData) {
$priceInfo = $priceInfo->withExtraTariff(
Expand All @@ -129,8 +129,7 @@ public static function deserialize(array $data): PriceInfo

public static function fromUdb3ModelPriceInfo(Udb3ModelPriceInfo $udb3ModelPriceInfo): PriceInfo
{
$basePrice = BasePrice::fromUdb3ModelTariff($udb3ModelPriceInfo->getBasePrice());
$priceInfo = new PriceInfo($basePrice);
$priceInfo = new PriceInfo($udb3ModelPriceInfo->getBasePrice());

foreach ($udb3ModelPriceInfo->getTariffs() as $udb3ModelTariff) {
$priceInfo = $priceInfo->withExtraTariff($udb3ModelTariff);
Expand Down
3 changes: 1 addition & 2 deletions tests/BackwardsCompatiblePayloadSerializerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use CultuurNet\UDB3\Place\Events\DescriptionTranslated as PlaceDescriptionTranslated;
use CultuurNet\UDB3\Place\Events\DescriptionUpdated as PlaceDescriptionUpdated;
use CultuurNet\UDB3\Place\Events\PlaceCreated;
use CultuurNet\UDB3\PriceInfo\BasePrice;
use CultuurNet\UDB3\PriceInfo\PriceInfo;
use CultuurNet\UDB3\Role\Events\ConstraintAdded;
use CultuurNet\UDB3\ValueObject\MultilingualString;
Expand Down Expand Up @@ -623,7 +622,7 @@ public function it_should_replace_string_names_with_translatable_objects_in_pric
$decoded = Json::decodeAssociatively($serialized);

$expectedPriceInfo = new PriceInfo(
new BasePrice(
Tariff::createBasePrice(
new Money(1500, new Currency('EUR'))
)
);
Expand Down
Loading

0 comments on commit 718b990

Please sign in to comment.