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

Commit

Permalink
[BCB] Removed ComparatorAwareTrait
Browse files Browse the repository at this point in the history
- Refactored MigrationComparator and NamespacesAwareComparator
  • Loading branch information
gsomoza committed Nov 12, 2015
1 parent 98d6e10 commit 9085a50
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 127 deletions.
2 changes: 0 additions & 2 deletions src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
use Baleen\Migrations\Migration\Factory\FactoryInterface;
use Baleen\Migrations\Migration\Factory\SimpleFactory;
use Baleen\Migrations\Version\Collection\Linked;
use Baleen\Migrations\Version\Comparator\ComparatorAwareInterface;
use Baleen\Migrations\Version\Comparator\ComparatorAwareTrait;
use Baleen\Migrations\Version\Comparator\ComparatorInterface;
use Baleen\Migrations\Version\Comparator\IdComparator;

Expand Down
36 changes: 0 additions & 36 deletions src/Version/Comparator/ComparatorAwareInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,36 @@
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Baleen\Migrations\Version\Comparator;

use Baleen\Migrations\Exception\InvalidArgumentException;
use Baleen\Migrations\Version\LinkedVersionInterface;
use Baleen\Migrations\Version\VersionInterface;

/**
* Trait ComparatorAwareTrait
*
* Class ComparesLinkedVersionsTrait
* @author Gabriel Somoza <gabriel@strategery.io>
*/
trait ComparatorAwareTrait
trait ComparesLinkedVersionsTrait
{
/** @var ComparatorInterface */
private $comparator;

/**
* @param ComparatorInterface $comparator
*/
final public function setComparator(ComparatorInterface $comparator)
{
$this->comparator = $comparator;
}

/**
* @return ComparatorInterface
* Validates the version is a LinkedVersion and returns the class name of its migration
*
* @param VersionInterface $version
*
* @return string
*
* @throws InvalidArgumentException
*/
final protected function getComparator()
final protected function getMigrationClass(VersionInterface $version)
{
return $this->comparator;
if (!$version instanceof LinkedVersionInterface) {
throw new InvalidArgumentException(sprintf(
"Expected version %s to be linked to a migration",
$version->getId()
));
}
return get_class($version->getMigration());
}
}
15 changes: 7 additions & 8 deletions src/Version/Comparator/MigrationComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
namespace Baleen\Migrations\Version\Comparator;

use Baleen\Migrations\Exception\InvalidArgumentException;
use Baleen\Migrations\Version\LinkedVersion;
use Baleen\Migrations\Version\LinkedVersionInterface;
use Baleen\Migrations\Version\VersionInterface;

Expand All @@ -30,18 +31,16 @@
*/
final class MigrationComparator extends AbstractComparator
{
use ComparesLinkedVersionsTrait;

/**
* @inheritdoc
*/
protected function compare(VersionInterface $version1, VersionInterface $version2)
{
if (!$version1 instanceof LinkedVersionInterface || !$version2 instanceof LinkedVersionInterface) {
throw new InvalidArgumentException(
"Expected both versions to be linked to a migration, but at least one of them isn't."
);
}
$class1 = get_class($version1->getMigration());
$class2 = get_class($version2->getMigration());
return strcmp($class1, $class2);
return strcmp(
$this->getMigrationClass($version1),
$this->getMigrationClass($version2)
);
}
}
52 changes: 32 additions & 20 deletions src/Version/Comparator/NamespacesAwareComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use Baleen\Migrations\Exception\InvalidArgumentException;
use Baleen\Migrations\Exception\Version\ComparatorException;
use Baleen\Migrations\Version\LinkedVersion;
use Baleen\Migrations\Version\LinkedVersionInterface;
use Baleen\Migrations\Version\VersionInterface;

Expand All @@ -31,6 +32,8 @@
*/
final class NamespacesAwareComparator extends AbstractComparator
{
use ComparesLinkedVersionsTrait;

/** @var array */
private $namespaces;

Expand Down Expand Up @@ -62,6 +65,14 @@ public function __construct($order, ComparatorInterface $fallbackComparator, arr
parent::__construct($order);
}

/**
* @inheritDoc
*/
public function withOrder($order)
{
return new static($order, $this->fallback, $this->namespaces);
}

/**
* {@inheritdoc}
*
Expand All @@ -85,19 +96,33 @@ public function __construct($order, ComparatorInterface $fallbackComparator, arr
*/
protected function compare(VersionInterface $version1, VersionInterface $version2)
{
if (!$version1 instanceof LinkedVersionInterface || !$version2 instanceof LinkedVersionInterface) {
throw new InvalidArgumentException(
"Expected both versions to be linked to a migration, but at least one of them isn't."
);
}
$class1 = get_class($version1->getMigration());
$class2 = get_class($version2->getMigration());
$class1 = $this->getMigrationClass($version1);
$class2 = $this->getMigrationClass($version2);

if ($class1 === $class2) {
// exit early in this case
return 0;
}

$res = $this->compareNamespaces($class1, $class2);

// null = could not determine order | zero = both orders are equal
if (empty($res)) {
// delegate sorting to the fallback comparator
$res = call_user_func($this->fallback, $version1, $version2);
}
return $res;
}

/**
* Compare using namespaces
*
* @param $class1
* @param $class2
* @return int|null
*/
private function compareNamespaces($class1, $class2)
{
$res = null;
// loop from highest namespace priority to lowest
foreach ($this->namespaces as $namespace) {
Expand All @@ -112,19 +137,6 @@ protected function compare(VersionInterface $version1, VersionInterface $version
break; // exit as soon as we found a sort order
}
}
// null = could not determine order / zero = both orders are equal
if (empty($res)) {
// delegate sorting to the fallback comparator
$res = call_user_func($this->fallback, $version1, $version2);
}
return $res;
}

/**
* @inheritDoc
*/
public function withOrder($order)
{
return new static($order, $this->fallback, $this->namespaces);
}
}
44 changes: 0 additions & 44 deletions test/Version/Comparator/ComparatorAwareTraitTest.php

This file was deleted.

0 comments on commit 9085a50

Please sign in to comment.