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
4 changes: 2 additions & 2 deletions run.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use solid\CsvDataImporter;
use solid\Loader;
use solid\Repository;
use solid\Repository\ImportedRepository;

require 'vendor/autoload.php';

Expand All @@ -11,7 +11,7 @@
'solid',
'local'
);
$repository = new Repository($db);
$repository = new ImportedRepository($db);
$loader = new Loader('var/import/data.csv');

$importer = new CsvDataImporter($repository, $loader);
Expand Down
6 changes: 4 additions & 2 deletions src/CsvDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

namespace solid;

use solid\Repository\AbstractRepository;

class CsvDataImporter
{
private Repository $repository;
private AbstractRepository $repository;
private Loader $loader;

public function __construct(Repository $repository, Loader $loader)
public function __construct(AbstractRepository $repository, Loader $loader)
{
$this->repository = $repository;
$this->loader = $loader;
Expand Down
23 changes: 10 additions & 13 deletions src/Repository.php → src/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@

declare(strict_types=1);

namespace solid;
namespace solid\Repository;

use PDO;
use PDOException;

class Repository
abstract class AbstractRepository
{
private PDO $db;
protected PDO $db;

public function __construct(PDO $db)
{
$this->db = $db;
}

abstract protected function beforeInserts(): void;
abstract protected function insert(array $record): void;

abstract public function getCount(): int;

public function importData(array $records): void
{
try {
$this->db->beginTransaction();

$this->db->exec('DELETE FROM imported');
$this->beforeInserts();

foreach ($records as $record) {
$this->db->prepare('INSERT INTO imported VALUES (?, ?, ?)')
->execute($record);
$this->insert($record);
}

$this->db->commit();
Expand All @@ -34,11 +38,4 @@ public function importData(array $records): void
throw $e;
}
}

public function getCount(): int
{
$data = $this->db->query('SELECT COUNT(*) AS nb FROM imported')->fetch();

return (int) $data['nb'];
}
}
28 changes: 28 additions & 0 deletions src/Repository/ImportedRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace solid\Repository;

use solid\Repository\AbstractRepository;

class ImportedRepository extends AbstractRepository
{
protected function beforeInserts(): void
{
$this->db->exec('DELETE FROM imported');
}

protected function insert(array $record): void
{
$this->db->prepare('INSERT INTO imported VALUES (?, ?, ?)')
->execute($record);
}

public function getCount(): int
{
$data = $this->db->query('SELECT COUNT(*) AS nb FROM imported')->fetch();

return (int) $data['nb'];
}
}
28 changes: 28 additions & 0 deletions src/Repository/UsersRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace solid\Repository;

use solid\Repository\AbstractRepository;

class UsersRepository extends AbstractRepository
{
protected function beforeInserts(): void
{
$this->db->exec('DELETE FROM users');
}

protected function insert(array $record): void
{
$this->db->prepare('INSERT INTO users VALUES (?, ?, ?)')
->execute($record);
}

public function getCount(): int
{
$data = $this->db->query('SELECT COUNT(*) AS nb FROM users')->fetch();

return (int) $data['nb'];
}
}
17 changes: 15 additions & 2 deletions tests/CsvDataImporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,32 @@
use PHPUnit\Framework\TestCase;
use solid\CsvDataImporter;
use solid\Loader;
use solid\Repository;
use solid\Repository\ImportedRepository;
use solid\Repository\UsersRepository;

class CsvDataImporterTest extends TestCase
{
public function testImport(): void
{
$db = TestsFacility::createDb();
$repository = new Repository($db);
$repository = new ImportedRepository($db);
$loader = new Loader('var/import/data.csv');

$importer = new CsvDataImporter($repository, $loader);
$importer->import();

$this->assertSame(3, $repository->getCount());
}

public function testUsers(): void
{
$db = TestsFacility::createDb();
$repository = new UsersRepository($db);
$loader = new Loader('var/import/users.csv');

$importer = new CsvDataImporter($repository, $loader);
$importer->import();

$this->assertSame(2, $repository->getCount());
}
}
6 changes: 6 additions & 0 deletions tests/TestsFacility.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,11 @@ private static function loadFixtures(PDO $db): void
champ2 TINYTEXT NOT NULL,
champ3 TINYTEXT NOT NULL
)');

$db->exec('CREATE TABLE users (
id TINYTEXT NOT NULL,
login TINYTEXT NOT NULL,
fullname TINYTEXT NOT NULL
)');
}
}
3 changes: 1 addition & 2 deletions var/import/users.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
1;toto;Toto
2;titi;Titi
3;pouet;Pouet
2;titi;Titi