Failed Conditions
Pull Request — master (#969)
by
unknown
09:23
created

Rollup   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 37
ccs 10
cts 14
cp 0.7143
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A rollup() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Migrations;
6
7
use Doctrine\Migrations\Exception\RollupFailed;
8
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
9
use Doctrine\Migrations\Version\ExecutionResult;
10
use Doctrine\Migrations\Version\Version;
11
use function count;
12
use function reset;
13
14
/**
15
 * The Rollup class is responsible for deleting all previously executed migrations from the versions table
16
 * and marking the freshly dumped schema migration (that was created with SchemaDumper) as migrated.
17
 *
18
 * @internal
19
 */
20
class Rollup
21
{
22
    /** @var MigrationRepository */
23
    private $migrationRepository;
24
25
    /** @var MetadataStorage */
26
    private $metadataStorage;
27
28 2
    public function __construct(
29
        MetadataStorage $metadataStorage,
30
        MigrationRepository $migrationRepository
31
    ) {
32 2
        $this->migrationRepository = $migrationRepository;
33 2
        $this->metadataStorage     = $metadataStorage;
34 2
    }
35
36
    /**
37
     * @throws RollupFailed
38
     */
39 2
    public function rollup() : Version
40
    {
41 2
        $versions = $this->migrationRepository->getMigrations();
42
43 2
        if (count($versions) === 0) {
44 1
            throw RollupFailed::noMigrationsFound();
45
        }
46
47 1
        if (count($versions) > 1) {
48 1
            throw RollupFailed::tooManyMigrations();
49
        }
50
51
        $this->metadataStorage->reset();
52
53
        $result = new ExecutionResult(reset($versions->getItems())->getVersion());
54
        $this->metadataStorage->complete($result);
55
56
        return $result->getVersion();
57
    }
58
}
59