Skip to content
This repository has been archived by the owner on Apr 18, 2023. It is now read-only.

Commit

Permalink
Made MigrationRunner immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
gsomoza committed Apr 25, 2016
1 parent d36ac9c commit ad0671c
Show file tree
Hide file tree
Showing 35 changed files with 280 additions and 248 deletions.
1 change: 1 addition & 0 deletions src/Migration/Command/Middleware/TransactionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public function execute($command, callable $next)
$migration = $command->getMigration();
if (!$migration instanceof TransactionAwareInterface) {
$next($command);
return;
}

$result = null;
Expand Down
2 changes: 1 addition & 1 deletion src/Migration/Command/MigrationBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ interface MigrationBusInterface
*
* @return mixed
*/
public function handle(MigrateCommand $command);
public function handle($command);
}
2 changes: 1 addition & 1 deletion src/Service/Command/DomainBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ interface DomainBusInterface
*
* @return mixed
*/
public function handle(DomainCommandInterface $command);
public function handle($command);
}
7 changes: 4 additions & 3 deletions src/Service/Command/Factory/DomainCommandBusFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Baleen\Migrations\Service\Command\Migrate\Converge\ConvergeHandler;
use Baleen\Migrations\Service\Command\Migrate\Single\SingleCommand;
use Baleen\Migrations\Service\Command\Migrate\Single\SingleHandler;
use Baleen\Migrations\Service\Runner\ContextualRunnerInterface;
use Baleen\Migrations\Service\Runner\Factory\CollectionRunnerFactory;
use Baleen\Migrations\Service\Runner\MigrationRunner;
use Baleen\Migrations\Shared\Event\PublisherInterface;
Expand All @@ -39,7 +40,7 @@
*/
final class DomainCommandBusFactory
{
/** @var MigrationRunner */
/** @var ContextualRunnerInterface */
private $migrationRunner;

/** @var PublisherInterface */
Expand All @@ -49,9 +50,9 @@ final class DomainCommandBusFactory
* DomainCommandBusFactory constructor.
*
* @param PublisherInterface $publisher
* @param MigrationRunner $migrationRunner
* @param ContextualRunnerInterface $migrationRunner
*/
public function __construct(PublisherInterface $publisher, MigrationRunner $migrationRunner)
public function __construct(PublisherInterface $publisher, ContextualRunnerInterface $migrationRunner)
{
$this->migrationRunner = $migrationRunner;
$this->publisher = $publisher;
Expand Down
28 changes: 14 additions & 14 deletions src/Service/Runner/CollectionRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
namespace Baleen\Migrations\Service\Runner;

use Baleen\Migrations\Migration\OptionsInterface;
use Baleen\Migrations\Service\Command\HasCollectionTrait;
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionAfterEvent;
use Baleen\Migrations\Service\Runner\Event\Collection\CollectionBeforeEvent;
use Baleen\Migrations\Shared\Collection\CollectionInterface;
use Baleen\Migrations\Shared\Event\Context\CollectionContext;
use Baleen\Migrations\Shared\Event\MutePublisher;
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait;
use Baleen\Migrations\Shared\Event\PublisherInterface;
use Baleen\Migrations\Version\Collection\Collection;
use Baleen\Migrations\Version\VersionInterface;
Expand All @@ -32,33 +35,32 @@
* Class CollectionRunner
* @author Gabriel Somoza <gabriel@strategery.io>
*/
final class CollectionRunner extends AbstractRunner
final class CollectionRunner implements RunnerInterface
{
/** @var CollectionInterface */
private $collection;
use HasCollectionTrait;
use HasInternalPublisherTrait;

/** @var MigrationRunner */
/** @var ContextualRunnerInterface */
private $migrationRunner;

/**
* CollectionRunner constructor.
* @param CollectionInterface $collection
* @param RunnerInterface $migrationRunner Will be use to run each individual migration
* @param ContextualRunnerInterface $migrationRunner Will be use to run each individual migration
* @param PublisherInterface $publisher
*/
public function __construct(
CollectionInterface $collection,
RunnerInterface $migrationRunner = null,
ContextualRunnerInterface $migrationRunner = null,
PublisherInterface $publisher = null
) {
$this->collection = $collection;

if (null === $migrationRunner) {
$migrationRunner = new MigrationRunner(null, $publisher);
}
$this->migrationRunner = $migrationRunner;

parent::__construct($publisher);
$this->setCollection($collection);
$this->setPublisher($publisher);
}

/**
Expand All @@ -72,9 +74,9 @@ public function __construct(
public function run(VersionInterface $target, OptionsInterface $options)
{
$current = 1;
$collection = $this->collection;
$collection = $this->getCollection();
$context = CollectionContext::createWithProgress(max($collection->count(), 1), $current);
$this->migrationRunner->setContext($context);
$migrationRunner = $this->migrationRunner->withContext($context);

$this->getPublisher()->publish(new CollectionBeforeEvent($target, $options, $collection));

Expand All @@ -85,7 +87,7 @@ public function run(VersionInterface $target, OptionsInterface $options)
$collection->first(); // rewind
foreach ($collection as $version) {
$context->getProgress()->update($current);
$result = $this->migrationRunner->run($version, $options);
$result = $migrationRunner->run($version, $options);
if ($result) {
$modified->add($version);
}
Expand All @@ -97,8 +99,6 @@ public function run(VersionInterface $target, OptionsInterface $options)

$this->getPublisher()->publish(new CollectionAfterEvent($target, $options, $collection));

$this->migrationRunner->clearContext();

return $modified;
}
}
39 changes: 39 additions & 0 deletions src/Service/Runner/ContextualRunnerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Baleen\Migrations\Service\Runner;

use Baleen\Migrations\Shared\Event\Context\ContextInterface;

/**
* Interface ContextualRunnerInterface
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
interface ContextualRunnerInterface extends RunnerInterface
{
/**
* Returns a new instance of this object with the same publisher but new context
*
* @param ContextInterface $context
*
* @return static
*/
public function withContext(ContextInterface $context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@
*/
final class CollectionAfterEvent extends CollectionEvent
{

const EVENT_NAME = 'baleen.migrate.after';
}
4 changes: 2 additions & 2 deletions src/Service/Runner/Event/Collection/CollectionBeforeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class CollectionBeforeEvent extends CollectionEvent
final class CollectionBeforeEvent extends CollectionEvent
{

const EVENT_NAME = 'baleen.migrate.before';
}
4 changes: 2 additions & 2 deletions src/Service/Runner/Event/Migration/MigrateAfterEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class MigrateAfterEvent extends MigrationEvent
final class MigrateAfterEvent extends MigrationEvent
{

const EVENT_NAME = 'baleen.single.after';
}
4 changes: 2 additions & 2 deletions src/Service/Runner/Event/Migration/MigrateBeforeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
class MigrateBeforeEvent extends MigrationEvent
final class MigrateBeforeEvent extends MigrationEvent
{

const EVENT_NAME = 'baleen.single.before';
}
15 changes: 8 additions & 7 deletions src/Service/Runner/Factory/CollectionRunnerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
namespace Baleen\Migrations\Service\Runner\Factory;

use Baleen\Migrations\Service\Runner\CollectionRunner;
use Baleen\Migrations\Service\Runner\ContextualRunnerInterface;
use Baleen\Migrations\Service\Runner\RunnerInterface;
use Baleen\Migrations\Shared\Collection\CollectionInterface;
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait;
use Baleen\Migrations\Shared\Event\PublisherInterface;

/**
Expand All @@ -30,20 +32,19 @@
*/
final class CollectionRunnerFactory implements CollectionRunnerFactoryInterface
{
/** @var PublisherInterface */
private $publisher;
use HasInternalPublisherTrait;

/** @var RunnerInterface */
/** @var ContextualRunnerInterface */
private $migrationRunner;

/**
* CollectionRunnerFactory constructor.
* @param PublisherInterface $publisher
* @param RunnerInterface $migrationRunner
* @param ContextualRunnerInterface $migrationRunner
*/
public function __construct(PublisherInterface $publisher = null, RunnerInterface $migrationRunner = null)
public function __construct(PublisherInterface $publisher = null, ContextualRunnerInterface $migrationRunner = null)
{
$this->publisher = $publisher;
$this->setPublisher($publisher);
$this->migrationRunner = $migrationRunner;
}

Expand All @@ -55,6 +56,6 @@ public function __construct(PublisherInterface $publisher = null, RunnerInterfac
*/
public function create(CollectionInterface $collection)
{
return new CollectionRunner($collection, $this->migrationRunner, $this->publisher);
return new CollectionRunner($collection, $this->migrationRunner, $this->getPublisher());
}
}
23 changes: 21 additions & 2 deletions src/Service/Runner/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
use Baleen\Migrations\Migration\OptionsInterface;
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateAfterEvent;
use Baleen\Migrations\Service\Runner\Event\Migration\MigrateBeforeEvent;
use Baleen\Migrations\Shared\Event\Context\CollectionContext;
use Baleen\Migrations\Shared\Event\Context\CollectionContextInterface;
use Baleen\Migrations\Shared\Event\Context\ContextInterface;
use Baleen\Migrations\Shared\Event\Context\HasContextTrait;
use Baleen\Migrations\Shared\Event\Publisher\HasInternalPublisherTrait;
use Baleen\Migrations\Shared\Event\PublisherInterface;
use Baleen\Migrations\Version\VersionInterface;

Expand All @@ -36,8 +40,11 @@
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
final class MigrationRunner extends AbstractRunner
final class MigrationRunner implements ContextualRunnerInterface
{
use HasInternalPublisherTrait;
use HasContextTrait;

/** @var MigrationBusInterface */
private $migrationBus;

Expand All @@ -58,7 +65,12 @@ public function __construct(
}
$this->migrationBus = $migrationBus;

parent::__construct($publisher, $context);
if (null === $context) {
$context = CollectionContext::createWithProgress(1, 1);
}
$this->setContext($context);

$this->setPublisher($publisher);
}

/**
Expand Down Expand Up @@ -124,4 +136,11 @@ protected function doRun(VersionInterface $version, OptionsInterface $options)
$command = $version->getMigrateCommand($options);
$this->migrationBus->handle($command);
}

/**
* @inheritdoc
*/
final public function withContext(ContextInterface $context) {
return new static($this->migrationBus, $this->getPublisher(), $context);
}
}
15 changes: 0 additions & 15 deletions src/Service/Runner/RunnerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,4 @@ interface RunnerInterface
* @return CollectionInterface|VersionInterface
*/
public function run(VersionInterface $target, OptionsInterface $options);

/**
* Set context information for this migrationRunner
*
* @param ContextInterface $context
* @return void
*/
public function setContext(ContextInterface $context);

/**
* Clear the context information for this migrationRunner
*
* @return ContextInterface|null The deleted context, if any
*/
public function clearContext();
}
33 changes: 19 additions & 14 deletions src/Shared/Event/AbstractDomainEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,22 @@ abstract class AbstractDomainEvent implements DomainEventInterface
/** @var DateTime */
private $occurredOn;

/** @var int */
private $version;

/**
* AbstractDomainEvent constructor.
*
* @param DateTime $createdOn
* @param DateTime $occurredOn
* @param int $version
*/
public function __construct(DateTime $createdOn = null)
public function __construct(DateTime $occurredOn = null, $version = null)
{
if (null === $createdOn) {
$createdOn = new DateTime(); // now
if (null === $occurredOn) {
$occurredOn = new DateTime(); // now
}

$this->occurredOn = $createdOn;
$this->occurredOn = $occurredOn;
$this->version = (int) $version;
}

/**
Expand All @@ -54,20 +58,21 @@ final public function getOccurredOn()
}

/**
* @inheritdoc
* @return int
*/
final public function getPayload() {
return array_merge([
'occurred_on' => $this->getOccurredOn(),
], $this->getAdditionalPayload());
public function getVersion()
{
return $this->version;
}

/**
* @inheritdoc
*/
public function getEventVersion()
{
return 1;
final public function getPayload() {
return array_merge([
'occurred_on' => $this->getOccurredOn(),
'version' => $this->getVersion(),
], $this->getAdditionalPayload());
}

/**
Expand Down
Loading

0 comments on commit ad0671c

Please sign in to comment.