Skip to content

Commit

Permalink
refactor(DuplicationService): retrieve it from the manager registry o…
Browse files Browse the repository at this point in the history
…r manager
  • Loading branch information
williarin committed Jul 14, 2022
1 parent 6533c7e commit 01b4d92
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,14 @@ Duplicate an entity with all its EAV attributes and terms with `DuplicationServi
The resulting entity is already persisted and has a new ID.

```php
$duplicationService = new DuplicationService($entityManager, new AsciiSlugger();
$duplicationService = $manager->getDuplicationService();

// Duplicate by ID
$newProduct = $this->duplicationService->duplicate(23, Product::class);
$newProduct = $duplicationService->duplicate(23, Product::class);

// Duplicate by object
$product = $manager->getRepository(Product::class)->findOneBySku('woo-hoodie-with-zipper');
$newProduct = $this->duplicationService->duplicate($product);
$newProduct = $duplicationService->duplicate($product);
```

### Available entities and repositories
Expand Down
17 changes: 16 additions & 1 deletion src/AbstractEntityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@
use Doctrine\DBAL\Connection;
use ReflectionClass;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\String\Slugger\AsciiSlugger;
use Williarin\WordpressInterop\Attributes\RepositoryClass;
use Williarin\WordpressInterop\Bridge\Entity\BaseEntity;
use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Persistence\DuplicationService;
use Williarin\WordpressInterop\Persistence\DuplicationServiceInterface;

abstract class AbstractEntityManager implements EntityManagerInterface
{
Expand All @@ -18,7 +21,8 @@ abstract class AbstractEntityManager implements EntityManagerInterface
public function __construct(
private Connection $connection,
protected SerializerInterface $serializer,
private string $tablePrefix = 'wp_'
private string $tablePrefix = 'wp_',
protected ?DuplicationServiceInterface $duplicationService = null,
) {
}

Expand Down Expand Up @@ -48,6 +52,17 @@ public function getRepository(string $entityClassName): RepositoryInterface
return $this->repositories[$entityClassName];
}

public function getDuplicationService(): DuplicationServiceInterface
{
if (!$this->duplicationService) {
$this->duplicationService = new DuplicationService(new AsciiSlugger());
}

$this->duplicationService->setEntityManager($this);

return $this->duplicationService;
}

public function getConnection(): Connection
{
return $this->connection;
Expand Down
9 changes: 9 additions & 0 deletions src/AbstractManagerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Exception\InvalidArgumentException;
use Williarin\WordpressInterop\Persistence\DuplicationServiceInterface;

abstract class AbstractManagerRegistry implements ManagerRegistryInterface
{
Expand Down Expand Up @@ -63,5 +64,13 @@ public function getRepository(string $entityClassName, ?string $managerName = nu
;
}

public function getDuplicationService(?string $managerName = null): DuplicationServiceInterface
{
return $this
->getManager($managerName)
->getDuplicationService()
;
}

abstract protected function getService(string $name): EntityManagerInterface;
}
3 changes: 3 additions & 0 deletions src/EntityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Williarin\WordpressInterop\Bridge\Repository\RepositoryInterface;
use Williarin\WordpressInterop\Persistence\DuplicationServiceInterface;

interface EntityManagerInterface
{
Expand All @@ -18,4 +19,6 @@ public function getRepositories(): array;
public function getRepository(string $entityClassName): RepositoryInterface;

public function getTablesPrefix(): string;

public function getDuplicationService(): DuplicationServiceInterface;
}
13 changes: 13 additions & 0 deletions src/Exception/EntityManagerNotSetException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Exception;

final class EntityManagerNotSetException extends \RuntimeException
{
public function __construct(string $owningClass)
{
parent::__construct(sprintf('You must call %s::setEntityManager() before use.', $owningClass));
}
}
24 changes: 15 additions & 9 deletions src/Persistence/DuplicationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
use Williarin\WordpressInterop\Bridge\Entity\Term;
use Williarin\WordpressInterop\Criteria\PostRelationshipCondition;
use Williarin\WordpressInterop\EntityManagerInterface;
use Williarin\WordpressInterop\Exception\EntityManagerNotSetException;
use Williarin\WordpressInterop\Exception\MissingEntityTypeException;
use function Williarin\WordpressInterop\Util\String\property_to_field;

final class DuplicationService
final class DuplicationService implements DuplicationServiceInterface
{
public const POST_STATUS_DRAFT = 'draft';
public const POST_STATUS_PRIVATE = 'private';
public const POST_STATUS_PUBLISH = 'publish';

public function __construct(
private EntityManagerInterface $entityManager,
private SluggerInterface $slugger,
) {
private ?EntityManagerInterface $entityManager = null;

public function __construct(private SluggerInterface $slugger)
{
}

public function setEntityManager(EntityManagerInterface $entityManager): void
{
$this->entityManager = $entityManager;
}

public function duplicate(
Expand All @@ -33,6 +35,10 @@ public function duplicate(
string $postStatus = self::POST_STATUS_DRAFT,
string $suffix = ' (Copy)',
): BaseEntity {
if (!$this->entityManager) {
throw new EntityManagerNotSetException(self::class);
}

if (is_int($entityOrId)) {
if ($entityType === null) {
throw new MissingEntityTypeException();
Expand Down
22 changes: 22 additions & 0 deletions src/Persistence/DuplicationServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Williarin\WordpressInterop\Persistence;

use Williarin\WordpressInterop\Bridge\Entity\BaseEntity;
use Williarin\WordpressInterop\EntityManagerAwareInterface;

interface DuplicationServiceInterface extends EntityManagerAwareInterface
{
public const POST_STATUS_DRAFT = 'draft';
public const POST_STATUS_PRIVATE = 'private';
public const POST_STATUS_PUBLISH = 'publish';

public function duplicate(
BaseEntity|int $entityOrId,
?string $entityType = null,
string $postStatus = self::POST_STATUS_DRAFT,
string $suffix = ' (Copy)',
): BaseEntity;
}
12 changes: 12 additions & 0 deletions test/Test/ManagerRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Williarin\WordpressInterop\Bridge\Repository\ProductRepository;
use Williarin\WordpressInterop\EntityManagerInterface;
use Williarin\WordpressInterop\Exception\InvalidArgumentException;
use Williarin\WordpressInterop\Persistence\DuplicationServiceInterface;

class ManagerRegistryTest extends TestCase
{
Expand Down Expand Up @@ -65,6 +66,17 @@ public function testGetRepository(): void
self::assertInstanceOf(ProductRepository::class, $this->managerRegistry->getRepository(Product::class));
}

public function testGetDefaultDuplicationService(): void
{
self::assertInstanceOf(DuplicationServiceInterface::class, $this->managerRegistry->getDuplicationService());
}

public function testGetNonExistentDuplicationService(): void
{
$this->expectException(InvalidArgumentException::class);
$this->managerRegistry->getDuplicationService('fr');
}

private function getManagerFactory(): Closure
{
return function () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

declare(strict_types=1);

namespace Williarin\WordpressInterop\Test\Manipulation;
namespace Williarin\WordpressInterop\Test\Persistence;

use Symfony\Component\String\Slugger\AsciiSlugger;
use Williarin\WordpressInterop\Bridge\Entity\Product;
use Williarin\WordpressInterop\Bridge\Entity\Term;
use Williarin\WordpressInterop\Criteria\PostRelationshipCondition;
use Williarin\WordpressInterop\Exception\EntityManagerNotSetException;
use Williarin\WordpressInterop\Exception\MissingEntityTypeException;
use Williarin\WordpressInterop\Persistence\DuplicationService;
use Williarin\WordpressInterop\Test\TestCase;
Expand All @@ -19,7 +20,7 @@ class DuplicationServiceTest extends TestCase
protected function setUp(): void
{
parent::setUp();
$this->duplicationService = new DuplicationService($this->manager, new AsciiSlugger());
$this->duplicationService = $this->manager->getDuplicationService();
}

public function testDuplicateByIdWithoutEntityClassNameThrowsException(): void
Expand All @@ -28,6 +29,13 @@ public function testDuplicateByIdWithoutEntityClassNameThrowsException(): void
$this->duplicationService->duplicate(23);
}

public function testEntityManagerNotSet(): void
{
$this->expectException(EntityManagerNotSetException::class);
$duplicationService = new DuplicationService(new AsciiSlugger());
$duplicationService->duplicate(23);
}

public function testDuplicateById(): void
{
$newEntity = $this->duplicationService->duplicate(23, Product::class);
Expand Down

0 comments on commit 01b4d92

Please sign in to comment.