Skip to content

Commit

Permalink
use enums, make some constants private, resolve #28
Browse files Browse the repository at this point in the history
fixes related to #34 and #35
  • Loading branch information
limenet committed Apr 7, 2023
1 parent f844029 commit d6ad7bd
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 168 deletions.
5 changes: 5 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ parameters:
count: 1
path: src/Command/Refresh.php

-
message: "#^Method Valantic\\\\ElasticaBridgeBundle\\\\DocumentType\\\\AbstractDocument\\:\\:getListingClass\\(\\) should return class\\-string but returns string\\.$#"
count: 1
path: src/DocumentType/AbstractDocument.php

-
message: "#^Method Valantic\\\\ElasticaBridgeBundle\\\\Repository\\\\DocumentRepository\\:\\:__construct\\(\\) has parameter \\$documents with no value type specified in iterable type iterable\\.$#"
count: 1
Expand Down
7 changes: 4 additions & 3 deletions src/Command/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Process\Process;
use Valantic\ElasticaBridgeBundle\Elastica\Client\ElasticsearchClient;
use Valantic\ElasticaBridgeBundle\Enum\IndexBlueGreenSuffix;
use Valantic\ElasticaBridgeBundle\Exception\Index\BlueGreenIndicesIncorrectlySetupException;
use Valantic\ElasticaBridgeBundle\Index\IndexInterface;
use Valantic\ElasticaBridgeBundle\Repository\IndexDocumentRepository;
Expand Down Expand Up @@ -188,8 +189,8 @@ protected function ensureCorrectBlueGreenIndexSetup(IndexInterface $indexConfig)
$nonAliasIndex->delete();
}

foreach (IndexInterface::INDEX_SUFFIXES as $suffix) {
$name = $indexConfig->getName() . $suffix;
foreach (IndexBlueGreenSuffix::cases() as $suffix) {
$name = $indexConfig->getName() . $suffix->value;
$aliasIndex = $this->esClient->getIndex($name);

if ($shouldDelete && $aliasIndex->exists()) {
Expand All @@ -204,7 +205,7 @@ protected function ensureCorrectBlueGreenIndexSetup(IndexInterface $indexConfig)
try {
$indexConfig->getBlueGreenActiveSuffix();
} catch (BlueGreenIndicesIncorrectlySetupException) {
$this->esClient->getIndex($indexConfig->getName() . IndexInterface::INDEX_SUFFIX_BLUE)->addAlias($indexConfig->getName());
$this->esClient->getIndex($indexConfig->getName() . IndexBlueGreenSuffix::BLUE->value)->addAlias($indexConfig->getName());
}

$this->output->writeln('<comment>-> Ensured indices are correctly set up with alias</comment>');
Expand Down
4 changes: 2 additions & 2 deletions src/Command/PopulateIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

class PopulateIndex extends BaseCommand
{
final public const OPTION_CONFIG = 'config';
final public const OPTION_INDEX = 'index';
private const OPTION_CONFIG = 'config';
private const OPTION_INDEX = 'index';
protected ElasticsearchClient $esClient;

public function __construct(
Expand Down
6 changes: 3 additions & 3 deletions src/DependencyInjection/ValanticElasticaBridgeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
*/
class ValanticElasticaBridgeExtension extends Extension
{
final public const TAG_INDEX = 'valantic.elastica_bridge.index';
final public const TAG_DOCUMENT = 'valantic.elastica_bridge.document';
final public const TAG_DOCUMENT_INDEX = 'valantic.elastica_bridge.document_index';
private const TAG_INDEX = 'valantic.elastica_bridge.index';
private const TAG_DOCUMENT = 'valantic.elastica_bridge.document';
private const TAG_DOCUMENT_INDEX = 'valantic.elastica_bridge.document_index';

/**
* {@inheritDoc}
Expand Down
52 changes: 22 additions & 30 deletions src/DocumentType/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

namespace Valantic\ElasticaBridgeBundle\DocumentType;

use Elastica\Document as ElasticaDocument;
use Pimcore\Model\Asset;
use Pimcore\Model\Document as PimcoreDocument;
use Pimcore\Model\Document\Listing as DocumentListing;
use Pimcore\Model\Asset\Listing as AssetListing;
use Pimcore\Model\Element\AbstractElement;
use Valantic\ElasticaBridgeBundle\Exception\DocumentType\ElasticsearchDocumentNotFoundException;
use UnhandledMatchError;
use Valantic\ElasticaBridgeBundle\Enum\DocumentType;
use Valantic\ElasticaBridgeBundle\Exception\DocumentType\UnknownPimcoreElementType;

abstract class AbstractDocument implements DocumentInterface
{
public function getDocumentType(): ?string
{
if (!in_array($this->getType(), [DocumentInterface::TYPE_DOCUMENT, DocumentInterface::TYPE_ASSET], true)) {
if (!in_array($this->getType(), DocumentType::casesSubTypeListing(), true)) {
return null;
}

$candidate = null;

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
if ($this->getType() === DocumentType::DOCUMENT) {
$candidate = [
PimcoreDocument\Folder::class => 'folder',
PimcoreDocument\Page::class => 'page',
Expand All @@ -41,7 +41,7 @@ public function getDocumentType(): ?string
}
}

if ($this->getType() === DocumentInterface::TYPE_ASSET) {
if ($this->getType() === DocumentType::ASSET) {
$candidate = [
Asset\Archive::class => 'archive',
Asset\Audio::class => 'audio',
Expand All @@ -67,44 +67,36 @@ public function getDocumentType(): ?string

final public function getElasticsearchId(AbstractElement $element): string
{
if (in_array($element->getType(), DocumentInterface::TYPES, true)) {
return $element->getType() . $element->getId();
}
$documentType = DocumentType::tryFrom($element->getType());

if ($element instanceof Asset) {
return DocumentInterface::TYPE_ASSET . $element->getId();
return DocumentType::ASSET->value . $element->getId();
}

if ($element instanceof PimcoreDocument) {
return DocumentInterface::TYPE_DOCUMENT . $element->getId();
return DocumentType::DOCUMENT->value . $element->getId();
}

throw new UnknownPimcoreElementType($element->getType());
}

final public function getPimcoreId(ElasticaDocument $document): int
{
if ($document->getId() === null) {
throw new ElasticsearchDocumentNotFoundException($document->getId());
if (in_array($documentType, DocumentType::casesDataObjects(), true)) {
return $documentType->value . $element->getId();
}

return (int) str_replace(DocumentInterface::TYPES, '', $document->getId());
throw new UnknownPimcoreElementType($documentType?->value);
}

/**
* @return class-string
*/
public function getListingClass(): string
{
if (in_array($this->getType(), [DocumentInterface::TYPE_OBJECT, DocumentInterface::TYPE_VARIANT], true)) {
return $this->getSubType() . '\Listing';
try {
return match ($this->getType()) {
DocumentType::ASSET => AssetListing::class,
DocumentType::DOCUMENT => DocumentListing::class,
DocumentType::DATA_OBJECT, DocumentType::VARIANT => $this->getSubType() . '\Listing',
};
} catch (UnhandledMatchError) {
throw new UnknownPimcoreElementType($this->getType()->value);
}

if ($this->getType() === DocumentInterface::TYPE_ASSET) {
return AssetListing::class;
}

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
return DocumentListing::class;
}

throw new UnknownPimcoreElementType($this->getType());
}
}
42 changes: 4 additions & 38 deletions src/DocumentType/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Elastica\Document;
use Pimcore\Model\Element\AbstractElement;
use Valantic\ElasticaBridgeBundle\Command\Index as IndexCommand;
use Valantic\ElasticaBridgeBundle\Enum\DocumentType;

/**
* This interface describes a Pimcore element in the context of Elasticsearch.
Expand All @@ -18,38 +19,14 @@
interface DocumentInterface
{
/**
* For use in getType().
* Indicates the definition of an Asset.
* Defines the Pimcore type of this document.
*/
public const TYPE_ASSET = 'asset';
/**
* For use in getType().
* Indicates the definition of a Document.
*/
public const TYPE_DOCUMENT = 'document';
/**
* For use in getType().
* Indicates the definition of a DataObject.
*/
public const TYPE_OBJECT = 'object';
/**
* For use in getType().
* Indicates the definition of a DataObject Variant.
*/
public const TYPE_VARIANT = 'variant';
public const TYPES = [self::TYPE_ASSET, self::TYPE_DOCUMENT, self::TYPE_OBJECT, self::TYPE_VARIANT];

/**
* Defines the Pimcore type of this document. One of self::TYPES.
*
* @return string
*/
public function getType(): string;
public function getType(): DocumentType;

/**
* The subtype, e.g. the DataObject class or Document\Page.
*
* @return string
* @return class-string
*/
public function getSubType(): string;

Expand All @@ -71,17 +48,6 @@ public function getDocumentType(): ?string;
*/
public function getElasticsearchId(AbstractElement $element): string;

/**
* Returns the Pimcore ID for an Elasticsearch document.
*
* @param Document $document
*
* @return int
*
* @internal
*/
public function getPimcoreId(Document $document): int;

/**
* The name of the class to use for listing all the associated Pimcore elements.
*
Expand Down
25 changes: 25 additions & 0 deletions src/Enum/DocumentType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Enum;

enum DocumentType: string
{
case ASSET = 'asset';
case DOCUMENT = 'document';
case DATA_OBJECT = 'object';
case VARIANT = 'variant';

/** @return self[] */
public static function casesDataObjects()
{
return [self::DATA_OBJECT, self::VARIANT];
}

/** @return self[] */
public static function casesSubTypeListing()
{
return [self::ASSET, self::DOCUMENT];
}
}
11 changes: 11 additions & 0 deletions src/Enum/IndexBlueGreenSuffix.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Valantic\ElasticaBridgeBundle\Enum;

enum IndexBlueGreenSuffix: string
{
case BLUE = 'blue';
case GREEN = 'green';
}

This file was deleted.

15 changes: 0 additions & 15 deletions src/Exception/DocumentType/PimcoreElementNotFoundException.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Exception/DocumentType/UnknownPimcoreDocumentType.php

This file was deleted.

0 comments on commit d6ad7bd

Please sign in to comment.