Skip to content

Commit

Permalink
refactor: extract tag count validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideIadeluca committed Nov 19, 2024
1 parent 19ffca7 commit 696f6fd
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 9 deletions.
35 changes: 26 additions & 9 deletions extensions/tags/src/Listener/SaveTagsToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\Tags\Event\DiscussionWasTagged;
use Flarum\Tags\Tag;
use Flarum\Tags\TagCountValidator;
use Flarum\User\Exception\PermissionDeniedException;
use Illuminate\Contracts\Validation\Factory;
use Symfony\Contracts\Translation\TranslatorInterface;
Expand All @@ -35,16 +36,24 @@ class SaveTagsToDatabase
*/
protected $translator;


/**
* @var TagCountValidator
*/
protected $tagCountValidator;

/**
* @param SettingsRepositoryInterface $settings
* @param Factory $validator
* @param TranslatorInterface $translator
* @param TagCountValidator $tagCountValidator
*/
public function __construct(SettingsRepositoryInterface $settings, Factory $validator, TranslatorInterface $translator)
public function __construct(SettingsRepositoryInterface $settings, Factory $validator, TranslatorInterface $translator, TagCountValidator $tagCountValidator)
{
$this->settings = $settings;
$this->validator = $validator;
$this->translator = $translator;
$this->tagCountValidator = $tagCountValidator;
}

/**
Expand Down Expand Up @@ -134,13 +143,21 @@ protected function validateTagCount($type, $count)
$max = $this->settings->get('flarum-tags.max_'.$type.'_tags');
$key = 'tag_count_'.$type;

$validator = $this->validator->make(
[$key => $count],
[$key => ['numeric', $min === $max ? "size:$min" : "between:$min,$max"]]
);

if ($validator->fails()) {
throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]);
}
$this->tagCountValidator->setType($type);
$this->tagCountValidator->setMin($min);
$this->tagCountValidator->setMax($max);
// $this->tagCountValidator->setCount($count);

$this->tagCountValidator->assertValid([$key => $count]);


// $validator = $this->validator->make(
// [$key => $count],
// [$key => ['numeric', $min === $max ? "size:$min" : "between:$min,$max"]]
// );

// if ($validator->fails()) {
// throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]);
// }
}
}
114 changes: 114 additions & 0 deletions extensions/tags/src/TagCountValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Flarum\Tags;

use Flarum\Foundation\AbstractValidator;

class TagCountValidator extends AbstractValidator
{
/**
* @var string
*/
protected $type;

/**
* @var int
*/
protected $min;

/**
* @var int
*/
protected $max;

// /**
// * @var int
// */
// protected $count;

/**
* @return string
*/
protected function getType()
{
return $this->type;
}

/**
* @param string $type
* @return void
*/
public function setType($type)
{
$this->type = $type;
}

/**
* @return int
*/
protected function getMin()
{
return $this->min;
}

/**
* @param int $min
* @return void
*/
public function setMin($min)
{
$this->min = $min;
}

/**
* @return int
*/
protected function getMax()
{
return $this->max;
}

/**
* @param int $max
* @return void
*/
public function setMax($max)
{
$this->max = $max;
}

// /**
// * @return int
// */
// protected function getCount()
// {
// return $this->count;
// }

// /**
// * @param int $count
// * @return void
// */
// public function setCount($count)
// {
// $this->count = $count;
// }

/**
* {@inheritdoc}
*/
protected function getRules()
{
$type = $this->type;
$min = $this->min;
$max = $this->max;

return [
'tag_count_'.$type => [
'numeric',
'size:'.$min,
'between:'.$min,$max
]
];
}
}

0 comments on commit 696f6fd

Please sign in to comment.