Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Managers WIP #107

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/Bridge/Doctrine/Persister/ObjectManagerRegistryPersister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace Fidry\AliceDataFixtures\Bridge\Doctrine\Persister;

use Doctrine\Common\Persistence\ManagerRegistry;
use Fidry\AliceDataFixtures\Persistence\PersisterInterface;

/**
* Class ObjectManagerRegistriesPersister.
*/
class ObjectManagerRegistryPersister implements PersisterInterface
{
/**
* @var ManagerRegistry[]
*/
private $managerRegistries;

/**
* @var ObjectManagerPersister[]
*/
private $managerPersisters;

public function __construct(array $managerRegistries)
{
$this->managerRegistries = $managerRegistries;
}

/**
* Persists objects into the database.
*
* @param object $object
*/
public function persist($object)
{
foreach ($this->getManagerPersisters() as $persister) {
$persister->persist($object);
}
}

public function flush()
{
foreach ($this->getManagerPersisters() as $persister) {
$persister->flush();
}
}

private function getManagerPersisters()
{
if (null === $this->managerPersisters) {
$this->managerPersisters = [];

foreach ($this->managerRegistries as $managerRegistry) {
foreach ($managerRegistry->getManagers() as $manager) {
$this->managerPersisters[] = new ObjectManagerPersister($manager);
}
}
}

return $this->managerPersisters;
}
}