Completed
Push — master ( 444b50...ece018 )
by Gábor
03:00
created

ReleaseManager::release()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the egabor/composer-release-plugin package.
5
 *
6
 * (c) Gábor Egyed <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace egabor\Composer\ReleasePlugin;
13
14
use Composer\Semver\Comparator;
15
use egabor\Composer\ReleasePlugin\Util\Git;
16
17
class ReleaseManager
18
{
19
    private $config;
20
    private $git;
21
22
    public function __construct(Config $config, Git $git)
23
    {
24
        $this->config = $config;
25
        $this->git = $git;
26
    }
27
28
    public function gatFutureVersion($version, Version $latestVersion = null)
29
    {
30
        if (null === $latestVersion) {
31
            $latestVersion = $this->getLatestReleaseVersion() ?: Version::fromString('0.0.0');
32
        }
33
34
        if (VersionManipulator::supportsLevel($version)) {
35
            $futureVersion = (string) VersionManipulator::bump($version, $latestVersion);
36
        } else {
37
            $futureVersion = (string) Version::fromString($version); // validation
38
        }
39
40
        if (Comparator::lessThanOrEqualTo($futureVersion, (string) $latestVersion)) {
41
            throw new \LogicException(sprintf('The provided version "%s" should be greater than the latest released version "%s".', $futureVersion, $latestVersion));
42
        }
43
44
        return $this->config->shouldUsePrefix() ? 'v'.$futureVersion : $futureVersion;
45
    }
46
47
    public function release($futureVersion, $message = null)
48
    {
49
        $this->git->tag($futureVersion, strtr($message, ['%version%' => $futureVersion]) ?: 'Release '.$futureVersion);
50
    }
51
52
    public function releaseAndPush($futureVersion, $message = null)
53
    {
54
        $this->release($futureVersion, $message);
55
56
        $this->git->push();
57
        $this->git->pushTags();
58
    }
59
60
    public function getLatestReleaseVersion()
61
    {
62
        try {
63
            $tag = $this->git->getLatestReachableTag($this->config->getReleaseBranch());
64
        } catch (\Exception $e) {
65
            return null;
66
        }
67
68
        return Version::fromString($tag);
69
    }
70
}
71