Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.75% |
15 / 16 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| VersionCheckNotifier | |
93.75% |
15 / 16 |
|
66.67% |
2 / 3 |
8.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| notify | |
91.67% |
11 / 12 |
|
0.00% |
0 / 1 |
5.01 | |||
| shouldSkipVersionCheck | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * Fast Forward Development Tools for PHP projects. |
| 7 | * |
| 8 | * This file is part of fast-forward/dev-tools project. |
| 9 | * |
| 10 | * @author Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 11 | * @license https://opensource.org/licenses/MIT MIT License |
| 12 | * |
| 13 | * @see https://github.com/php-fast-forward/ |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward/dev-tools/issues |
| 16 | * @see https://php-fast-forward.github.io/dev-tools/ |
| 17 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 18 | */ |
| 19 | |
| 20 | namespace FastForward\DevTools\SelfUpdate; |
| 21 | |
| 22 | use FastForward\DevTools\Environment\RuntimeEnvironmentInterface; |
| 23 | use Throwable; |
| 24 | use Symfony\Component\Console\Output\OutputInterface; |
| 25 | |
| 26 | /** |
| 27 | * Emits update warnings while ensuring version checks never block the requested command. |
| 28 | */ |
| 29 | class VersionCheckNotifier implements VersionCheckNotifierInterface |
| 30 | { |
| 31 | /** |
| 32 | * @param VersionCheckerInterface $versionChecker the checker used to resolve latest release metadata |
| 33 | * @param RuntimeEnvironmentInterface $environment resolves runtime environment capabilities |
| 34 | */ |
| 35 | public function __construct( |
| 36 | private VersionCheckerInterface $versionChecker, |
| 37 | private RuntimeEnvironmentInterface $environment, |
| 38 | ) {} |
| 39 | |
| 40 | /** |
| 41 | * Warns when a newer stable DevTools version is available. |
| 42 | * |
| 43 | * @param OutputInterface $output the command output receiving a non-blocking warning |
| 44 | */ |
| 45 | public function notify(OutputInterface $output): void |
| 46 | { |
| 47 | if ($this->shouldSkipVersionCheck()) { |
| 48 | return; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | $result = $this->versionChecker->check(); |
| 53 | } catch (Throwable) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | if (! $result instanceof VersionCheckResult || ! $result->isOutdated()) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | $output->writeln(\sprintf( |
| 62 | '<comment>DevTools %s is available; current version is %s. Run "dev-tools self-update" to update.</comment>', |
| 63 | $result->getLatestVersion(), |
| 64 | $result->getCurrentVersion(), |
| 65 | )); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns whether DevTools SHOULD skip the best-effort version check. |
| 70 | */ |
| 71 | private function shouldSkipVersionCheck(): bool |
| 72 | { |
| 73 | if ($this->environment->isCi()) { |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | return $this->environment->isEnabled('FAST_FORWARD_SKIP_VERSION_CHECK'); |
| 78 | } |
| 79 | } |