Skip to content

Commit

Permalink
support assets, resolve #34
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Apr 7, 2023
1 parent 96394bf commit 56ce123
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 16 deletions.
64 changes: 49 additions & 15 deletions src/DocumentType/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Valantic\ElasticaBridgeBundle\DocumentType;

use Elastica\Document as ElasticaDocument;
use Pimcore\Model\Asset;
use Pimcore\Model\DataObject\Concrete;
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 Valantic\ElasticaBridgeBundle\Exception\DocumentType\PimcoreElementNotFoundException;
Expand All @@ -17,23 +19,48 @@ abstract class AbstractDocument implements DocumentInterface
{
public function getDocumentType(): ?string
{
if ($this->getType() !== DocumentInterface::TYPE_DOCUMENT) {
if (!in_array($this->getType(), [DocumentInterface::TYPE_DOCUMENT, DocumentInterface::TYPE_ASSET], true)) {
return null;
}

$candidate = [
PimcoreDocument\Folder::class => 'folder',
PimcoreDocument\Page::class => 'page',
PimcoreDocument\Snippet::class => 'snippet',
PimcoreDocument\Link::class => 'link',
PimcoreDocument\Hardlink::class => 'hardlink',
PimcoreDocument\Email::class => 'email',
PimcoreDocument\Newsletter::class => 'newsletter',
PimcoreDocument\Printpage::class => 'printpage',
PimcoreDocument\Printcontainer::class => 'printcontainer',
][$this->getSubType()] ?? null;

if ($candidate === null || !in_array($candidate, PimcoreDocument::getTypes(), true)) {
$candidate = null;

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
$candidate = [
PimcoreDocument\Folder::class => 'folder',
PimcoreDocument\Page::class => 'page',
PimcoreDocument\Snippet::class => 'snippet',
PimcoreDocument\Link::class => 'link',
PimcoreDocument\Hardlink::class => 'hardlink',
PimcoreDocument\Email::class => 'email',
PimcoreDocument\Newsletter::class => 'newsletter',
PimcoreDocument\Printpage::class => 'printpage',
PimcoreDocument\Printcontainer::class => 'printcontainer',
][$this->getSubType()] ?? null;

if (!in_array($candidate, PimcoreDocument::getTypes(), true)) {
throw new UnknownPimcoreElementType($candidate);
}
}

if ($this->getType() === DocumentInterface::TYPE_ASSET) {
$candidate = [
Asset\Archive::class => 'archive',
Asset\Audio::class => 'audio',
Asset\Document::class => 'document',
Asset\Folder::class => 'folder',
Asset\Image::class => 'image',
Asset\Text::class => 'text',
Asset\Unknown::class => 'unknown',
Asset\Video::class => 'video',
][$this->getSubType()] ?? null;

if (!in_array($candidate, Asset::getTypes(), true)) {
throw new UnknownPimcoreElementType($candidate);
}
}

if ($candidate === null) {
throw new UnknownPimcoreElementType($candidate);
}

Expand All @@ -46,6 +73,10 @@ final public function getElasticsearchId(AbstractElement $element): string
return $element->getType() . $element->getId();
}

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

if ($element instanceof PimcoreDocument) {
return DocumentInterface::TYPE_DOCUMENT . $element->getId();
}
Expand All @@ -68,8 +99,11 @@ public function getListingClass(): string
return $this->getSubType() . '\Listing';
}

if (in_array($this->getType(), [DocumentInterface::TYPE_ASSET], true)) {
return AssetListing::class;
}

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
// TODO: this listing doesn't seem to have an option to e.g. only list Hardlinks
return DocumentListing::class;
}

Expand Down
2 changes: 1 addition & 1 deletion src/DocumentType/Index/ListingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function getListingInstance(IndexInterface $index): AbstractListing
$listingInstance->setObjectTypes([DataObject\AbstractObject::OBJECT_TYPE_OBJECT, DataObject\AbstractObject::OBJECT_TYPE_VARIANT]);
}

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
if (in_array($this->getType(), [DocumentInterface::TYPE_DOCUMENT, DocumentInterface::TYPE_ASSET], true)) {
$typeCondition = sprintf("`type` = '%s'", $this->getDocumentType());
if ($this->getIndexListingCondition() !== null) {
$listingInstance->setCondition(sprintf('%s AND (%s)', $typeCondition, $this->getIndexListingCondition()));
Expand Down

0 comments on commit 56ce123

Please sign in to comment.