From 3e8aba9b77fc752501cc1932c90ac7cc9fcd1e3f Mon Sep 17 00:00:00 2001 From: Davide Iadeluca Date: Tue, 19 Nov 2024 23:56:15 +0100 Subject: [PATCH] refactor: extract tag count validation logic --- .../tags/src/Listener/SaveTagsToDatabase.php | 22 +++-- extensions/tags/src/TagCountValidator.php | 99 +++++++++++++++++++ .../core/src/Foundation/AbstractValidator.php | 16 ++- 3 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 extensions/tags/src/TagCountValidator.php diff --git a/extensions/tags/src/Listener/SaveTagsToDatabase.php b/extensions/tags/src/Listener/SaveTagsToDatabase.php index c6d04c4c52..fb86c0ec47 100755 --- a/extensions/tags/src/Listener/SaveTagsToDatabase.php +++ b/extensions/tags/src/Listener/SaveTagsToDatabase.php @@ -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; @@ -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; } /** @@ -134,13 +143,10 @@ 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"]] - ); + $this->tagCountValidator->setType($type); + $this->tagCountValidator->setMin($min); + $this->tagCountValidator->setMax($max); - if ($validator->fails()) { - throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]); - } + $this->tagCountValidator->assertValid([$key => $count]); } } diff --git a/extensions/tags/src/TagCountValidator.php b/extensions/tags/src/TagCountValidator.php new file mode 100644 index 0000000000..63068ed380 --- /dev/null +++ b/extensions/tags/src/TagCountValidator.php @@ -0,0 +1,99 @@ +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; + } + + /** + * {@inheritdoc} + */ + protected function getRules() + { + $type = $this->type; + $min = $this->min; + $max = $this->max; + + return [ + "tag_count_{$type}" => [ + 'numeric', + "size:{$min}", + "between:{$min},{$max}" + ] + ]; + } +} diff --git a/framework/core/src/Foundation/AbstractValidator.php b/framework/core/src/Foundation/AbstractValidator.php index 3d8b00a7da..fbe0909f2d 100644 --- a/framework/core/src/Foundation/AbstractValidator.php +++ b/framework/core/src/Foundation/AbstractValidator.php @@ -46,20 +46,14 @@ public function addConfiguration($callable) */ protected $translator; - /** - * @var Cache - */ - protected $cache; - /** * @param Factory $validator * @param TranslatorInterface $translator */ - public function __construct(Factory $validator, TranslatorInterface $translator, Cache $cache) + public function __construct(Factory $validator, TranslatorInterface $translator) { $this->validator = $validator; $this->translator = $translator; - $this->cache = $cache; } /** @@ -97,8 +91,10 @@ protected function getMessages() */ protected function getAttributeNames() { - if ($this->cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) { - return $this->cache->get(self::$CORE_VALIDATION_CACHE_KEY); + $cache = resolve(Cache::class); + + if ($cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) { + return $cache->get(self::$CORE_VALIDATION_CACHE_KEY); } $extId = $this->getClassExtensionId(); @@ -109,7 +105,7 @@ protected function getAttributeNames() $attributeNames[$attribute] = $this->translator->trans($key); } - $this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames); + $cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames); return $attributeNames; }