Skip to content

Commit

Permalink
Next-31 - Implement collector pattern for view cart transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
htkassner authored and OliverSkroblin committed Apr 19, 2018
1 parent 071ca5e commit e604dd8
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/CartBridge/View/ViewCartTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use Shopware\Cart\Cart\Struct\CalculatedCart;
use Shopware\Context\Struct\ShopContext;
use Shopware\Framework\Struct\StructCollection;

class ViewCartTransformer
{
Expand All @@ -44,8 +45,10 @@ public function transform(CalculatedCart $calculatedCart, ShopContext $context):
{
$viewCart = new ViewCart($calculatedCart);

$dataCollection = $this->prepare($calculatedCart, $context);

foreach ($this->transformers as $transformer) {
$transformer->transform($calculatedCart, $viewCart, $context);
$transformer->transform($calculatedCart, $viewCart, $context, $dataCollection);
}

$viewCart->getViewLineItems()->sortByIdentifiers(
Expand All @@ -72,4 +75,19 @@ public function transform(CalculatedCart $calculatedCart, ShopContext $context):

return $viewCart;
}

private function prepare(CalculatedCart $calculatedCart, ShopContext $context): StructCollection
{
$fetchDefinitions = new StructCollection();
foreach ($this->transformers as $transformer) {
$transformer->prepare($fetchDefinitions, $calculatedCart, $context);
}

$dataCollection = new StructCollection();
foreach ($this->transformers as $transformer) {
$transformer->fetch($dataCollection, $fetchDefinitions, $context);
}

return $dataCollection;
}
}
16 changes: 15 additions & 1 deletion src/CartBridge/View/ViewLineItemTransformerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@

use Shopware\Cart\Cart\Struct\CalculatedCart;
use Shopware\Context\Struct\ShopContext;
use Shopware\Framework\Struct\StructCollection;

interface ViewLineItemTransformerInterface
{
public function prepare(
StructCollection $fetchDefinitions,
CalculatedCart $calculatedCart,
ShopContext $context
): void;

public function fetch(
StructCollection $dataCollection,
StructCollection $fetchDefinitions,
ShopContext $context
): void;

public function transform(
CalculatedCart $calculatedCart,
ViewCart $templateCart,
ShopContext $context
ShopContext $context,
StructCollection $dataCollection
): void;
}
61 changes: 54 additions & 7 deletions src/CartBridge/View/ViewProductTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,19 @@
use Shopware\Cart\Cart\Struct\CalculatedCart;
use Shopware\Cart\LineItem\CalculatedLineItemCollection;
use Shopware\CartBridge\Product\Struct\CalculatedProduct;
use Shopware\CartBridge\Product\Struct\ProductFetchDefinition;
use Shopware\Context\Struct\ShopContext;
use Shopware\Product\Repository\ProductMediaRepository;
use Shopware\Framework\Struct\StructCollection;
use Shopware\Product\Repository\ProductRepository;
use Shopware\Product\Struct\ProductBasicCollection;
use Shopware\Product\Struct\ProductMediaBasicStruct;
use Shopware\Product\Struct\ProductMediaSearchResult;
use Shopware\Product\Repository\ProductMediaRepository;

class ViewProductTransformer implements ViewLineItemTransformerInterface
{
const COLLECTION_KEY = 'products';

/**
* @var ProductRepository
*/
Expand All @@ -57,12 +62,9 @@ public function __construct(
$this->productMediaRepository = $productMediaRepository;
}

/**
* {@inheritdoc}
*/
public function transform(
public function prepare(
StructCollection $fetchDefinitions,
CalculatedCart $calculatedCart,
ViewCart $templateCart,
ShopContext $context
): void {
$collection = $calculatedCart->getCalculatedLineItems()->filterInstance(CalculatedProduct::class);
Expand All @@ -71,18 +73,63 @@ public function transform(
return;
}

$fetchDefinitions->add(new ProductFetchDefinition($collection->getIdentifiers()));
}

public function fetch(
StructCollection $dataCollection,
StructCollection $fetchDefinitons,
ShopContext $context
): void {
$definitions = $fetchDefinitons->filterInstance(ProductFetchDefinition::class);
if ($definitions->count() === 0) {
return;
}

$numbers = [];
/** @var ProductFetchDefinition[] $definitions */
foreach ($definitions as $definition) {
$numbers = array_merge($numbers, $definition->getNumbers());
}

$numbers = array_keys(array_flip($numbers));

$products = $this->productRepository->readBasic(
$collection->getIdentifiers(),
$numbers,
$context->getTranslationContext()
);

$dataCollection->add($products, self::COLLECTION_KEY);
}

/**
* {@inheritdoc}
*/
public function transform(
CalculatedCart $calculatedCart,
ViewCart $templateCart,
ShopContext $context,
StructCollection $dataCollection
): void {
$collection = $calculatedCart->getCalculatedLineItems()->filterInstance(CalculatedProduct::class);
/** @var ProductBasicCollection $products */
$products = $dataCollection->get(self::COLLECTION_KEY);

if ($collection->count() === 0 || !$products || $products->count() === 0) {
return;
}

$covers = $this->fetchCovers($products->getUuids(), $context);

/** @var CalculatedLineItemCollection $collection */
/** @var CalculatedProduct $calculated */
foreach ($collection as $calculated) {
$product = $products->get($calculated->getIdentifier());

if (!$product) {
continue;
}

/** @var ProductMediaBasicStruct $cover */
$cover = $covers->filterByProductUuid($product->getUuid())->first();

Expand Down

0 comments on commit e604dd8

Please sign in to comment.