Skip to content

Commit

Permalink
Clean up finder class
Browse files Browse the repository at this point in the history
  • Loading branch information
ilijastuden committed Dec 22, 2019
1 parent ae3c1a7 commit 4005dff
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 59 deletions.
27 changes: 8 additions & 19 deletions src/Finder/FinderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@
* (c) A51 doo <info@activecollab.com>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseMigrations\Finder;

/**
* @package ActiveCollab\DatabaseMigrations\Finder
*/
interface FinderInterface
{
/**
* Return migration class name -> class file path map.
*
* @return array
*/
public function getMigrationClassFilePathMap();

/**
* Prepare migration file path based on classified migration name (DoSomethingAwesome) and optional extra arguments.
*
* @param string $classified_name
* @param string|null $migrations_dir
* @param array $extra_arguments
* @return string
*/
public function prepareMigrationPath($classified_name, $migrations_dir = null, ...$extra_arguments);
public function getMigrationClassFilePathMap(): array;
public function prepareMigrationPath(
string $classified_name,
string $migrations_dir = null,
...$extra_arguments
): string;
}
68 changes: 28 additions & 40 deletions src/Finder/MigrationsInChangesetsFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* (c) A51 doo <info@activecollab.com>. All rights reserved.
*/

declare(strict_types=1);

namespace ActiveCollab\DatabaseMigrations\Finder;

use ActiveCollab\DateValue\DateTimeValue;
Expand All @@ -17,32 +19,13 @@
use Psr\Log\LoggerInterface;
use RuntimeException;

/**
* @package ActiveCollab\DatabaseMigrations\Finder
*/
class MigrationsInChangesetsFinder implements FinderInterface
{
/**
* @var LoggerInterface
*/
private $log;

/**
* @var string
*/
private $logger;
private $namespace;

/**
* @var string[]
*/
private $migrations_dirs;

/**
* @param LoggerInterface $log
* @param string $namespace
* @param string[] ...$migrations_dirs
*/
public function __construct(LoggerInterface &$log, $namespace, ...$migrations_dirs)
public function __construct(LoggerInterface $logger, $namespace, string ...$migrations_dirs)
{
if (empty($migrations_dirs)) {
throw new BadMethodCallException('Migration dir, or a list of migration dirs is required');
Expand All @@ -54,15 +37,12 @@ public function __construct(LoggerInterface &$log, $namespace, ...$migrations_di
}
}

$this->log = $log;
$this->logger = $logger;
$this->namespace = $namespace ? '\\' . ltrim($namespace, '\\') : '';
$this->migrations_dirs = $migrations_dirs;
}

/**
* {@inheritdoc}
*/
public function getMigrationClassFilePathMap()
public function getMigrationClassFilePathMap(): array
{
$migrations_by_changeset = [];
$total_migrations_found = 0;
Expand All @@ -84,7 +64,13 @@ public function getMigrationClassFilePathMap()
}

if (empty($migrations_found)) {
$this->log->debug('No migrations found in {migrations_dir}/{changeset}', ['migrations_dir' => $migrations_dir, 'changeset' => $changeset_dir]);
$this->logger->debug(
'No migrations found in {migrations_dir}/{changeset}',
[
'migrations_dir' => $migrations_dir,
'changeset' => $changeset_dir
]
);
} else {
$total_migrations_found += $migrations_found;
}
Expand All @@ -94,7 +80,14 @@ public function getMigrationClassFilePathMap()
}
}

$this->log->debug('{total_migrations} migrations found {total_changesets} changesets', ['total_migrations' => $total_migrations_found, 'total_changesets' => count($migrations_by_changeset), 'changesets' => array_keys($migrations_by_changeset)]);
$this->logger->debug(
'{total_migrations} migrations found {total_changesets} changesets',
[
'total_migrations' => $total_migrations_found,
'total_changesets' => count($migrations_by_changeset),
'changesets' => array_keys($migrations_by_changeset)
]
);

ksort($migrations_by_changeset);

Expand All @@ -113,28 +106,23 @@ public function getMigrationClassFilePathMap()
* @param string $changeset_name
* @return bool
*/
public function isValidChangesetName($changeset_name)
public function isValidChangesetName($changeset_name): bool
{
return (bool) preg_match('/^(\d{4})-(\d{2})-(\d{2})-(.*)$/', $changeset_name);
}

/**
* Prepare full class name.
*
* @param string $migration_file_path
* @return string
*/
private function getMigrationClassName($migration_file_path)
private function getMigrationClassName(string $migration_file_path): string
{
$migration_class = basename($migration_file_path, '.php');

return $this->namespace ? $this->namespace . '\\' . $migration_class : $migration_class;
}

/**
* {@inheritdoc}
*/
public function prepareMigrationPath($classified_name, $migrations_dir = null, ...$extra_arguments)
public function prepareMigrationPath(
string $classified_name,
string $migrations_dir = null,
...$extra_arguments
): string
{
if ($migrations_dir === null) {
$migrations_dir = $this->migrations_dirs[0];
Expand Down

0 comments on commit 4005dff

Please sign in to comment.