Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Contract/ImportDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace solid\Contract;

interface ImportDataInterface
{
public function importData(array $records): void;
}
10 changes: 10 additions & 0 deletions src/Contract/UserListInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace solid\Contract;

interface UserListInterface
{
public function getActives(): array;
}
10 changes: 5 additions & 5 deletions src/DataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace solid;

use solid\Contract\ImportDataInterface;
use solid\Loader\CsvLoader;
use solid\Repository\AbstractRepository;

class DataImporter
{
private AbstractRepository $repository;
private ImportDataInterface $importer;
private CsvLoader $loader;

public function __construct(AbstractRepository $repository, CsvLoader $loader)
public function __construct(ImportDataInterface $importer, CsvLoader $loader)
{
$this->repository = $repository;
$this->importer = $importer;
$this->loader = $loader;
}

public function import(): void
{
$records = $this->loader->loadFile();
$this->repository->importData($records);
$this->importer->importData($records);
}
}
31 changes: 31 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace solid;

use solid\Contract\UserListInterface;

class Mailer
{
private UserListInterface $userList;

public function __construct(UserListInterface $userList)
{
$this->userList = $userList;
}

public function send(): int
{
$users = $this->userList->getActives();
$mails = 0;

foreach ($users as $user) {
// ...

$mails++;
}

return $mails;
}
}
3 changes: 2 additions & 1 deletion src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@

use PDO;
use PDOException;
use solid\Contract\ImportDataInterface;

abstract class AbstractRepository
abstract class AbstractRepository implements ImportDataInterface
{
protected PDO $db;

Expand Down
10 changes: 8 additions & 2 deletions src/Repository/UsersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

Expand All @@ -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();
}
}
23 changes: 23 additions & 0 deletions tests/MailerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace tests;

use PHPUnit\Framework\TestCase;
use solid\Mailer;
use solid\Repository\UsersRepository;

class MailerTest extends TestCase
{
public function testMailer(): void
{
$mailer = new Mailer(
new UsersRepository(TestsFacility::createDb())
);

$mailsSent = $mailer->send();

$this->assertSame(2, $mailsSent);
}
}
3 changes: 2 additions & 1 deletion tests/TestsFacility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
)');
}
}