CurrentMigrationStatusCalculator.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Migrations\Version;
  4. use Doctrine\Migrations\Metadata\AvailableMigration;
  5. use Doctrine\Migrations\Metadata\AvailableMigrationsList;
  6. use Doctrine\Migrations\Metadata\ExecutedMigration;
  7. use Doctrine\Migrations\Metadata\ExecutedMigrationsList;
  8. use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
  9. use function array_filter;
  10. /**
  11. * The MigrationPlanCalculator is responsible for calculating the plan for migrating from the current
  12. * version to another version.
  13. */
  14. final class CurrentMigrationStatusCalculator implements MigrationStatusCalculator
  15. {
  16. /** @var MigrationPlanCalculator */
  17. private $migrationPlanCalculator;
  18. /** @var MetadataStorage */
  19. private $metadataStorage;
  20. public function __construct(
  21. MigrationPlanCalculator $migrationPlanCalculator,
  22. MetadataStorage $metadataStorage
  23. ) {
  24. $this->migrationPlanCalculator = $migrationPlanCalculator;
  25. $this->metadataStorage = $metadataStorage;
  26. }
  27. public function getExecutedUnavailableMigrations(): ExecutedMigrationsList
  28. {
  29. $executedMigrations = $this->metadataStorage->getExecutedMigrations();
  30. $availableMigration = $this->migrationPlanCalculator->getMigrations();
  31. return new ExecutedMigrationsList(array_filter($executedMigrations->getItems(), static function (ExecutedMigration $migrationInfo) use ($availableMigration): bool {
  32. return ! $availableMigration->hasMigration($migrationInfo->getVersion());
  33. }));
  34. }
  35. public function getNewMigrations(): AvailableMigrationsList
  36. {
  37. $executedMigrations = $this->metadataStorage->getExecutedMigrations();
  38. $availableMigration = $this->migrationPlanCalculator->getMigrations();
  39. return new AvailableMigrationsList(array_filter($availableMigration->getItems(), static function (AvailableMigration $migrationInfo) use ($executedMigrations): bool {
  40. return ! $executedMigrations->hasMigration($migrationInfo->getVersion());
  41. }));
  42. }
  43. }