forked from softberg/quantum-php-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommentCreateCommand.php
More file actions
106 lines (90 loc) · 2.54 KB
/
CommentCreateCommand.php
File metadata and controls
106 lines (90 loc) · 2.54 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
/**
* Quantum PHP Framework
*
* An open source software development framework for PHP
*
* @package Quantum
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 3.0.0
*/
namespace Shared\Commands;
use Quantum\Service\Exceptions\ServiceException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Di\Exceptions\DiException;
use Shared\Services\CommentService;
use Quantum\Console\QtCommand;
use Quantum\Validation\Rule;
use Shared\DTOs\CommentDTO;
use ReflectionException;
/**
* Class CommentCreateCommand
* @package Shared\Commands
*/
class CommentCreateCommand extends QtCommand
{
use CommandValidationTrait;
/**
* Command name
* @var string|null
*/
protected ?string $name = 'comment:create';
/**
* Command description
* @var string|null
*/
protected ?string $description = 'Allows to create a comment record';
/**
* Command help text
* @var string|null
*/
protected ?string $help = 'Use the following format to create a comment record:' . PHP_EOL . 'php qt comment:create `Post UUID` `User UUID` `Content`';
/**
* Command arguments
* @var array[]
*/
protected array $args = [
['post_uuid', 'required', 'The post uuid the comment belongs to'],
['user_uuid', 'required', 'The user uuid who writes the comment'],
['content', 'required', 'Comment text'],
];
/**
* Executes the command
* @throws DiException|ServiceException|BaseException|ReflectionException
*/
public function exec(): void
{
$this->initValidator();
$data = [
'content' => $this->getArgument('content'),
];
if (!$this->validate($this->validationRules(), $data)) {
$this->error($this->firstError() ?? 'Validation failed');
return;
}
$commentDto = new CommentDTO(
$this->getArgument('post_uuid'),
$this->getArgument('user_uuid'),
trim($this->getArgument('content'))
);
service(CommentService::class)->addComment($commentDto);
$this->info('Comment created successfully');
}
/**
* Validation rules
* @return array[]
*/
protected function validationRules(): array
{
return [
'content' => [
Rule::required(),
Rule::minLen(2),
Rule::maxLen(100),
],
];
}
}