Skip to content

Commit 7a41d4c

Browse files
committed
add recomputeSingleDocumentChangeset and document class clear to unit of work
1 parent 5abdbfc commit 7a41d4c

16 files changed

+722
-28
lines changed

src/Annotation/Document.php

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Refugis\ODM\Elastica\Annotation;
44

5-
use Doctrine\Common\Annotations\Annotation\Required;
65
use Doctrine\Common\Annotations\Annotation\Target;
76

87
/**
@@ -15,8 +14,6 @@ final class Document
1514
* The elastica index/type name.
1615
*
1716
* @var string
18-
*
19-
* @Required()
2017
*/
2118
public $type;
2219

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Refugis\ODM\Elastica\Exception;
4+
5+
use Throwable;
6+
7+
class DocumentNotManagedException extends InvalidArgumentException
8+
{
9+
public function __construct(object $document, Throwable $previous = null)
10+
{
11+
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);
12+
}
13+
}

src/Exception/ExceptionInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace Refugis\ODM\Elastica\Exception;
44

5-
interface ExceptionInterface
5+
interface ExceptionInterface extends \Throwable
66
{
77
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Refugis\ODM\Elastica\Exception;
4+
5+
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
6+
{
7+
/**
8+
* Helper method to show an object as string.
9+
*
10+
* @param object $obj
11+
*
12+
* @return string
13+
*/
14+
protected static function objToStr(object $obj): string
15+
{
16+
return \method_exists($obj, '__toString') ? (string) $obj : \get_class($obj).'@'.\spl_object_hash($obj);
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Refugis\ODM\Elastica\Exception;
4+
5+
use Refugis\ODM\Elastica\UnitOfWork;
6+
use Throwable;
7+
8+
class UnexpectedDocumentStateException extends InvalidArgumentException
9+
{
10+
public function __construct(int $state, Throwable $previous = null)
11+
{
12+
parent::__construct(\sprintf('Unexpected document state "%s"', self::getStateAsString($state)), 0, $previous);
13+
}
14+
15+
/**
16+
* Gets the state string.
17+
*/
18+
private static function getStateAsString(int $state): string
19+
{
20+
switch ($state) {
21+
case UnitOfWork::STATE_MANAGED: return 'MANAGED';
22+
case UnitOfWork::STATE_NEW: return 'NEW';
23+
case UnitOfWork::STATE_DETACHED: return 'DETACHED';
24+
case UnitOfWork::STATE_REMOVED: return 'REMOVED';
25+
26+
default:
27+
return \sprintf('UNKNOWN (%u)', $state);
28+
}
29+
}
30+
}

src/Id/IdentityGenerator.php

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public function generate(DocumentManagerInterface $dm, $document)
1717
return $collection->getLastInsertedId();
1818
}
1919

20+
/**
21+
* {@inheritdoc}
22+
*/
2023
public function isPostInsertGenerator(): bool
2124
{
2225
return true;

src/Id/PostInsertId.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,23 @@ final class PostInsertId
1818
*/
1919
private $id;
2020

21-
public function __construct($document, string $id)
21+
public function __construct(object $document, string $id)
2222
{
2323
$this->document = $document;
2424
$this->id = $id;
2525
}
2626

27-
public function getDocument()
27+
/**
28+
* Gets the document for this id.
29+
*/
30+
public function getDocument(): object
2831
{
2932
return $this->document;
3033
}
3134

35+
/**
36+
* Returns the newly generated id.
37+
*/
3238
public function getId(): string
3339
{
3440
return $this->id;

src/Metadata/Processor/DocumentProcessor.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Refugis\ODM\Elastica\Metadata\Processor;
44

5+
use Doctrine\Common\Inflector\Inflector;
6+
use Elastica\Type;
57
use Kcs\Metadata\Loader\Processor\Annotation\Processor;
68
use Kcs\Metadata\Loader\Processor\ProcessorInterface;
79
use Kcs\Metadata\MetadataInterface;
@@ -22,7 +24,29 @@ class DocumentProcessor implements ProcessorInterface
2224
public function process(MetadataInterface $metadata, $subject): void
2325
{
2426
$metadata->document = true;
25-
$metadata->collectionName = $subject->type;
27+
28+
$metadata->collectionName = $subject->type ?? $this->calculateType($metadata->name);
29+
if (\class_exists(Type::class) && false === \strpos($metadata->collectionName, '/')) {
30+
$metadata->collectionName .= '/'.$metadata->collectionName;
31+
}
32+
2633
$metadata->customRepositoryClassName = $subject->repositoryClass;
2734
}
35+
36+
/**
37+
* Build a collection name from class name.
38+
*
39+
* @param string $name
40+
*
41+
* @return string
42+
*/
43+
private function calculateType(string $name): string
44+
{
45+
$indexName = Inflector::tableize($name);
46+
if (\class_exists(Type::class)) {
47+
return "$indexName/$indexName";
48+
}
49+
50+
return $indexName;
51+
}
2852
}

0 commit comments

Comments
 (0)