-
Notifications
You must be signed in to change notification settings - Fork 2
finish the app #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
garak
wants to merge
1
commit into
yceruto:master
Choose a base branch
from
garak:refactoring
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Solid\App\BlogPost\Application\UseCase\EditBlogPostUseCase; | ||
| use Solid\App\BlogPost\Domain\Model\BlogPostNormalizer; | ||
| use Solid\App\BlogPost\Infrastructure\Persistence\FileStorage; | ||
| use Solid\App\BlogPost\Presentation\Controller\EditAction; | ||
| use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\JsonRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\RenderContainer; | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| $normalizer = new BlogPostNormalizer(); | ||
| $storage = new FileStorage($normalizer, __DIR__.'/../../var/data/blog_post.dat'); | ||
| $useCase = new EditBlogPostUseCase($storage); | ||
| $renderContainer = new RenderContainer([ | ||
| 'html' => new HtmlRenderer(), | ||
| 'json' => new JsonRenderer($normalizer), | ||
| ]); | ||
| $format = $_GET['format'] ?? 'html'; | ||
|
|
||
| $action = new EditAction($useCase, $renderContainer->get($format)); | ||
| $action((int) ($_GET['id'] ?? 0), $_GET['author'] ?? null, $_GET['title'] ?? null, $_GET['content'] ?? null, $format); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Solid\App\BlogPost\Application\UseCase\ListBlogPostUseCase; | ||
| use Solid\App\BlogPost\Domain\Model\BlogPostNormalizer; | ||
| use Solid\App\BlogPost\Infrastructure\Persistence\FileStorage; | ||
| use Solid\App\BlogPost\Presentation\Controller\ListAction; | ||
| use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\JsonRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\RenderContainer; | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| $normalizer = new BlogPostNormalizer(); | ||
| $storage = new FileStorage($normalizer, __DIR__.'/../../var/data/blog_post.dat'); | ||
| $useCase = new ListBlogPostUseCase($storage); | ||
| $renderContainer = new RenderContainer([ | ||
| 'html' => new HtmlRenderer(), | ||
| 'json' => new JsonRenderer($normalizer), | ||
| ]); | ||
| $format = $_GET['format'] ?? 'html'; | ||
|
|
||
| $action = new ListAction($useCase, $renderContainer->get($format)); | ||
| $action($format); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Solid\App\BlogPost\Application\UseCase\NewBlogPostUseCase; | ||
| use Solid\App\BlogPost\Domain\Model\BlogPostNormalizer; | ||
| use Solid\App\BlogPost\Infrastructure\Persistence\FileStorage; | ||
| use Solid\App\BlogPost\Presentation\Controller\NewAction; | ||
| use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\JsonRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\RenderContainer; | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| $normalizer = new BlogPostNormalizer(); | ||
| $storage = new FileStorage($normalizer, __DIR__.'/../../var/data/blog_post.dat'); | ||
| $useCase = new NewBlogPostUseCase($storage); | ||
| $renderContainer = new RenderContainer([ | ||
| 'html' => new HtmlRenderer(), | ||
| 'json' => new JsonRenderer($normalizer), | ||
| ]); | ||
| $format = $_GET['format'] ?? 'html'; | ||
|
|
||
| $action = new NewAction($useCase, $renderContainer->get($format)); | ||
| $action(random_int(0, 999999), $_GET['author'] ?? null, $_GET['title'] ?? null, $_GET['content'] ?? null, $format); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Solid\App\BlogPost\Application\UseCase\FindBlogPostUseCase; | ||
| use Solid\App\BlogPost\Domain\Model\BlogPostNormalizer; | ||
| use Solid\App\BlogPost\Infrastructure\Persistence\FileStorage; | ||
| use Solid\App\BlogPost\Presentation\Controller\ShowAction; | ||
| use Solid\App\BlogPost\Presentation\Renderer\HtmlRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\JsonRenderer; | ||
| use Solid\App\BlogPost\Presentation\Renderer\RenderContainer; | ||
|
|
||
| require __DIR__.'/../../vendor/autoload.php'; | ||
|
|
||
| $normalizer = new BlogPostNormalizer(); | ||
| $storage = new FileStorage($normalizer, __DIR__.'/../../var/data/blog_post.dat'); | ||
| $useCase = new FindBlogPostUseCase($storage); | ||
| $renderContainer = new RenderContainer([ | ||
| 'html' => new HtmlRenderer(), | ||
| 'json' => new JsonRenderer($normalizer), | ||
| ]); | ||
| $format = $_GET['format'] ?? 'html'; | ||
|
|
||
| $action = new ShowAction($useCase, $renderContainer->get($format)); | ||
| $action((int) ($_GET['id'] ?? 0), $format); |
29 changes: 29 additions & 0 deletions
29
src/App/BlogPost/Application/UseCase/EditBlogPostUseCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Application\UseCase; | ||
|
|
||
| use Solid\App\BlogPost\Domain\Model\BlogPost; | ||
| use Solid\App\BlogPost\Domain\Model\Storage; | ||
|
|
||
| final class EditBlogPostUseCase | ||
| { | ||
| public function __construct( | ||
| private readonly Storage $storage, | ||
| private readonly RandomWords $rand = new RandomWords(), | ||
| ) { | ||
| } | ||
|
|
||
| public function execute(int $id, ?string $author, ?string $title, ?string $content): BlogPost | ||
| { | ||
| $blogPost = $this->storage->get($id); | ||
| $blogPost->update( | ||
| $author ?? $this->rand->get('s'), | ||
| $title ?? $this->rand->get('m'), | ||
| $content ?? $this->rand->get('l') | ||
| ); | ||
| $this->storage->save($blogPost); | ||
|
|
||
| return $blogPost; | ||
| } | ||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
src/App/BlogPost/Application/UseCase/FindBlogPostUseCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Application\UseCase; | ||
|
|
||
| use Solid\App\BlogPost\Domain\Model\BlogPost; | ||
| use Solid\App\BlogPost\Domain\Model\Storage; | ||
|
|
||
| final class FindBlogPostUseCase | ||
| { | ||
| public function __construct(private readonly Storage $storage) | ||
| { | ||
| } | ||
|
|
||
| public function execute(int $id): BlogPost | ||
| { | ||
| return $this->storage->get($id); | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
src/App/BlogPost/Application/UseCase/ListBlogPostUseCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Application\UseCase; | ||
|
|
||
| use Solid\App\BlogPost\Domain\Model\Storage; | ||
|
|
||
| final class ListBlogPostUseCase | ||
| { | ||
| public function __construct(private readonly Storage $storage) | ||
| { | ||
| } | ||
|
|
||
| public function execute(): iterable | ||
| { | ||
| return $this->storage->getAll(); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
src/App/BlogPost/Application/UseCase/NewBlogPostUseCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Application\UseCase; | ||
|
|
||
| use Solid\App\BlogPost\Domain\Model\BlogPost; | ||
| use Solid\App\BlogPost\Domain\Model\Storage; | ||
|
|
||
| final class NewBlogPostUseCase | ||
| { | ||
| public function __construct( | ||
| private readonly Storage $storage, | ||
| private readonly RandomWords $rand = new RandomWords(), | ||
| ) { | ||
| } | ||
|
|
||
| public function execute(int $id, ?string $author, ?string $title, ?string $content): BlogPost | ||
| { | ||
| $blogPost = new BlogPost( | ||
| $id, | ||
| $author ?? $this->rand->get('s'), | ||
| $title ?? $this->rand->get('m'), | ||
| $content ?? $this->rand->get('l'), | ||
| ); | ||
| $this->storage->save($blogPost); | ||
|
|
||
| return $blogPost; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Application\UseCase; | ||
|
|
||
| final class RandomWords | ||
| { | ||
| public function get(string $size): string | ||
| { | ||
| $times = match ($size) { | ||
| 's' => 2, | ||
| 'm' => 3, | ||
| 'l' => 10, | ||
| }; | ||
| $string = ''; | ||
| for ($i = 0; $i < $times; ++$i) { | ||
| $string .= $this->randWord().' '; | ||
| } | ||
|
|
||
| return trim($string); | ||
| } | ||
|
|
||
| private function randWord(): string | ||
| { | ||
| $word = array_merge(range('a', 'z'), range('A', 'Z')); | ||
| shuffle($word); | ||
| $length = random_int(7, 12); | ||
|
|
||
| return substr(implode($word), 0, $length); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Domain\Model; | ||
|
|
||
| use DateTimeImmutable; | ||
|
|
||
| final class BlogPost | ||
| { | ||
| private DateTimeImmutable $createdAt; | ||
| private ?DateTimeImmutable $updatedAt = null; | ||
|
|
||
| public function __construct( | ||
| private int $id, | ||
| private string $author, | ||
| private string $title, | ||
| private string $content, | ||
| ) { | ||
| $this->createdAt = new DateTimeImmutable(); | ||
| } | ||
|
|
||
| public function update( | ||
| ?string $author = null, | ||
| ?string $title = null, | ||
| ?string $content = null, | ||
| ): void { | ||
| $this->author = $author ?? $this->author; | ||
| $this->title = $title ?? $this->title; | ||
| $this->content = $content ?? $this->content; | ||
| $this->updatedAt = new DateTimeImmutable(); | ||
| } | ||
|
|
||
| public function getId(): int | ||
| { | ||
| return $this->id; | ||
| } | ||
|
|
||
| public function getAuthor(): string | ||
| { | ||
| return $this->author; | ||
| } | ||
|
|
||
| public function getTitle(): string | ||
| { | ||
| return $this->title; | ||
| } | ||
|
|
||
| public function getContent(): string | ||
| { | ||
| return $this->content; | ||
| } | ||
|
|
||
| public function getCreatedAt(): DateTimeImmutable | ||
| { | ||
| return $this->createdAt; | ||
| } | ||
|
|
||
| public function getUpdatedAt(): ?DateTimeImmutable | ||
| { | ||
| return $this->updatedAt; | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Domain\Model; | ||
|
|
||
| use DateTimeImmutable; | ||
|
|
||
| final class BlogPostNormalizer implements Normalizer | ||
| { | ||
| public function normalize(BlogPost $blogPost): array | ||
| { | ||
| return [ | ||
| 'id' => $blogPost->getId(), | ||
| 'author' => $blogPost->getAuthor(), | ||
| 'title' => $blogPost->getTitle(), | ||
| 'content' => $blogPost->getContent(), | ||
| 'created_at' => $blogPost->getCreatedAt()->format('Y-m-d H:i:s'), | ||
| 'updated_at' => $blogPost->getUpdatedAt()?->format('Y-m-d H:i:s'), | ||
| ]; | ||
| } | ||
|
|
||
| public function denormalize(array $data): BlogPost | ||
| { | ||
| $blogPost = new BlogPost($data['id'], $data['author'], $data['title'], $data['content']); | ||
| $this->setTimestampProperties($blogPost, $data['created_at'], $data['updated_at']); | ||
|
|
||
| return $blogPost; | ||
| } | ||
|
|
||
| private function setTimestampProperties(BlogPost $blogPost, string $created, ?string $updated): void | ||
| { | ||
| $createdAt = new \ReflectionProperty($blogPost, 'createdAt'); | ||
| $createdAt->setAccessible(true); | ||
| $createdAt->setValue($blogPost, new DateTimeImmutable($created)); | ||
| if (null !== $updated) { | ||
| $updatedAt = new \ReflectionProperty($blogPost, 'updatedAt'); | ||
| $updatedAt->setAccessible(true); | ||
| $updatedAt->setValue($blogPost, new DateTimeImmutable($updated)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Domain\Model; | ||
|
|
||
| use DateTimeImmutable; | ||
|
|
||
| interface Normalizer | ||
| { | ||
| /** | ||
| * @return array{id: int, title: string, content: string, author: string, createdAt: DateTimeImmutable, updatedAt: ?DateTimeImmutable} | ||
| */ | ||
| public function normalize(BlogPost $blogPost): array; | ||
|
|
||
| /** | ||
| * @param array{id: int, title: string, content: string, author: string, createdAt: DateTimeImmutable, updatedAt: ?DateTimeImmutable} $data | ||
| */ | ||
| public function denormalize(array $data): BlogPost; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| namespace Solid\App\BlogPost\Domain\Model; | ||
|
|
||
| interface Storage | ||
| { | ||
| public function get(int $id): BlogPost; | ||
|
|
||
| /** | ||
| * @return iterable<int, BlogPost> | ||
| */ | ||
| public function getAll(): iterable; | ||
|
|
||
| public function save(BlogPost $blogPost): void; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vs
[SRP] It looks simple but in my opinion, is not a good thing when your logic starts growing or you have to do something specific for each value (like domain validations).