Rollup::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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
13
/**
14
 * The Rollup class is responsible for deleting all previously executed migrations from the versions table
15
 * and marking the freshly dumped schema migration (that was created with SchemaDumper) as migrated.
16
 *
17
 * @internal
18
 */
19
class Rollup
20
{
21
    /** @var MigrationsRepository */
22
    private $migrationRepository;
23
24
    /** @var MetadataStorage */
25
    private $metadataStorage;
26
27 3
    public function __construct(
28
        MetadataStorage $metadataStorage,
29
        MigrationsRepository $migrationRepository
30
    ) {
31 3
        $this->migrationRepository = $migrationRepository;
32 3
        $this->metadataStorage     = $metadataStorage;
33 3
    }
34
35
    /**
36
     * @throws RollupFailed
37
     */
38 3
    public function rollup() : Version
39
    {
40 3
        $versions = $this->migrationRepository->getMigrations();
41
42 3
        if (count($versions) === 0) {
43 1
            throw RollupFailed::noMigrationsFound();
44
        }
45
46 2
        if (count($versions) > 1) {
47 1
            throw RollupFailed::tooManyMigrations();
48
        }
49
50 1
        $this->metadataStorage->reset();
51
52 1
        $result = new ExecutionResult($versions->getItems()[0]->getVersion());
53 1
        $this->metadataStorage->complete($result);
54
55 1
        return $result->getVersion();
56
    }
57
}
58