Skip to content
This repository was archived by the owner on Aug 2, 2025. It is now read-only.

Commit 51d485a

Browse files
committed
Save last update date
1 parent 2f9d496 commit 51d485a

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

Makefile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ help: ## Display this help
55
.PHONY: init
66
init: ## Init database
77
@rm -f var/db.sqlite
8-
@sqlite3 -line var/db.sqlite 'CREATE TABLE php_release(version VARCHAR(15) PRIMARY KEY, release_date DATE NOT NULL);'
9-
@sqlite3 -line var/db.sqlite 'CREATE TABLE php_version(version VARCHAR(15) PRIMARY KEY, last_release VARCHAR(15) NOT NULL, initial_release_date DATE NOT NULL, active_support_until DATE, end_of_life_date DATE NOT NULL);'
8+
@sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS php_release(version VARCHAR(15) PRIMARY KEY, release_date DATE NOT NULL);'
9+
@sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS php_version(version VARCHAR(15) PRIMARY KEY, last_release VARCHAR(15) NOT NULL, initial_release_date DATE NOT NULL, active_support_until DATE, end_of_life_date DATE NOT NULL);'
10+
@sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS last_update(last_update DATETIME NOT NULL);'
1011

1112
.PHONY: clean
1213
clean: ## Clean project files
@@ -23,9 +24,10 @@ serve: ## Run project through docker-compose
2324

2425
ifeq (,$(wildcard var/db.sqlite))
2526
@echo "--> Initialize database"
26-
@docker-compose exec fpm sqlite3 -line var/db.sqlite 'CREATE TABLE php_release(version VARCHAR(15) PRIMARY KEY, release_date DATE NOT NULL);'
27-
@docker-compose exec fpm sqlite3 -line var/db.sqlite 'CREATE TABLE php_version(version VARCHAR(15) PRIMARY KEY, last_release VARCHAR(15) NOT NULL, initial_release_date DATE NOT NULL, active_support_until DATE, end_of_life_date DATE NOT NULL);'
27+
@docker-compose exec fpm sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS php_release(version VARCHAR(15) PRIMARY KEY, release_date DATE NOT NULL);'
28+
@docker-compose exec fpm sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS php_version(version VARCHAR(15) PRIMARY KEY, last_release VARCHAR(15) NOT NULL, initial_release_date DATE NOT NULL, active_support_until DATE, end_of_life_date DATE NOT NULL);'
29+
@docker-compose exec fpm sqlite3 -line var/db.sqlite 'CREATE TABLE IF NOT EXISTS last_update(last_update DATETIME NOT NULL);'
2830

29-
@echo "--> synchronize data"
31+
@echo "--> Synchronize data"
3032
@docker-compose exec fpm php bin/console synchronize
3133
endif

src/Clock.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App;
6+
7+
use DateTimeImmutable;
8+
9+
class Clock
10+
{
11+
public function now(): DateTimeImmutable
12+
{
13+
return new DateTimeImmutable('now');
14+
}
15+
}

src/Command/SynchronizeCommand.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
namespace App\Command;
66

77
use App\{
8+
Clock,
89
PhpVersionFetcher,
10+
Repository\LastUpdateRepository,
911
Repository\PhpReleaseRepository,
1012
Repository\PhpVersionRepository,
1113
};
@@ -18,6 +20,8 @@
1820
final class SynchronizeCommand extends Command
1921
{
2022
public function __construct(
23+
private Clock $clock,
24+
private LastUpdateRepository $lastUpdateRepository,
2125
private PhpReleaseRepository $releaseRepository,
2226
private PhpVersionRepository $versionRepository,
2327
private PhpVersionFetcher $fetcher,
@@ -27,18 +31,23 @@ public function __construct(
2731

2832
protected function execute(InputInterface $input, OutputInterface $output): int
2933
{
30-
$output->writeln('<info>Process current versions</info>');
34+
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
35+
$output->writeln("<info>[$currentTime] Process current versions</info>");
3136
$currents = $this->fetcher->currents();
3237
$this->versionRepository->save(...$currents);
3338

34-
$output->writeln('<info>Process unmaintened versions</info>');
39+
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
40+
$output->writeln("<info>[$currentTime] Process unmaintened versions</info>");
3541
$eol = $this->fetcher->eol();
3642
$this->versionRepository->save(...$eol);
3743

38-
$output->writeln('<info>Process releases versions</info>');
44+
$currentTime = $this->clock->now()->format('Y-m-d H:i:s');
45+
$output->writeln("<info>[$currentTime] Process releases versions</info>");
3946
$releases = $this->fetcher->releases();
4047
$this->releaseRepository->save(...$releases);
4148

49+
$this->lastUpdateRepository->save();
50+
4251
return 0;
4352
}
4453
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Repository;
6+
7+
use App\Clock;
8+
use DateTimeImmutable;
9+
use PDO;
10+
use PDOException;
11+
12+
final class LastUpdateRepository
13+
{
14+
public function __construct(
15+
private PDO $db,
16+
private Clock $clock,
17+
) {
18+
}
19+
20+
public function save(): void
21+
{
22+
$this->db->beginTransaction();
23+
24+
try {
25+
$this->db->exec('DELETE FROM last_update');
26+
27+
$stmt = $this->db->prepare('INSERT INTO last_update VALUES (:date)');
28+
$stmt->bindValue('date', $this->clock->now()->format('Y-m-d H:i:s'));
29+
$stmt->execute();
30+
31+
$this->db->commit();
32+
} catch (PDOException $e) {
33+
$this->db->rollBack();
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)