Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,10 @@
"php qt core:version"
],
"test": "vendor/bin/phpunit --stderr --coverage-clover coverage.xml"
},
"config": {
"audit": {
"block-insecure": false
}
}
}
72 changes: 72 additions & 0 deletions shared/Commands/CommandValidationTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

/**
* 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 2.9.9
*/

namespace Shared\Commands;

use Quantum\Libraries\Validation\Validator;

/**
* Trait CommandValidationTrait
* @package Shared\Commands
*/
trait CommandValidationTrait
{

/**
* @var Validator
*/
protected $validator;

/**
* Initiates the validator
*/
protected function initValidator(): void
{
$this->validator = new Validator();
}

/**
* Validates rules
* @param array $rules
* @param array $data
* @return bool
*/
protected function validate(array $rules, array $data): bool
{
$this->validator->setRules($rules);

if (!$this->validator->isValid($data)) {
return false;
}

return true;
}

/**
* Gets the first validation error
* @return string|null
*/
public function firstError(): ?string
{
$errors = $this->validator->getErrors();

foreach ($errors as $fieldErrors) {
if (!empty($fieldErrors)) {
return $fieldErrors[0];
}
}

return null;
}
}
55 changes: 55 additions & 0 deletions shared/Commands/CommentCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,70 @@

namespace Shared\Commands;

use Quantum\Service\Exceptions\ServiceException;
use Quantum\Service\Factories\ServiceFactory;
use Quantum\Libraries\Validation\Rule;
use Quantum\Di\Exceptions\DiException;
use Shared\Services\CommentService;
use Quantum\Console\QtCommand;
use ReflectionException;

/**
* Class CommentCreateCommand
* @package Shared\Commands
*/
class CommentCreateCommand extends QtCommand
{

use CommandValidationTrait;

/**
* Command name
* @var string
*/
protected $name = 'comment:create';

/**
* Command description
* @var string
*/
protected $description = 'Allows to create a comment record';

/**
* Command help text
* @var string
*/
protected $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 $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
* @throws ServiceException
* @throws ReflectionException
*/
public function exec()
{
$this->initValidator();

$data = [
'content' => $this->getArgument('content'),
];

if (!$this->validate($this->validationRules(), $data)) {
$this->error($this->firstError() ?? 'Validation failed');
return;
}

$commentService = ServiceFactory::create(CommentService::class);

$comment = [
Expand All @@ -50,4 +90,19 @@ public function exec()

$this->info('Comment created successfully');
}

/**
* Validation rules
* @return array[]
*/
protected function validationRules(): array
{
return [
'content' => [
Rule::required(),
Rule::minLen(2),
Rule::maxLen(100),
],
];
}
}
35 changes: 35 additions & 0 deletions shared/Commands/PostCreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Quantum\Service\Exceptions\ServiceException;
use Quantum\Service\Factories\ServiceFactory;
use Quantum\Libraries\Validation\Rule;
use Quantum\Di\Exceptions\DiException;
use Shared\Services\PostService;
use Quantum\Console\QtCommand;
Expand All @@ -28,6 +29,8 @@
class PostCreateCommand extends QtCommand
{

use CommandValidationTrait;

/**
* Command name
* @var string
Expand Down Expand Up @@ -66,6 +69,18 @@ class PostCreateCommand extends QtCommand
*/
public function exec()
{
$this->initValidator();

$data = [
'title' => $this->getArgument('title'),
'content' => $this->getArgument('description'),
];

if (!$this->validate($this->validationRules(), $data)) {
$this->error($this->firstError() ?? 'Validation failed');
return;
}

$postService = ServiceFactory::get(PostService::class);

$post = [
Expand All @@ -80,4 +95,24 @@ public function exec()

$this->info('Post created successfully');
}

/**
* Validation rules
* @return array[]
*/
protected function validationRules(): array
{
return [
'title' => [
Rule::required(),
Rule::minLen(10),
Rule::maxLen(50),
],
'content' => [
Rule::required(),
Rule::minLen(10),
Rule::maxLen(1000),
],
];
}
}
34 changes: 33 additions & 1 deletion shared/Commands/PostUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.8
* @since 2.9.9
*/

namespace Shared\Commands;

use Quantum\Service\Exceptions\ServiceException;
use Quantum\Service\Factories\ServiceFactory;
use Quantum\Libraries\Validation\Rule;
use Quantum\Di\Exceptions\DiException;
use Shared\Services\PostService;
use Quantum\Console\QtCommand;
Expand All @@ -28,6 +29,8 @@
class PostUpdateCommand extends QtCommand
{

use CommandValidationTrait;

/**
* Command name
* @var string
Expand Down Expand Up @@ -72,6 +75,8 @@ class PostUpdateCommand extends QtCommand
*/
public function exec()
{
$this->initValidator();

$postService = ServiceFactory::get(PostService::class);

$postId = $this->getArgument('uuid');
Expand All @@ -83,6 +88,16 @@ public function exec()
return;
}

$data = [
'title' => $this->getOption('title') ?: $post->title,
'content' => $this->getOption('description') ?: $post->content,
];

if (!$this->validate($this->validationRules(), $data)) {
$this->error($this->firstError() ?? 'Validation failed');
return;
}

$postData = [
'title' => $this->getOption('title') ?: $post->title,
'content' => $this->getOption('description') ?: $post->content,
Expand All @@ -95,4 +110,21 @@ public function exec()
$this->info('Post updated successfully');
}

/**
* Validation rules
* @return array[]
*/
protected function validationRules(): array
{
return [
'title' => [
Rule::minLen(10),
Rule::maxLen(50),
],
'content' => [
Rule::minLen(10),
Rule::maxLen(1000),
],
];
}
}
Loading