forked from jkapuscik2/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateListing.php
More file actions
46 lines (40 loc) · 1.41 KB
/
CreateListing.php
File metadata and controls
46 lines (40 loc) · 1.41 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
45
46
<?php
namespace Behavioral\Command;
use LengthException;
final class CreateListing implements Command
{
private const MIN_TITLE_LENGTH = 10;
private const MIN_CONTENT_LENGTH = 15;
private const MIN_AUTHOR_LENGTH = 5;
private $repository;
private $title;
private $content;
private $author;
public function __construct(ListingRepository $repository, string $title, string $content, string $author)
{
$this->repository = $repository;
$this->title = $title;
$this->content = $content;
$this->author = $author;
}
private function validate(): void
{
if (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 (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 (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);
}
}