add 'adding content' chapter

This commit is contained in:
lubiana 2022-04-05 19:09:40 +02:00
parent fed49011bd
commit 10aa69d6d4
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
20 changed files with 884 additions and 251 deletions

View file

@ -1,7 +1,4 @@
<?php
declare(strict_types=1);
<?php declare(strict_types=1);
namespace Lubian\NoFramework\Repository;
@ -10,15 +7,17 @@ use Doctrine\ORM\EntityRepository;
use Lubian\NoFramework\Exception\NotFound;
use Lubian\NoFramework\Model\MarkdownPage;
use function random_int;
use function usleep;
final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
{
/**
* @var EntityRepository<MarkdownPage>
*/
/** @var EntityRepository<MarkdownPage> */
private EntityRepository $repo;
public function __construct(
private EntityManagerInterface $entityManager
){
) {
$this->repo = $this->entityManager->getRepository(MarkdownPage::class);
}
@ -27,15 +26,15 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
*/
public function all(): array
{
usleep(rand(500, 1500) * 1000);
usleep(random_int(500, 1500) * 1000);
return $this->repo->findAll();
}
public function byId(int $id): MarkdownPage
{
usleep(rand(500, 1500) * 1000);
usleep(random_int(500, 1500) * 1000);
$page = $this->repo->findOneBy(['id' => $id]);
if (!$page instanceof MarkdownPage){
if (! $page instanceof MarkdownPage) {
throw new NotFound;
}
return $page;
@ -43,9 +42,9 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
public function byTitle(string $title): MarkdownPage
{
usleep(rand(500, 1500) * 1000);
usleep(random_int(500, 1500) * 1000);
$page = $this->repo->findOneBy(['title' => $title]);
if (!$page instanceof MarkdownPage){
if (! $page instanceof MarkdownPage) {
throw new NotFound;
}
return $page;
@ -57,4 +56,4 @@ final class DoctrineMarkdownPageRepo implements MarkdownPageRepo
$this->entityManager->flush();
return $page;
}
}
}

View file

@ -13,6 +13,7 @@ use function count;
use function file_get_contents;
use function glob;
use function is_array;
use function random_int;
use function str_replace;
use function substr;
use function usleep;
@ -31,7 +32,7 @@ final class MarkdownPageFilesystem implements MarkdownPageRepo
$fileNames = glob($this->dataPath . '*.md');
assert(is_array($fileNames));
return array_map(function (string $name): MarkdownPage {
usleep(rand(200, 500) * 1000);
usleep(random_int(200, 500) * 1000);
$content = file_get_contents($name);
$name = str_replace($this->dataPath, '', $name);
$name = str_replace('.md', '', $name);