Skip to content

Commit

Permalink
fix: Add data export
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoya-de committed Aug 20, 2024
1 parent 0bf5616 commit 96ad255
Show file tree
Hide file tree
Showing 24 changed files with 125 additions and 101 deletions.
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
"version": "0.1.0",
"require": {
"php": ">= 8.2",
"shopware/core": "^6.4",
"shopware/core": "6.5.*",
"league/pipeline": "^1.0",
"phpdocumentor/reflection-docblock": "^5.0",
"psr/clock": "^1.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.34",
"psalm/plugin-symfony": "^5.0",
"vimeo/psalm": "^5.15"
"vimeo/psalm": "^5.15",
"rector/rector": "^1.2",
"frosh/shopware-rector": "^0.4"
},
"autoload": {
"psr-4": {
Expand All @@ -27,5 +30,10 @@
"de-DE": "Makaira Connect",
"en-GB": "Makaira Connect"
}
},
"config": {
"allow-plugins": {
"symfony/runtime": true
}
}
}
20 changes: 20 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Frosh\Rector\Set\ShopwareSetList;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_81,
SetList::EARLY_RETURN,
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::GMAGICK_TO_IMAGICK,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
ShopwareSetList::SHOPWARE_6_5_0,
]);
$rectorConfig->paths([__DIR__ . '/src']);
};
2 changes: 1 addition & 1 deletion src/Api/ApiGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function insertPersistenceRevision(array $data, string $language): void

public function insertPersistenceRevisions(array $items): void
{
if (0 === \count($items)) {
if ([] === $items) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Api/RequestSigner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(private string $sharedSecret)
{
}

public function sign($nonce, string $body): string
public function sign(string $nonce, string $body): string
{
return hash_hmac('sha256', $nonce . ':' . $body, $this->sharedSecret);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
#[Package('content')]
#[Route(defaults: ['_routeScope' => ['store-api']])]
class CachedCategoryRoute extends AbstractCategoryRoute
{
public function __construct(
Expand All @@ -34,6 +32,6 @@ public function load(string $navigationId, Request $request, SalesChannelContext
$request->query->set('slots', null);

// dont use the cache layer
return $this->getDecorated()->getDecorated()->load($navigationId, $request, $context);
return $this->getDecorated()->load($navigationId, $request, $context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Makaira\Connect\Core\Content\Product\SalesChannel\Listing;

use AllowDynamicProperties;
use Exception;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Makaira\Connect\Exception\NoDataException;
use Makaira\Connect\Service\AggregationProcessingService;
use Makaira\Connect\Service\BannerProcessingService;
Expand All @@ -27,10 +30,11 @@
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use stdClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

#[\AllowDynamicProperties]
#[AllowDynamicProperties]
class ProductListingRoute extends AbstractProductListingRoute
{
public function __construct(
Expand All @@ -45,7 +49,7 @@ public function __construct(
private readonly ShopwareProductFetchingService $shopwareProductFetchingService,
private readonly AggregationProcessingService $aggregationProcessingService,
private readonly BannerProcessingService $bannerProcessingService,
private readonly LoggerInterface $logger,
private readonly LoggerInterface $httpClientLogger,
) {
$this->decorated = $decorated;
$this->categoryRepository = $categoryRepository;
Expand Down Expand Up @@ -86,20 +90,20 @@ public function load(

$makairaResponse = $this->makairaProductFetchingService->fetchMakairaProductsFromCategory($context, $catId, $criteria, $makairaFilter, $makairaSorting);

if (null === $makairaResponse) {
if (!$makairaResponse instanceof stdClass) {
throw new NoDataException('Keine Daten oder fehlerhaft vom Makaira Server.');
}
} catch (\Exception $exception) {
$this->logger->error('[Makaira] ' . $exception->getMessage(), ['type' => __CLASS__]);
} catch (Exception $exception) {
$this->httpClientLogger->error('[Makaira] ' . $exception->getMessage(), ['type' => self::class]);

return $this->decorated->load($categoryId, $request, $context, $criteria);
}

$shopwareResult = $this->shopwareProductFetchingService->fetchProductsFromShopware($makairaResponse, $request, $criteria, $context);

$result = (new Pipeline())
->pipe(fn ($payload) => $this->aggregationProcessingService->processAggregationsFromMakairaResponse($payload, $makairaResponse))
->pipe(fn ($payload) => $this->bannerProcessingService->processBannersFromMakairaResponse($payload, $makairaResponse, $context))
->pipe(fn ($payload): EntitySearchResult => $this->aggregationProcessingService->processAggregationsFromMakairaResponse($payload, $makairaResponse))
->pipe(fn ($payload): EntitySearchResult => $this->bannerProcessingService->processBannersFromMakairaResponse($payload, $makairaResponse, $context))
->process($shopwareResult);

/** @var ProductListingResult $result */
Expand Down Expand Up @@ -141,7 +145,6 @@ private function fetchCategory(string $categoryId, SalesChannelContext $context)
$categoryCriteria = new Criteria([$categoryId]);
$categoryCriteria->setTitle('product-listing-route::category-loading');

/** @var CategoryEntity $category */
return $this->categoryRepository->search($categoryCriteria, $context->getContext())->first();
}
}
25 changes: 15 additions & 10 deletions src/Core/Content/Product/SalesChannel/Search/ProductSearchRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Makaira\Connect\Core\Content\Product\SalesChannel\Search;

use Exception;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Makaira\Connect\Exception\NoDataException;
use Makaira\Connect\Service\AggregationProcessingService;
use Makaira\Connect\Service\BannerProcessingService;
Expand All @@ -20,12 +22,15 @@
use Shopware\Core\Content\Product\SalesChannel\Search\AbstractProductSearchRoute;
use Shopware\Core\Content\Product\SalesChannel\Search\ProductSearchRouteResponse;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use stdClass;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

use function count;

class ProductSearchRoute extends AbstractProductSearchRoute
{
public function __construct(
Expand All @@ -37,7 +42,7 @@ public function __construct(
private readonly MakairaProductFetchingService $makairaProductFetchingService,
private readonly AggregationProcessingService $aggregationProcessingService,
private readonly BannerProcessingService $bannerProcessingService,
private readonly LoggerInterface $logger,
private readonly LoggerInterface $httpClientLogger,
) {
}

Expand All @@ -59,11 +64,11 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c

$makairaResponse = $this->makairaProductFetchingService->fetchProductsFromMakaira($context, $query, $criteria, $makairaSorting, $makairaFilter);

if (null === $makairaResponse) {
if (!$makairaResponse instanceof stdClass) {
throw new NoDataException('Keine Daten oder fehlerhaft vom Makaira Server.');
}
} catch (\Exception $exception) {
$this->logger->error('[Makaira] ' . $exception->getMessage(), ['type' => __CLASS__]);
} catch (Exception $exception) {
$this->httpClientLogger->error('[Makaira] ' . $exception->getMessage(), ['type' => self::class]);

return $this->decorated->load($request, $context, $criteria);
}
Expand All @@ -78,8 +83,8 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c
$shopwareResult = $this->shopwareProductFetchingService->fetchProductsFromShopware($makairaResponse, $request, $criteria, $context);

$result = (new Pipeline())
->pipe(fn ($payload) => $this->aggregationProcessingService->processAggregationsFromMakairaResponse($payload, $makairaResponse))
->pipe(fn ($payload) => $this->bannerProcessingService->processBannersFromMakairaResponse($payload, $makairaResponse, $context))
->pipe(fn ($payload): EntitySearchResult => $this->aggregationProcessingService->processAggregationsFromMakairaResponse($payload, $makairaResponse))
->pipe(fn ($payload): EntitySearchResult => $this->bannerProcessingService->processBannersFromMakairaResponse($payload, $makairaResponse, $context))
->process($shopwareResult);

$this->eventDispatcher->dispatch(new ProductSearchCriteriaEvent($request, $criteria, $context), ProductEvents::PRODUCT_SEARCH_CRITERIA);
Expand All @@ -94,15 +99,15 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c
private function validateSearchRequest(Request $request): void
{
if (!$request->get('search')) {
throw new MissingRequestParameterException('search');
throw RoutingException::missingRequestParameter('search');
}
}

private function checkForSearchRedirect($makairaResponse): ?string
private function checkForSearchRedirect(stdClass $makairaResponse): ?string
{
$redirects = isset($makairaResponse->searchredirect) ? $makairaResponse->searchredirect->items : [];

if (\count($redirects) > 0) {
if (count($redirects) > 0) {
$targetUrl = $redirects[0]->fields->targetUrl;

if ($targetUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Makaira\Connect\Core\Content\Product\SalesChannel\Suggest;

use AllowDynamicProperties;
use Makaira\Connect\Service\MakairaProductFetchingService;
use Makaira\Connect\Service\ShopwareProductFetchingService;
use Psr\Log\LoggerInterface;
Expand All @@ -18,18 +19,18 @@
use Shopware\Core\Content\Product\SalesChannel\Suggest\AbstractProductSuggestRoute;
use Shopware\Core\Content\Product\SalesChannel\Suggest\ProductSuggestRouteResponse;
use Shopware\Core\Content\Product\SearchKeyword\ProductSearchBuilderInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\RequestCriteriaBuilder;
use Shopware\Core\Framework\Feature;
use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
use Shopware\Core\Framework\Routing\RoutingException;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Throwable;

#[\AllowDynamicProperties]
#[AllowDynamicProperties]
class ProductSuggestRoute extends AbstractProductSuggestRoute
{
public function __construct(
Expand All @@ -42,7 +43,7 @@ public function __construct(
ProductDefinition $definition,
private readonly MakairaProductFetchingService $makairaProductFetchingService,
private readonly ShopwareProductFetchingService $shopwareProductFetchingService,
private readonly LoggerInterface $logger,
private readonly LoggerInterface $httpClientLogger,
) {
$this->decorated = $decorated;
$this->eventDispatcher = $eventDispatcher;
Expand All @@ -61,14 +62,14 @@ public function getDecorated(): AbstractProductSuggestRoute
public function load(Request $request, SalesChannelContext $context, Criteria $criteria): ProductSuggestRouteResponse
{
if (!$request->get('search')) {
throw new MissingRequestParameterException('search');
throw RoutingException::missingRequestParameter('search');
}
$criteria->addFilter(
new ProductAvailableFilter($context->getSalesChannel()->getId(), ProductVisibilityDefinition::VISIBILITY_SEARCH)
);
$criteria->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
if (!Feature::isActive('v6.5.0.0')) {
$context->getContext()->addState(Context::STATE_ELASTICSEARCH_AWARE);
$context->getContext()->addState(Criteria::STATE_ELASTICSEARCH_AWARE);
}
$this->searchBuilder->build($request, $criteria, $context);
$this->eventDispatcher->dispatch(
Expand All @@ -81,8 +82,8 @@ public function load(Request $request, SalesChannelContext $context, Criteria $c

try {
$makairaResponse = $this->makairaProductFetchingService->fetchSuggestionsFromMakaira($context, $query);
} catch (\Exception $exception) {
$this->logger->error('[Makaira] ' . $exception->getMessage(), ['type' => __CLASS__]);
} catch (Throwable $exception) {
$this->httpClientLogger->error('[Makaira] ' . $exception->getMessage(), ['type' => self::class]);

return $this->decorated->load($request, $context, $criteria);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Events/ModifierQueryRequestEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class ModifierQueryRequestEvent extends Event

public const NAME_AUTOSUGGESTER = 'makaira.request.modifier.query.autosuggester';

private \ArrayObject $query;
private readonly \ArrayObject $query;

public function __construct(
array $query,
Expand Down
2 changes: 1 addition & 1 deletion src/PersistenceLayer/EntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function findModified(?\DateTimeInterface $lastUpdate, SalesChannelContex
foreach ($this->repositories as $entityName => $repository) {
$criteria = new Criteria();

if (null !== $lastUpdate) {
if ($lastUpdate instanceof \DateTimeInterface) {
$lastUpdateFormatted = $lastUpdate->format(Defaults::STORAGE_DATE_TIME_FORMAT);

$criteria->addFilter(new OrFilter([
Expand Down
12 changes: 6 additions & 6 deletions src/PersistenceLayer/History/HistoryEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ final class HistoryEntity extends Entity
{
use EntityIdTrait;

protected string $salesChannelId;
protected string $languageId;
protected string $entityName;
protected string $entityId;
protected array $data;
protected \DateTimeInterface $sentAt;
private string $salesChannelId;
private string $languageId;
private string $entityName;
private string $entityId;
private array $data;
private \DateTimeInterface $sentAt;

public function getSalesChannelId(): string
{
Expand Down
4 changes: 2 additions & 2 deletions src/PersistenceLayer/History/HistoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function clear(SalesChannelContext $context): void

$ids = $this->repository->searchIds($criteria, $context->getContext())->getIds();

if (0 < \count($ids)) {
if ([] !== $ids) {
$this->repository->delete(array_map(fn (string $id): array => ['id' => $id], $ids), $context->getContext());
}
}
Expand Down Expand Up @@ -86,7 +86,7 @@ public function garbageCollector(int $keep, SalesChannelContext $context): void
foreach ($grouped as $group) {
$delete = \array_slice($group, $keep);

if (0 < \count($delete)) {
if ([] !== $delete) {
$this->repository->delete(array_map(fn (string $id): array => ['id' => $id], $delete), $context->getContext());
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/PersistenceLayer/Normalizer/CategoryNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private function getSubcategories(CategoryEntity $category, SalesChannelContext

private function getHierarchy(CategoryEntity $category): string
{
$hierarchy = null !== $category->getPath() ? \array_slice(explode('|', (string) $category->getPath()), 1, -1) : [];
$hierarchy = null !== $category->getPath() ? \array_slice(explode('|', $category->getPath()), 1, -1) : [];
$hierarchy[] = $category->getId();

return implode('//', $hierarchy);
Expand Down
6 changes: 2 additions & 4 deletions src/PersistenceLayer/Normalizer/ProductNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ public function normalize(Entity $entity, SalesChannelContext $context): array
'path' => '',
]);

$images = $entity->getMedia()->fmap(function (ProductMediaEntity $media): ?array {
return $this->processMedia($media->getMedia());
});
$images = $entity->getMedia()->fmap(fn(ProductMediaEntity $media): ?array => $this->processMedia($media->getMedia()));

return [
'id' => $entity->getId(),
Expand Down Expand Up @@ -106,7 +104,7 @@ private function getSearchKeys(ProductEntity $product): ?string
$searchKeys[] = $product->getTranslation('customSearchKeywords');
}

return 0 < \count($searchKeys) ? implode(' ', $searchKeys) : null;
return [] !== $searchKeys ? implode(' ', $searchKeys) : null;
}

private function getGroupedOptions(?PropertyGroupOptionCollection $properties, ?PropertyGroupOptionCollection $options): array
Expand Down
2 changes: 1 addition & 1 deletion src/PersistenceLayer/Normalizer/Traits/MediaTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ trait MediaTrait
{
private function processMedia(?MediaEntity $media): ?array
{
if (null === $media) {
if (!$media instanceof MediaEntity) {
return null;
}

Expand Down
Loading

0 comments on commit 96ad255

Please sign in to comment.