Skip to content

Commit

Permalink
add recomputeSingleDocumentChangeset and document class clear to unit…
Browse files Browse the repository at this point in the history
… of work
  • Loading branch information
alekitto committed Apr 8, 2020
1 parent 5abdbfc commit 7a41d4c
Show file tree
Hide file tree
Showing 16 changed files with 722 additions and 28 deletions.
3 changes: 0 additions & 3 deletions src/Annotation/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Refugis\ODM\Elastica\Annotation;

use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\Common\Annotations\Annotation\Target;

/**
Expand All @@ -15,8 +14,6 @@ final class Document
* The elastica index/type name.
*
* @var string
*
* @Required()
*/
public $type;

Expand Down
13 changes: 13 additions & 0 deletions src/Exception/DocumentNotManagedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Refugis\ODM\Elastica\Exception;

use Throwable;

class DocumentNotManagedException extends InvalidArgumentException
{
public function __construct(object $document, Throwable $previous = null)
{
parent::__construct(\sprintf('Document %s is not managed. A document is managed if it\'s fetched from the database or registered as new through DocumentManager#persist', self::objToStr($document)), 0, $previous);
}
}
2 changes: 1 addition & 1 deletion src/Exception/ExceptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace Refugis\ODM\Elastica\Exception;

interface ExceptionInterface
interface ExceptionInterface extends \Throwable
{
}
18 changes: 18 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace Refugis\ODM\Elastica\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
/**
* Helper method to show an object as string.
*
* @param object $obj
*
* @return string
*/
protected static function objToStr(object $obj): string
{
return \method_exists($obj, '__toString') ? (string) $obj : \get_class($obj).'@'.\spl_object_hash($obj);
}
}
30 changes: 30 additions & 0 deletions src/Exception/UnexpectedDocumentStateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);

namespace Refugis\ODM\Elastica\Exception;

use Refugis\ODM\Elastica\UnitOfWork;
use Throwable;

class UnexpectedDocumentStateException extends InvalidArgumentException
{
public function __construct(int $state, Throwable $previous = null)
{
parent::__construct(\sprintf('Unexpected document state "%s"', self::getStateAsString($state)), 0, $previous);
}

/**
* Gets the state string.
*/
private static function getStateAsString(int $state): string
{
switch ($state) {
case UnitOfWork::STATE_MANAGED: return 'MANAGED';
case UnitOfWork::STATE_NEW: return 'NEW';
case UnitOfWork::STATE_DETACHED: return 'DETACHED';
case UnitOfWork::STATE_REMOVED: return 'REMOVED';

default:
return \sprintf('UNKNOWN (%u)', $state);
}
}
}
3 changes: 3 additions & 0 deletions src/Id/IdentityGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public function generate(DocumentManagerInterface $dm, $document)
return $collection->getLastInsertedId();
}

/**
* {@inheritdoc}
*/
public function isPostInsertGenerator(): bool
{
return true;
Expand Down
10 changes: 8 additions & 2 deletions src/Id/PostInsertId.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@ final class PostInsertId
*/
private $id;

public function __construct($document, string $id)
public function __construct(object $document, string $id)
{
$this->document = $document;
$this->id = $id;
}

public function getDocument()
/**
* Gets the document for this id.
*/
public function getDocument(): object
{
return $this->document;
}

/**
* Returns the newly generated id.
*/
public function getId(): string
{
return $this->id;
Expand Down
26 changes: 25 additions & 1 deletion src/Metadata/Processor/DocumentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Refugis\ODM\Elastica\Metadata\Processor;

use Doctrine\Common\Inflector\Inflector;
use Elastica\Type;
use Kcs\Metadata\Loader\Processor\Annotation\Processor;
use Kcs\Metadata\Loader\Processor\ProcessorInterface;
use Kcs\Metadata\MetadataInterface;
Expand All @@ -22,7 +24,29 @@ class DocumentProcessor implements ProcessorInterface
public function process(MetadataInterface $metadata, $subject): void
{
$metadata->document = true;
$metadata->collectionName = $subject->type;

$metadata->collectionName = $subject->type ?? $this->calculateType($metadata->name);
if (\class_exists(Type::class) && false === \strpos($metadata->collectionName, '/')) {
$metadata->collectionName .= '/'.$metadata->collectionName;
}

$metadata->customRepositoryClassName = $subject->repositoryClass;
}

/**
* Build a collection name from class name.
*
* @param string $name
*
* @return string
*/
private function calculateType(string $name): string
{
$indexName = Inflector::tableize($name);
if (\class_exists(Type::class)) {
return "$indexName/$indexName";
}

return $indexName;
}
}
Loading

0 comments on commit 7a41d4c

Please sign in to comment.