Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

[FEATURE] Added addon prices to products api #335

Merged
merged 3 commits into from
Nov 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions Api/AddonPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ProductBundle\Api;

use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty;
use Sulu\Bundle\ProductBundle\Entity\AddonPrice as AddonPriceEntity;
use Sulu\Bundle\ProductBundle\Util\PriceFormatter;
use Sulu\Component\Rest\ApiWrapper;

/**
* @ExclusionPolicy("all")
*/
class AddonPrice extends ApiWrapper
{
/**
* @var PriceFormatter
*/
private $priceFormatter;

/**
* @param AddonPriceEntity $entity
* @param string $locale
* @param PriceFormatter $priceFormatter
*/
public function __construct(AddonPriceEntity $entity, $locale, PriceFormatter $priceFormatter)
{
$this->entity = $entity;
$this->locale = $locale;
$this->priceFormatter = $priceFormatter;
}

/**
* @VirtualProperty
* @SerializedName("id")
*
* @return int
*/
public function getId()
{
return $this->entity->getId();
}

/**
* @VirtualProperty
* @SerializedName("currency")
*
* @return string
*/
public function getCurrency()
{
return new Currency($this->entity->getCurrency(), $this->locale);
}

/**
* @VirtualProperty
* @SerializedName("price")
*
* @return float
*/
public function getPrice()
{
return $this->entity->getPrice();
}

/**
* @VirtualProperty
* @SerializedName("priceFormatted")
*
* @return string
*/
public function getPriceFormatted()
{
return $this->priceFormatter->format($this->entity->getPrice(), null, $this->locale);
}
}
93 changes: 93 additions & 0 deletions Api/AddonProduct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ProductBundle\Api;

use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\VirtualProperty;
use Sulu\Bundle\ContactBundle\Contact\AccountManager;
use Sulu\Bundle\ProductBundle\Entity\AddonPrice as AddonPriceEntity;
use Sulu\Bundle\ProductBundle\Entity\ProductInterface;
use Sulu\Bundle\ProductBundle\Product\ProductFactory;
use Sulu\Bundle\ProductBundle\Product\ProductLocaleManager;
use Sulu\Bundle\ProductBundle\Util\PriceFormatter;

/**
* @ExclusionPolicy("all")
*/
class AddonProduct extends Product implements ApiAddonProductInterface
{
/**
* @var AddonPriceEntity[]
*/
private $addonPrices;

/**
* @param ProductInterface $product The product to wrap
* @param string $locale The locale of this product
* @param PriceFormatter $priceFormatter
* @param ProductLocaleManager $productLocaleManager
* @param ProductFactory $productFactory
* @param AccountManager|null $accountManager
* @param array $addonPrices
*/
public function __construct(
ProductInterface $product,
$locale,
PriceFormatter $priceFormatter,
ProductLocaleManager $productLocaleManager,
ProductFactory $productFactory,
AccountManager $accountManager = null,
array $addonPrices = []
) {
parent::__construct(
$product,
$locale,
$priceFormatter,
$productLocaleManager,
$productFactory,
$accountManager
);

$this->setAddonPrices($addonPrices);
}

/**
* @param array $addonPrices
*
* @return $this
*/
public function setAddonPrices(array $addonPrices)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this to the constructor and remove this function.

{
$this->addonPrices = $addonPrices;

return $this;
}

/**
* @VirtualProperty
* @SerializedName("addonPrices")
*
* {@inheritdoc}
*/
public function getAddonPrices()
{
$addonPrices = [];
if ($this->addonPrices) {
foreach ($this->addonPrices as $addonPrice) {
$addonPrices[] = new AddonPrice($addonPrice, $this->locale, $this->priceFormatter);
}
}

return $addonPrices;
}
}
23 changes: 23 additions & 0 deletions Api/ApiAddonProductInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ProductBundle\Api;

/**
* Interface for Addon-Product Api Objects.
*/
interface ApiAddonProductInterface
{
/**
* @return AddonPrice[]
*/
public function getAddonPrices();
}
28 changes: 16 additions & 12 deletions Api/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Sulu\Bundle\ProductBundle\Entity\TaxClass as TaxClassEntity;
use Sulu\Bundle\ProductBundle\Entity\Type as TypeEntity;
use Sulu\Bundle\ProductBundle\Entity\Unit as UnitEntity;
use Sulu\Bundle\ProductBundle\Product\ProductFactory;
use Sulu\Bundle\ProductBundle\Product\ProductLocaleManager;
use Sulu\Bundle\ProductBundle\Util\PriceFormatter;
use Sulu\Component\Rest\ApiWrapper;
Expand Down Expand Up @@ -67,23 +68,31 @@ class Product extends ApiWrapper implements ApiProductInterface
*/
protected $productLocaleManager;

/**
* @var ProductFactory
*/
protected $productFactory;

/**
* @param Entity $product The product to wrap
* @param string $locale The locale of this product
* @param PriceFormatter $priceFormatter
* @param ProductLocaleManager $productLocaleManager
* @param ProductFactory $productFactory
* @param AccountManager|null $accountManager
*/
public function __construct(
Entity $product,
$locale,
PriceFormatter $priceFormatter,
ProductLocaleManager $productLocaleManager,
ProductFactory $productFactory,
AccountManager $accountManager = null
) {
$this->entity = $product;
$this->priceFormatter = $priceFormatter;
$this->productLocaleManager = $productLocaleManager;
$this->productFactory = $productFactory;
$this->accountManager = $accountManager;

$this->locale = $locale;
Expand Down Expand Up @@ -487,17 +496,11 @@ public function getParent()
{
$parent = $this->entity->getParent();

if ($parent) {
return new static(
$parent,
$this->locale,
$this->priceFormatter,
$this->productLocaleManager,
$this->accountManager
);
if (!$parent) {
return null;
}

return null;
return $this->productFactory->createApiEntity($parent, $this->locale);
}

/**
Expand Down Expand Up @@ -1329,12 +1332,13 @@ public function getAddons()
$addons = $this->entity->getAddons();
/** @var AddonEntity $addon */
foreach ($addons as $addon) {
$apiAddons[] = new static(
$apiAddon = $this->productFactory->createAddonProductApiEntity(
$addon->getAddon(),
$this->locale,
$this->priceFormatter,
$this->productLocaleManager
iterator_to_array($addon->getAddonPrices())
);

$apiAddons[] = $apiAddon;
}

return $apiAddons;
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
CHANGELOG for Sulu Product Bundle
=================================

* dev-develop
* BUGFIX Resolved wrong locale in xliff file.
* FEATURE Added property `addonPrices` to addons in products api.

* 0.16.1 (2016-10-27)

* BUGFIX Fixed deleting of variant attributes.
Expand Down
8 changes: 4 additions & 4 deletions Entity/AddonPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getId()
/**
* @param int $id
*
* @return self
* @return $this
*/
public function setId($id)
{
Expand All @@ -68,7 +68,7 @@ public function getCurrency()
/**
* @param Currency $currency
*
* @return self
* @return $this
*/
public function setCurrency(Currency $currency)
{
Expand All @@ -88,7 +88,7 @@ public function getPrice()
/**
* @param float $price
*
* @return self
* @return $this
*/
public function setPrice($price)
{
Expand All @@ -108,7 +108,7 @@ public function getAddon()
/**
* @param Addon $addon
*
* @return self
* @return $this
*/
public function setAddon(Addon $addon)
{
Expand Down
18 changes: 18 additions & 0 deletions Product/ProductFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\ProductBundle\Product;

use Sulu\Bundle\ContactBundle\Contact\AccountManager;
use Sulu\Bundle\ProductBundle\Api\AddonProduct as ApiAddonProduct;
use Sulu\Bundle\ProductBundle\Api\Product as ApiProduct;
use Sulu\Bundle\ProductBundle\Entity\Product;
use Sulu\Bundle\ProductBundle\Entity\ProductInterface;
Expand Down Expand Up @@ -67,7 +68,24 @@ public function createApiEntity(ProductInterface $product, $locale)
$locale,
$this->priceFormatter,
$this->productLocaleManager,
$this,
$this->accountManager
);
}

/**
* {@inheritdoc}
*/
public function createAddonProductApiEntity(ProductInterface $product, $locale, array $addonPrices = [])
{
return new ApiAddonProduct(
$product,
$locale,
$this->priceFormatter,
$this->productLocaleManager,
$this,
$this->accountManager,
$addonPrices
);
}
}
9 changes: 9 additions & 0 deletions Product/ProductFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\ProductBundle\Product;

use Sulu\Bundle\ProductBundle\Api\ApiAddonProductInterface;
use Sulu\Bundle\ProductBundle\Api\ApiProductInterface;
use Sulu\Bundle\ProductBundle\Entity\ProductInterface;

Expand All @@ -32,4 +33,12 @@ public function createEntity();
* @return ApiProductInterface
*/
public function createApiEntity(ProductInterface $product, $locale);

/**
* @param ProductInterface $product
* @param string $locale
*
* @return ApiAddonProductInterface
*/
public function createAddonProductApiEntity(ProductInterface $product, $locale);
}
Loading