|
5 | 5 | namespace App\Repository; |
6 | 6 |
|
7 | 7 | use App\Model\PhpVersion; |
8 | | -use DateTimeImmutable; |
9 | | -use PDO; |
10 | | -use PDOException; |
11 | 8 |
|
12 | | -final class PhpVersionRepository |
| 9 | +interface PhpVersionRepository |
13 | 10 | { |
14 | | - public function __construct( |
15 | | - private PDO $db, |
16 | | - ) { |
17 | | - } |
18 | | - |
19 | 11 | /** |
20 | 12 | * @return PhpVersion[] |
21 | 13 | */ |
22 | | - public function all(): iterable |
23 | | - { |
24 | | - $stmt = $this->db->query('SELECT * FROM php_version ORDER BY end_of_life_date DESC'); |
25 | | - while ($row = $stmt->fetch()) { |
26 | | - yield $this->createObjectInstance($row); |
27 | | - } |
28 | | - } |
| 14 | + public function all(): iterable; |
29 | 15 |
|
30 | 16 | /** |
31 | 17 | * @return PhpVersion[] |
32 | 18 | */ |
33 | | - public function maintenedVersions(): iterable |
34 | | - { |
35 | | - $stmt = $this->db->query('SELECT * FROM php_version WHERE end_of_life_date > DATE("now") ORDER BY end_of_life_date DESC'); |
36 | | - while ($row = $stmt->fetch()) { |
37 | | - yield $this->createObjectInstance($row); |
38 | | - } |
39 | | - } |
| 19 | + public function maintenedVersions(): iterable; |
40 | 20 |
|
41 | 21 | /** |
42 | 22 | * @return PhpVersion[] |
43 | 23 | */ |
44 | | - public function unmaintenedVersions(): iterable |
45 | | - { |
46 | | - $stmt = $this->db->query('SELECT * FROM php_version WHERE end_of_life_date <= DATE("now") ORDER BY end_of_life_date DESC'); |
47 | | - while ($row = $stmt->fetch()) { |
48 | | - yield $this->createObjectInstance($row); |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - public function save(PhpVersion ...$versions): void |
53 | | - { |
54 | | - $this->db->beginTransaction(); |
55 | | - |
56 | | - try { |
57 | | - foreach ($versions as $phpVersion) { |
58 | | - $stmt = $this->db->prepare('INSERT OR REPLACE INTO php_version(version, last_release, initial_release_date, end_of_life_date, active_support_until) VALUES(:version, :last_release, :initial_release, :end_of_life_date, :active_support_until)'); |
59 | | - $stmt->bindValue('version', $phpVersion->getVersion()); |
60 | | - $stmt->bindValue('last_release', $phpVersion->getLastRelease()); |
61 | | - $stmt->bindValue('initial_release', $phpVersion->getInitialRelease()->format('Y-m-d')); |
62 | | - $stmt->bindValue('active_support_until', $phpVersion->getActiveSupportUntil()?->format('Y-m-d')); |
63 | | - $stmt->bindValue('end_of_life_date', $phpVersion->getEndOfLife()->format('Y-m-d')); |
64 | | - $stmt->execute(); |
65 | | - } |
66 | | - |
67 | | - $this->db->commit(); |
68 | | - } catch (PDOException $e) { |
69 | | - $this->db->rollBack(); |
70 | | - } |
71 | | - } |
| 24 | + public function unmaintenedVersions(): iterable; |
72 | 25 |
|
73 | | - private function createObjectInstance(array $data): PhpVersion |
74 | | - { |
75 | | - return new PhpVersion( |
76 | | - version: $data['version'], |
77 | | - lastRelease: $data['last_release'], |
78 | | - initialRelease: DateTimeImmutable::createFromFormat('Y-m-d', $data['initial_release_date']), |
79 | | - endOfLife: DateTimeImmutable::createFromFormat('Y-m-d', $data['end_of_life_date']), |
80 | | - activeSupportUntil: $data['active_support_until'] ? DateTimeImmutable::createFromFormat('Y-m-d', $data['active_support_until']) : null, |
81 | | - ); |
82 | | - } |
| 26 | + public function save(PhpVersion ...$versions): void; |
83 | 27 | } |
0 commit comments