Skip to content

Commit

Permalink
remove ES-to-Pimcore methods, resolve #35
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Apr 7, 2023
1 parent 3dfcd11 commit f844029
Show file tree
Hide file tree
Showing 8 changed files with 0 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace AppBundle\Elasticsearch\Document;

use Elastica\Document;
use Pimcore\Model\DataObject\Category;
use Valantic\ElasticaBridgeBundle\DocumentType\AbstractDocument;
use Valantic\ElasticaBridgeBundle\DocumentType\DocumentInterface;
Expand All @@ -25,15 +24,4 @@ public function treatObjectVariantsAsDocuments(): bool
{
return false;
}

public function getPimcoreElement(Document $document): Category
{
$el = parent::getPimcoreElement($document);

if (!($el instanceof Category)) {
throw new \InvalidArgumentException();
}

return $el;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace AppBundle\Elasticsearch\Document;

use Elastica\Document;
use Pimcore\Model\DataObject\NewsArticle;
use Valantic\ElasticaBridgeBundle\DocumentType\AbstractDocument;
use Valantic\ElasticaBridgeBundle\DocumentType\DocumentInterface;
Expand All @@ -25,15 +24,4 @@ public function treatObjectVariantsAsDocuments(): bool
{
return false;
}

public function getPimcoreElement(Document $document): NewsArticle
{
$el = parent::getPimcoreElement($document);

if (!($el instanceof NewsArticle)) {
throw new \InvalidArgumentException();
}

return $el;
}
}
12 changes: 0 additions & 12 deletions docs/example/src/AppBundle/Elasticsearch/Document/PageDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace AppBundle\Elasticsearch\Document;

use Elastica\Document;
use Pimcore\Model\Document\Page;
use Valantic\ElasticaBridgeBundle\DocumentType\AbstractDocument;
use Valantic\ElasticaBridgeBundle\DocumentType\DocumentInterface;
Expand All @@ -25,15 +24,4 @@ public function treatObjectVariantsAsDocuments(): bool
{
return false;
}

public function getPimcoreElement(Document $document): Page
{
$el = parent::getPimcoreElement($document);

if (!($el instanceof Page)) {
throw new \InvalidArgumentException();
}

return $el;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace AppBundle\Elasticsearch\Document;

use Elastica\Document;
use Pimcore\Model\DataObject\Product;
use Valantic\ElasticaBridgeBundle\DocumentType\AbstractDocument;
use Valantic\ElasticaBridgeBundle\DocumentType\DocumentInterface;
Expand All @@ -25,15 +24,4 @@ public function treatObjectVariantsAsDocuments(): bool
{
return false;
}

public function getPimcoreElement(Document $document): Product
{
$el = parent::getPimcoreElement($document);

if (!($el instanceof Product)) {
throw new \InvalidArgumentException();
}

return $el;
}
}
31 changes: 0 additions & 31 deletions src/DocumentType/AbstractDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@

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;
use Valantic\ElasticaBridgeBundle\Exception\DocumentType\UnknownPimcoreElementType;

abstract class AbstractDocument implements DocumentInterface
Expand Down Expand Up @@ -109,33 +107,4 @@ public function getListingClass(): string

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

public function getPimcoreElement(ElasticaDocument $document): AbstractElement
{
if (in_array($this->getType(), [DocumentInterface::TYPE_OBJECT, DocumentInterface::TYPE_VARIANT], true)) {
$pimcoreId = $this->getPimcoreId($document);
$element = Concrete::getById($pimcoreId);

if (!$element instanceof Concrete) {
throw new PimcoreElementNotFoundException($pimcoreId);
}

return $element;
}

if ($this->getType() === DocumentInterface::TYPE_DOCUMENT) {
/** @var PimcoreDocument $documentTypeClass */
$documentTypeClass = $this->getSubType();
$pimcoreId = $this->getPimcoreId($document);
$element = $documentTypeClass::getById($pimcoreId);

if (!$element instanceof PimcoreDocument) {
throw new PimcoreElementNotFoundException($pimcoreId);
}

return $element;
}

throw new UnknownPimcoreElementType($this->getType());
}
}
10 changes: 0 additions & 10 deletions src/DocumentType/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,4 @@ public function getPimcoreId(Document $document): int;
* @internal
*/
public function getListingClass(): string;

/**
* Given an Elasticsearch document, return the corresponding Pimcore element.
* This method can be overridden to use the correct return type for that instance.
*
* @param Document $document
*
* @return AbstractElement
*/
public function getPimcoreElement(Document $document): AbstractElement;
}
55 changes: 0 additions & 55 deletions src/Index/AbstractIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use Elastica\Document;
use Elastica\Index;
use Elastica\Query;
use Elastica\ResultSet;
use Pimcore\Model\Element\AbstractElement;
use RuntimeException;
use Valantic\ElasticaBridgeBundle\DocumentType\DocumentInterface;
use Valantic\ElasticaBridgeBundle\DocumentType\Index\IndexDocumentInterface;
use Valantic\ElasticaBridgeBundle\Elastica\Client\ElasticsearchClient;
Expand Down Expand Up @@ -93,21 +90,6 @@ public function getIndexDocumentInstance(Document $document): ?IndexDocumentInte
return null;
}

public function getDocumentFromElement(AbstractElement $element): ?Document
{
$documentInstance = $this->findIndexDocumentInstanceByPimcore($element);

if (!$documentInstance instanceof IndexDocumentInterface) {
return null;
}

try {
return $this->getElasticaIndex()->getDocument($documentInstance->getElasticsearchId($element));
} catch (RuntimeException) {
return null;
}
}

public function findIndexDocumentInstanceByPimcore(AbstractElement $element): ?IndexDocumentInterface
{
foreach ($this->getAllowedDocuments() as $allowedDocument) {
Expand All @@ -125,43 +107,6 @@ public function findIndexDocumentInstanceByPimcore(AbstractElement $element): ?I
return null;
}

/**
* @param int $size Max number of elements to be retrieved aka limit
* @param int $from Number of elements to skip from the beginning aka offset
*
* @return AbstractElement[]
*/
public function searchForElements(Query\AbstractQuery $query, int $size = 10, int $from = 0): array
{
return $this->documentResultToElements(
$this->getElasticaIndex()
->search(
(new Query($query))
->setSize($size)
->setFrom($from)
)
);
}

/**
* @return AbstractElement[]
*/
public function documentResultToElements(ResultSet $result): array
{
$elements = [];
foreach ($result->getDocuments() as $esDoc) {
$instance = $this->getIndexDocumentInstance($esDoc);

if (!$instance instanceof IndexDocumentInterface) {
continue;
}

$elements[] = $instance->getPimcoreElement($esDoc);
}

return $elements;
}

public function subscribedDocuments(): array
{
return $this->getAllowedDocuments();
Expand Down
9 changes: 0 additions & 9 deletions src/Index/IndexInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,6 @@ public function getIndexDocumentInstance(Document $document): ?IndexDocumentInte
*/
public function getElasticaIndex(): Index;

/**
* Given a Pimcore element, returns the corresponding Elasticsearch document (if available).
*
* @param AbstractElement $element
*
* @return Document|null
*/
public function getDocumentFromElement(AbstractElement $element): ?Document;

/**
* Given a Pimcore element, returns the corresponding IndexDocumentInterface.
*
Expand Down

0 comments on commit f844029

Please sign in to comment.