diff --git a/src/Contract/ImportDataInterface.php b/src/Contract/ImportDataInterface.php new file mode 100644 index 0000000..406ed76 --- /dev/null +++ b/src/Contract/ImportDataInterface.php @@ -0,0 +1,10 @@ +repository = $repository; + $this->importer = $importer; $this->loader = $loader; } public function import(): void { $records = $this->loader->loadFile(); - $this->repository->importData($records); + $this->importer->importData($records); } } diff --git a/src/Mailer.php b/src/Mailer.php new file mode 100644 index 0000000..e822686 --- /dev/null +++ b/src/Mailer.php @@ -0,0 +1,31 @@ +userList = $userList; + } + + public function send(): int + { + $users = $this->userList->getActives(); + $mails = 0; + + foreach ($users as $user) { + // ... + + $mails++; + } + + return $mails; + } +} diff --git a/src/Repository/AbstractRepository.php b/src/Repository/AbstractRepository.php index aaf442b..4601d11 100644 --- a/src/Repository/AbstractRepository.php +++ b/src/Repository/AbstractRepository.php @@ -6,8 +6,9 @@ use PDO; use PDOException; +use solid\Contract\ImportDataInterface; -abstract class AbstractRepository +abstract class AbstractRepository implements ImportDataInterface { protected PDO $db; diff --git a/src/Repository/UsersRepository.php b/src/Repository/UsersRepository.php index d612a52..a583d2b 100644 --- a/src/Repository/UsersRepository.php +++ b/src/Repository/UsersRepository.php @@ -4,9 +4,10 @@ namespace solid\Repository; +use solid\Contract\UserListInterface; use solid\Repository\AbstractRepository; -class UsersRepository extends AbstractRepository +class UsersRepository extends AbstractRepository implements UserListInterface { protected function beforeInserts(): void { @@ -15,7 +16,7 @@ protected function beforeInserts(): void protected function insert(array $record): void { - $this->db->prepare('INSERT INTO users VALUES (?, ?, ?)') + $this->db->prepare('INSERT INTO users (id, login, fullname) VALUES (?, ?, ?)') ->execute($record); } @@ -25,4 +26,9 @@ public function getCount(): int return (int) $data['nb']; } + + public function getActives(): array + { + return $this->db->query('SELECT * FROM users WHERE active = 1')->fetchAll(); + } } diff --git a/tests/MailerTest.php b/tests/MailerTest.php new file mode 100644 index 0000000..a0b45a5 --- /dev/null +++ b/tests/MailerTest.php @@ -0,0 +1,23 @@ +send(); + + $this->assertSame(2, $mailsSent); + } +} diff --git a/tests/TestsFacility.php b/tests/TestsFacility.php index 7b1ca6f..0fc6d94 100644 --- a/tests/TestsFacility.php +++ b/tests/TestsFacility.php @@ -35,7 +35,8 @@ private static function loadFixtures(PDO $db): void $db->exec('CREATE TABLE users ( id TINYTEXT NOT NULL, login TINYTEXT NOT NULL, - fullname TINYTEXT NOT NULL + fullname TINYTEXT NOT NULL, + active TINYINT NOT NULL DEFAULT 1 )'); } }