1717use Symfony \Component \Console \Event \ConsoleCommandEvent ;
1818use Symfony \Component \EventDispatcher \EventSubscriberInterface ;
1919use Ymir \Cli \Application ;
20+ use Ymir \Cli \CliConfiguration ;
2021use Ymir \Cli \GitHubClient ;
2122
2223class VersionCheckSubscriber implements EventSubscriberInterface
2324{
25+ /**
26+ * The global Ymir CLI configuration.
27+ *
28+ * @var CliConfiguration
29+ */
30+ private $ cliConfiguration ;
31+
2432 /**
2533 * The API client that interacts with the GitHub API.
2634 *
@@ -38,8 +46,9 @@ class VersionCheckSubscriber implements EventSubscriberInterface
3846 /**
3947 * Constructor.
4048 */
41- public function __construct (GitHubClient $ gitHubClient , string $ version )
49+ public function __construct (CliConfiguration $ cliConfiguration , GitHubClient $ gitHubClient , string $ version )
4250 {
51+ $ this ->cliConfiguration = $ cliConfiguration ;
4352 $ this ->gitHubClient = $ gitHubClient ;
4453 $ this ->version = $ version ;
4554 }
@@ -59,9 +68,14 @@ public static function getSubscribedEvents()
5968 */
6069 public function onConsoleCommand (ConsoleCommandEvent $ event )
6170 {
62- $ latestVersion = ltrim ($ this ->gitHubClient ->getTags ('ymirapp/cli ' )->pluck ('name ' )->first (), 'v ' );
71+ $ time = time ();
72+
73+ if ($ time - $ this ->cliConfiguration ->getGitHubLastCheckedTimestamp () > 3600 ) {
74+ $ this ->cliConfiguration ->setGitHubCliVersion (ltrim ($ this ->gitHubClient ->getTags ('ymirapp/cli ' )->pluck ('name ' )->first (), 'v ' ));
75+ $ this ->cliConfiguration ->setGitHubLastCheckedTimestamp ($ time );
76+ }
6377
64- if (version_compare ($ latestVersion , $ this ->version , '> ' )) {
78+ if (version_compare ($ this -> cliConfiguration -> getGitHubCliVersion () , $ this ->version , '> ' )) {
6579 $ event ->getOutput ()->writeln ('<comment>A new version of the Ymir CLI is available</comment> ' );
6680 }
6781 }
0 commit comments