Navigation Menu

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

Commit

Permalink
Merge pull request #11 from baleen/code-quality
Browse files Browse the repository at this point in the history
Code Quality Improvements
  • Loading branch information
gsomoza committed Nov 12, 2015
2 parents a18126e + 709d0a6 commit c026e67
Show file tree
Hide file tree
Showing 42 changed files with 600 additions and 503 deletions.
6 changes: 3 additions & 3 deletions src/Event/Timeline/Progress.php
Expand Up @@ -73,13 +73,13 @@ public function getCurrent()
*/
public function setCurrent($current)
{
if (!is_numeric($current) || (int)$current < 0 || (int)$current > $this->total) {
if (!is_numeric($current) || (int) $current < 0 || (int) $current > $this->total) {
throw new InvalidArgumentException(sprintf(
'Argument must be an integer between zero and total (%s). Value given: %s.',
$this->total,
(int)$current
(int) $current
));
}
$this->current = (int)$current;
$this->current = (int) $current;
}
}
16 changes: 16 additions & 0 deletions src/Exception/BaleenException.php
Expand Up @@ -25,4 +25,20 @@
*/
class BaleenException extends \Exception
{
/**
* throwInvalidObjectException
*
* @param $object
* @param $expected
*
* @throws static
*/
public static function throwInvalidObjectException($object, $expected)
{
throw new static(sprintf(
'Expected an object of type "%s", but got "%s" instead.',
$expected,
is_object($object) ? get_class($object) : gettype($object)
));
}
}
5 changes: 2 additions & 3 deletions src/Migration/Command/MigrationBus.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -23,7 +22,7 @@
use Baleen\Migrations\Exception\MigrationBusException;
use League\Tactician\CommandBus;

class MigrationBus extends CommandBus
final class MigrationBus extends CommandBus implements MigrationBusInterface
{
/**
* @inheritdoc
Expand All @@ -39,7 +38,7 @@ public function __construct(array $middleware)
}
if (!$foundHandler) {
throw new MigrationBusException(sprintf(
'MigraitonBus must have at least one instance of "%s"',
'MigrationBus must have at least one instance of "%s"',
MigrateHandler::class
));
}
Expand Down
Expand Up @@ -17,20 +17,16 @@
* <http://www.doctrine-project.org>.
*/

namespace Baleen\Migrations\Version\Comparator;
namespace Baleen\Migrations\Migration\Command;

/**
* Interface ComparatorAwareInterface
* @package Baleen\Migrations\Version\Comparator
*/
interface ComparatorAwareInterface
interface MigrationBusInterface
{
/**
* Set the comparator
* Executes the given command and optionally returns a value
*
* @param ComparatorInterface $comparator
* @param object $command
*
* @return void
* @return mixed
*/
public function setComparator(ComparatorInterface $comparator);
public function handle($command);
}
55 changes: 30 additions & 25 deletions src/Repository/AbstractRepository.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -24,31 +23,49 @@
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;

/**
* Class AbstractRepository.
*
* @author Gabriel Somoza <gabriel@strategery.io>
*/
abstract class AbstractRepository implements RepositoryInterface, ComparatorAwareInterface
abstract class AbstractRepository implements RepositoryInterface
{
use ComparatorAwareTrait;

/**
* @var FactoryInterface
*/
private $factory = null;

/** @var ComparatorInterface */
private $comparator = null;

/**
* {@inheritdoc}
* AbstractRepository constructor
*
* @param FactoryInterface $factory
* @param ComparatorInterface $comparator
*/
final public function setMigrationFactory(FactoryInterface $factory)
public function __construct(FactoryInterface $factory = null, ComparatorInterface $comparator = null)
{
if (null === $factory) {
$factory = new SimpleFactory();
}
$this->factory = $factory;

if (null === $comparator) {
$comparator = new IdComparator();
}
$this->comparator = $comparator;
}

/**
* @return ComparatorInterface
*/
final private function getComparator()
{
return $this->comparator;
}

/**
Expand All @@ -58,40 +75,28 @@ final public function setMigrationFactory(FactoryInterface $factory)
*/
final protected function getMigrationFactory()
{
if (null === $this->factory) {
$this->factory = new SimpleFactory();
}
return $this->factory;
}

/**
* {@inheritdoc}
*
* @return Linked
*
* @throws RepositoryException
* @inheritdoc
*/
final public function fetchAll()
{
$collection = $this->doFetchAll();
if (!is_object($collection) || !$collection instanceof Linked) {
throw new RepositoryException(sprintf(
'Method AbstractRepository::doFetchAll() must return a "%s" collection. Got "%s" instead.',
Linked::class,
is_object($collection) ? get_class($collection) : gettype($collection)
));
if (!$collection instanceof Linked) {
RepositoryException::throwInvalidObjectException($collection, Linked::class);
}
$collection->sort($this->getComparator());

return $collection;
return $collection->sort($this->getComparator());
}

/**
* Must fetch all versions available to the repository, load them with their migrations, and return them as a
* Linked collection. It must use a factory (default or supplied by 'setMigrationFactory()') to instantiate
* each of the migrations.
*
* @return mixed
* @return Linked
*/
abstract protected function doFetchAll();
}
32 changes: 11 additions & 21 deletions src/Repository/DirectoryRepository.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand Down Expand Up @@ -27,6 +26,7 @@
use Baleen\Migrations\Version\Collection\Linked;
use Baleen\Migrations\Version\Comparator\ComparatorInterface;
use Baleen\Migrations\Version\Comparator\MigrationComparator;
use Baleen\Migrations\Version\LinkedVersion;
use Zend\Code\Scanner\DerivedClassScanner;
use Zend\Code\Scanner\DirectoryScanner;

Expand All @@ -49,18 +49,17 @@ final class DirectoryRepository extends AbstractRepository

/**
* @param string $path Full path to the repository's directory
* @param string $pattern Regex pattern to extract the version ID from a migration's class name. If null it will
* default to DirectoryRepository::PATTERN_DEFAULT
* @param FactoryInterface $migrationFactory
* @param ComparatorInterface $comparator
*
* @param string $pattern Regex pattern to extract the version ID from a migration's class name. If null it will
* default to DirectoryRepository::PATTERN_DEFAULT
* @throws InvalidArgumentException
*/
public function __construct(
$path,
$pattern = null,
FactoryInterface $migrationFactory = null,
ComparatorInterface $comparator = null
ComparatorInterface $comparator = null,
$pattern = null
) {
$path = (string) $path;
if (empty($path) || !is_dir($path)) {
Expand All @@ -73,39 +72,30 @@ public function __construct(
}
$this->pattern = $pattern;


if (null !== $migrationFactory) {
$this->setMigrationFactory($migrationFactory);
}

if (null === $comparator) {
$comparator = new MigrationComparator();
}
$this->setComparator($comparator);

$this->scanner = new DirectoryScanner($path);

parent::__construct($migrationFactory, $comparator);
}

/**
* @inheritdoc
*/
public function doFetchAll()
{
$versions = new Linked([], null, $this->getComparator());
$versions = new Linked();
$classes = $this->scanner->getClasses(true);
foreach ($classes as $class) {
/* @var DerivedClassScanner $class */
$className = $class->getName();
$matches = [];
if ($class->isInstantiable()
&& preg_match($this->pattern, $className, $matches)
if (preg_match($this->pattern, $className, $matches)
&& isset($matches[1])
&& $class->isInstantiable()
) {
$migration = $this->getMigrationFactory()->create($className);
if ($migration instanceof MigrationInterface) {
$id = hash('sha1', $className);
$version = new Version($id);
$version->setMigration($migration);
$version = new LinkedVersion($id, false, $migration);
$versions->add($version);
}
}
Expand Down
15 changes: 3 additions & 12 deletions src/Repository/RepositoryInterface.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand All @@ -20,7 +19,6 @@

namespace Baleen\Migrations\Repository;

use Baleen\Migrations\Migration\Factory\FactoryInterface;
use Baleen\Migrations\Version\Collection\Linked;

/**
Expand All @@ -32,18 +30,11 @@ interface RepositoryInterface
{
/**
* Must fetch all versions available to the repository, load them with their migrations, and return them as a
* Linked collection. It must use a factory (default or supplied by 'setMigrationFactory()') to instantiate
* each of the migrations.
* Linked collection.
*
* @return Linked
*/
public function fetchAll();

/**
* Use a custom factory to create migrations. Useful to inject migration instances with additional dependencies
* (e.g. database adapters).
*
* @param FactoryInterface $factory
* @throws \Baleen\Migrations\Exception\RepositoryException
*/
public function setMigrationFactory(FactoryInterface $factory);
public function fetchAll();
}
12 changes: 3 additions & 9 deletions src/Storage/AbstractStorage.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand Down Expand Up @@ -41,12 +40,8 @@ abstract class AbstractStorage implements StorageInterface
final public function fetchAll()
{
$collection = $this->doFetchAll();
if (!is_object($collection) || !$collection instanceof Migrated) {
throw new StorageException(sprintf(
'Method AbstractStorage::doFetchAll() must return a "%s" collection. Got "%s" instead."',
Migrated::class,
is_object($collection) ? get_class($collection) : gettype($collection)
));
if (!$collection instanceof Migrated) {
StorageException::throwInvalidObjectException($collection, Migrated::class);
}
return $collection;
}
Expand All @@ -56,7 +51,6 @@ final public function fetchAll()
*/
final public function update(VersionInterface $version)
{
$result = null;
if ($version->isMigrated()) {
$result = $this->save($version);
} else {
Expand All @@ -66,7 +60,7 @@ final public function update(VersionInterface $version)
}

/**
* @return VersionInterface[]|Migrated
* @return Migrated
*/
abstract protected function doFetchAll();
}
1 change: 0 additions & 1 deletion src/Storage/StorageInterface.php
@@ -1,5 +1,4 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Expand Down

0 comments on commit c026e67

Please sign in to comment.