Skip to content

Commit

Permalink
Up the code style to PHP 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 1, 2024
1 parent 3c28af8 commit 981c46e
Show file tree
Hide file tree
Showing 28 changed files with 102 additions and 303 deletions.
71 changes: 22 additions & 49 deletions src/Config/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,41 @@
use Setono\SyliusMeilisearchPlugin\Document\Document;
use Setono\SyliusMeilisearchPlugin\Exception\NonExistingResourceException;
use Setono\SyliusMeilisearchPlugin\Indexer\IndexerInterface;
use Webmozart\Assert\Assert;

final class Index
final class Index implements \Stringable
{
/**
* This is the name you gave the index in the configuration.
* This name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing
*/
public string $name;

/**
* This is the FQCN for the document that is mapped to an index in Algolia.
* If you are indexing products this could be Setono\SyliusMeilisearchPlugin\Document\Product
*
* @var class-string<Document>
*/
public string $document;

public IndexerInterface $indexer;

/**
* An array of resources, indexed by the resource name
*
* @var array<string, IndexableResource>
*/
public array $resources;

/** @var non-empty-string|null */
public ?string $prefix;

/**
* @param class-string<Document> $document
* @param array<string, IndexableResource> $resources
*/
public function __construct(
string $name,
string $document,
IndexerInterface $indexer,
array $resources,
string $prefix = null,
/**
* This is the name you gave the index in the configuration.
* This name is also used when resolving the final index name in Meilisearch, so do not change this unless you know what you're doing
*/
public readonly string $name,
/**
* This is the FQCN for the document that is mapped to an index in Algolia.
* If you are indexing products this could be Setono\SyliusMeilisearchPlugin\Document\Product
*
* @var class-string<Document> $document
*/
public readonly string $document,
public readonly IndexerInterface $indexer,
/**
* An array of resources, indexed by the resource name
*
* @var array<string, IndexableResource> $resources
*/
public readonly array $resources,
public readonly ?string $prefix = null,
) {
Assert::stringNotEmpty($name);

if (!is_a($document, Document::class, true)) {
throw new \InvalidArgumentException(sprintf(
'The document class %s MUST be an instance of %s',
$document,
Document::class,
));
}

$this->name = $name;
$this->document = $document;
$this->indexer = $indexer;
$this->resources = $resources;
$this->prefix = '' === $prefix ? null : $prefix;
}

/**
* @param string|IndexableResource $resource
*/
public function hasResource($resource): bool
public function hasResource(string|IndexableResource $resource): bool
{
if ($resource instanceof IndexableResource) {
$resource = $resource->name;
Expand Down
37 changes: 13 additions & 24 deletions src/Config/IndexableResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,31 @@
namespace Setono\SyliusMeilisearchPlugin\Config;

use Setono\SyliusMeilisearchPlugin\Model\IndexableInterface;
use Webmozart\Assert\Assert;

/**
* This class represents a Sylius resource that is indexable
*/
final class IndexableResource
final class IndexableResource implements \Stringable
{
/**
* This is the name of the Sylius resource, e.g. 'sylius.product'
*/
public string $name;

/**
* This is the FQCN for the resource
*
* @var class-string<IndexableInterface>
*/
public string $class;

/**
* @param class-string<IndexableInterface> $class
*/
public function __construct(string $name, string $class)
{
Assert::stringNotEmpty($name);

public function __construct(
/**
* This is the name of the Sylius resource, e.g. 'sylius.product'
*/
public readonly string $name,
/**
* This is the FQCN for the resource
*
* @var class-string<IndexableInterface> $class
*/
public readonly string $class,
) {
if (!is_a($class, IndexableInterface::class, true)) {
throw new \InvalidArgumentException(sprintf(
'The document class %s MUST be an instance of %s',
$class,
IndexableInterface::class,
));
}

$this->name = $name;
$this->class = $class;
}

/**
Expand Down
35 changes: 7 additions & 28 deletions src/Controller/Action/SearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,15 @@

final class SearchAction
{
private Environment $twig;

private IndexNameResolverInterface $indexNameResolver;

private TaxonRepositoryInterface $taxonRepository;

private LocaleContextInterface $localeContext;

private EventDispatcherInterface $eventDispatcher;

private IndexRegistry $indexRegistry;

private SortByResolverInterface $sortByResolver;

public function __construct(
Environment $twig,
IndexNameResolverInterface $indexNameResolver,
TaxonRepositoryInterface $taxonRepository,
LocaleContextInterface $localeContext,
EventDispatcherInterface $eventDispatcher,
IndexRegistry $indexableResourceRegistry,
SortByResolverInterface $sortByResolver,
private readonly Environment $twig,
private readonly IndexNameResolverInterface $indexNameResolver,
private readonly TaxonRepositoryInterface $taxonRepository,
private readonly LocaleContextInterface $localeContext,
private readonly EventDispatcherInterface $eventDispatcher,
private readonly IndexRegistry $indexRegistry,
private readonly SortByResolverInterface $sortByResolver,
) {
$this->twig = $twig;
$this->indexNameResolver = $indexNameResolver;
$this->taxonRepository = $taxonRepository;
$this->localeContext = $localeContext;
$this->eventDispatcher = $eventDispatcher;
$this->indexRegistry = $indexableResourceRegistry;
$this->sortByResolver = $sortByResolver;
}

public function __invoke(string $slug): Response
Expand Down
19 changes: 5 additions & 14 deletions src/DataMapper/ImageUrlsDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,15 @@

final class ImageUrlsDataMapper implements DataMapperInterface
{
private CacheManager $cacheManager;

/** @var array<class-string<ResourceInterface>, string> */
private array $resourceToFilterSetMapping;

private string $defaultFilterSet;

/**
* @param array<class-string<ResourceInterface>, string> $resourceToFilterSetMapping
*/
public function __construct(
CacheManager $cacheManager,
array $resourceToFilterSetMapping = [], // todo add this to the plugin configuration
string $defaultFilterSet = 'sylius_large',
private readonly CacheManager $cacheManager,
private readonly array $resourceToFilterSetMapping = [],
// todo add this to the plugin configuration
private readonly string $defaultFilterSet = 'sylius_large',
) {
$this->cacheManager = $cacheManager;
$this->resourceToFilterSetMapping = $resourceToFilterSetMapping;
$this->defaultFilterSet = $defaultFilterSet;
}

public function map(
Expand All @@ -51,7 +42,7 @@ public function map(
foreach ($source->getImages() as $image) {
$imageUrls[] = $this->cacheManager->getBrowserPath(
(string) $image->getPath(),
$this->resourceToFilterSetMapping[get_class($source)] ?? $this->defaultFilterSet,
$this->resourceToFilterSetMapping[$source::class] ?? $this->defaultFilterSet,
[],
null,
UrlGeneratorInterface::ABSOLUTE_PATH,
Expand Down
5 changes: 1 addition & 4 deletions src/DataMapper/Product/PriceDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ final class PriceDataMapper implements DataMapperInterface
{
use FormatAmountTrait;

private ChannelRepositoryInterface $channelRepository;

public function __construct(ChannelRepositoryInterface $channelRepository)
public function __construct(private readonly ChannelRepositoryInterface $channelRepository)
{
$this->channelRepository = $channelRepository;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/DataMapper/Product/TaxonCodesDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@

final class TaxonCodesDataMapper implements DataMapperInterface
{
private bool $includeDescendants;

public function __construct(bool $includeDescendants)
public function __construct(private readonly bool $includeDescendants)
{
$this->includeDescendants = $includeDescendants;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/DataMapper/ResourceNameDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@

final class ResourceNameDataMapper implements DataMapperInterface
{
private IndexRegistry $indexRegistry;

public function __construct(IndexRegistry $indexRegistry)
public function __construct(private readonly IndexRegistry $indexRegistry)
{
$this->indexRegistry = $indexRegistry;
}

public function map(ResourceInterface $source, Document $target, IndexScope $indexScope, array $context = []): void
Expand Down
5 changes: 1 addition & 4 deletions src/DataMapper/UrlDataMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@

final class UrlDataMapper implements DataMapperInterface
{
private ResourceUrlGeneratorInterface $urlGenerator;

public function __construct(ResourceUrlGeneratorInterface $urlGenerator)
public function __construct(private readonly ResourceUrlGeneratorInterface $urlGenerator)
{
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/EventSubscriber/Doctrine/EntityChangeSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

final class EntityChangeSubscriber implements EventSubscriber
{
private MessageBusInterface $commandBus;

public function __construct(MessageBusInterface $commandBus)
public function __construct(private readonly MessageBusInterface $commandBus)
{
$this->commandBus = $commandBus;
}

public function getSubscribedEvents(): array
Expand Down
9 changes: 3 additions & 6 deletions src/EventSubscriber/InjectConfigurationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ final class InjectConfigurationSubscriber implements EventSubscriberInterface
*/
private array $tags = [];

private Environment $twig;

public function __construct(Environment $twig)
public function __construct(private readonly Environment $twig)
{
$this->twig = $twig;
}

public static function getSubscribedEvents(): array
Expand Down Expand Up @@ -64,8 +61,8 @@ public function inject(ResponseEvent $event): void
// this 'if' has been copied from \Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener::onKernelResponse()
if ($response->isRedirection() ||
'html' !== $request->getRequestFormat() ||
false !== stripos($response->headers->get('Content-Disposition', ''), 'attachment;') ||
($response->headers->has('Content-Type') && strpos($response->headers->get('Content-Type', ''), 'html') === false)
false !== stripos((string) $response->headers->get('Content-Disposition', ''), 'attachment;') ||
($response->headers->has('Content-Type') && !str_contains((string) $response->headers->get('Content-Type', ''), 'html'))
) {
return;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Indexer/AbstractIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ public function indexEntities(array $entities): void
$type = null;
foreach ($entities as $entity) {
if ($type === null) {
$type = get_class($entity);
$type = $entity::class;
}

if ($type !== get_class($entity)) {
if ($type !== $entity::class) {
throw new \InvalidArgumentException('All the entities must be of the same type');
}

Expand All @@ -44,7 +44,7 @@ public function indexEntities(array $entities): void

public function removeEntity(IndexableInterface $entity): void
{
$this->removeEntityWithId($entity->getId(), get_class($entity));
$this->removeEntityWithId($entity->getId(), $entity::class);
}

public function removeEntityWithId(mixed $id, string $type): void
Expand Down
Loading

0 comments on commit 981c46e

Please sign in to comment.