-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathCreateListing.php
More file actions
44 lines (38 loc) · 1.33 KB
/
Copy pathCreateListing.php
File metadata and controls
44 lines (38 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace App\Behavioral\Command;
use LengthException;
final readonly class CreateListing implements Command
{
private const MIN_TITLE_LENGTH = 10;
private const MIN_CONTENT_LENGTH = 15;
private const MIN_AUTHOR_LENGTH = 5;
public function __construct(private ListingRepository $repository, private string $title, private string $content, private string $author)
{
}
private function validate(): void
{
if (mb_strlen($this->title) < self::MIN_TITLE_LENGTH) {
throw new LengthException(sprintf(
"Title is too short. Must be at least %d characters",
self::MIN_TITLE_LENGTH
));
}
if (mb_strlen($this->content) < self::MIN_CONTENT_LENGTH) {
throw new LengthException(sprintf(
"Content is too short. Must be at least %d characters",
self::MIN_CONTENT_LENGTH
));
}
if (mb_strlen($this->author) < self::MIN_AUTHOR_LENGTH) {
throw new LengthException(sprintf(
"Author name is too short. Must be at least %d characters",
self::MIN_AUTHOR_LENGTH
));
}
}
public function handle(): void
{
$this->validate();
$this->repository->create($this->title, $this->content, $this->author);
}
}