Lines 71.42% 5 / 7
Functions and Methods 50.00% 1 / 2
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RunningReleaseDetector 71.42% 5 / 7 50.00% 1 / 2 5.58 0.00% 0 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 findRunningCommit 66.66% 4 / 6 0.00% 0 / 1 4.59
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Broker\Deploy;
6
7use Stewart\Runtime\Config\ProjectRoot;
8use Stewart\Runtime\Exception\DeployException;
9
10final readonly class RunningReleaseDetector
11{
12    private const string RELEASES_DIRECTORY = 'releases';
13
14    private const string CURRENT_RELEASE_LINK = 'current';
15
16    public function __construct(private ProjectRoot $projectRoot) {}
17
18    // The entrypoint runs a release from <app>/releases/<commit>, with <app>/current pointing at it.
19    public function findRunningCommit(): ?CommitId
20    {
21        $releasesDirectory = \dirname($this->projectRoot->path);
22
23        if (basename($releasesDirectory) !== self::RELEASES_DIRECTORY || !is_link(\dirname($releasesDirectory) . '/' . self::CURRENT_RELEASE_LINK)) {
24            return null;
25        }
26
27        try {
28            return new CommitId(basename($this->projectRoot->path));
29        } catch (DeployException) {
30            return null;
31        }
32    }
33}