<?php declare(strict_types=1);
/**
* @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
*
* @author Jakub MedyĆski <jme@crehler.com>
* @support Crehler <support@crehler.com>
* @created 10 sie 2020
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Crehler\Inspirations\Service;
use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductCollection;
use Crehler\Inspirations\Entity\Inspiration\Aggregate\InspirationProduct\InspirationProductEntity;
use Crehler\Inspirations\Entity\Inspiration\InspirationEntity;
use Crehler\Tools\Enum\LoaderFlags;
use Crehler\Tools\Product\ProductLoaderInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
class InspirationService
{
private EntityRepository $inspirationRepository;
private ProductLoaderInterface $productLoader;
public function __construct(
EntityRepository $inspirationRepository,
ProductLoaderInterface $productLoader
)
{
$this->productLoader = $productLoader;
$this->inspirationRepository = $inspirationRepository;
}
public function getInspirationById(string $inspirationId, Context $context): ?InspirationEntity
{
$criteria = new Criteria([$inspirationId]);
$criteria->addAssociation('media')
->addAssociation('cover')
->addAssociation('inspirationProducts');
$criteria->getAssociation('inspirationProducts')->addSorting(new FieldSorting('createdAt','ASC'));
$criteria->getAssociation('inspirationProducts')->addAssociation('product.options.group');
$criteria->getAssociation('media')->addSorting(new FieldSorting('position'));
$criteria->getAssociation('inspirationProducts.product')->addAssociation('cover')->addAssociation('media');
/** @var InspirationEntity $inspiration */
$inspiration = $this->inspirationRepository->search($criteria, $context)->first();
/** @var InspirationProductEntity $inspirationProduct */
foreach ($inspiration->getInspirationProducts() as $inspirationProduct){
$inspirationProduct->setProduct($this->productLoader->load(
$inspirationProduct->getProductId(),
null,
LoaderFlags::SkipPageLoading,
LoaderFlags::SkipSeoCategory,
LoaderFlags::IgnoreHideCloseoutProductsWhenOutOfStock)
->first()); ;
}
return $inspiration;
}
}