• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

andreypostal / php-stuff-versioned / 14454006719

14 Apr 2025 07:25PM UTC coverage: 93.617% (+11.3%) from 82.353%
14454006719

push

github

andreypostal
Check testing

0 of 2 new or added lines in 1 file covered. (0.0%)

44 of 47 relevant lines covered (93.62%)

1.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.62
/src/VersionManager.php
1
<?php
2

3
namespace Andrey\StuffVersioned;
4

5
use Psr\Log\LoggerInterface;
6
use Throwable;
7

8
class VersionManager implements VersionManagerInterface
9
{
10
    /** @var VersionInterface[] */
11
    protected array $versions = [];
12

13
    public function __construct(
2✔
14
        private readonly BackendInterface $backend,
15
        private readonly ?LoggerInterface $logger = null,
16
    ) {
17
    }
2✔
18

19
    public function addVersion(VersionInterface $v): void
2✔
20
    {
21
        $this->versions[] = clone $v;
2✔
22
    }
23

24
    public function withVersion(VersionInterface $v): self
1✔
25
    {
26
        $new = clone $this;
1✔
27
        $new->addVersion($v);
1✔
28
        return $new;
1✔
29
    }
30

31
    /** @return int representing the counter of versions executed */
32
    public function run(): int
2✔
33
    {
34
        $currentVersion = $this->backend->getCurrentVersionId();
2✔
35
        $versionRuns = $this->backend->getVersionList();
2✔
36

37
        $indexForLastVersionExecuted = -1;
2✔
38
        foreach ($this->versions as $i => $version) {
2✔
39
            if ($version->getId() === $currentVersion) {
2✔
40
                $indexForLastVersionExecuted = $i;
1✔
41
                break;
1✔
42
            }
43

44
            if ($version->check() === false) {
2✔
45
                continue;
1✔
46
            }
47

48
            if (isset($versionRuns[$i]) && $versionRuns[$i]->versionId !== $version->getId()) {
2✔
NEW
49
                $this->logger?->warning("Inconsistent versioning, found {$version->getId()} expected {$versionRuns[$i]->versionId} as {$i}th run");
×
50
            }
51
        }
52

53
        // Move to next version
54
        $currentVersionToExecute = $indexForLastVersionExecuted + 1;
2✔
55

56
        $versionsAvailable = count($this->versions);
2✔
57

58
        // Already on latest version
59
        if ($currentVersionToExecute === $versionsAvailable) {
2✔
60
            $this->logger?->debug(
1✔
61
                'Already on latest version',
1✔
62
            );
1✔
63
            return 0;
1✔
64
        }
65

66
        // Invalid configuration
67
        if ($currentVersionToExecute > $versionsAvailable) {
2✔
NEW
68
            $this->logger?->warning('Number of versions executed exceeds available ones');
×
69
            return 0;
×
70
        }
71

72
        $counter = 0;
2✔
73
        for (; $currentVersionToExecute < $versionsAvailable; $currentVersionToExecute++) {
2✔
74
            $version = $this->versions[$currentVersionToExecute];
2✔
75
            if ($version->check() === false) {
2✔
76
                continue;
1✔
77
            }
78

79
            $entry = $this->backend->markVersionAsProcessing($version->getId());
2✔
80

81
            try {
82
                $version->run();
2✔
83
                $this->backend->markVersionAsSuccessful($entry);
2✔
84
                $counter++;
2✔
85
            } catch (Throwable $exception) {
1✔
86
                $this->logger?->warning(
1✔
87
                    "Version run failed with exception {$exception->getMessage()}",
1✔
88
                );
1✔
89

90
                $version->rollback();
1✔
91
                $this->backend->abortVersionProcessing($entry, $exception->getMessage());
1✔
92
                break;
1✔
93
            }
94
        }
95

96
        return $counter;
2✔
97
    }
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc