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 3e8aba9
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 18 deletions.
22 changes: 14 additions & 8 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,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]);
}
}
99 changes: 99 additions & 0 deletions extensions/tags/src/TagCountValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/

namespace Flarum\Tags;

use Flarum\Foundation\AbstractValidator;

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

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

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

/**
* @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;
}

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

return [
"tag_count_{$type}" => [
'numeric',
"size:{$min}",
"between:{$min},{$max}"
]
];
}
}
16 changes: 6 additions & 10 deletions framework/core/src/Foundation/AbstractValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down

0 comments on commit 3e8aba9

Please sign in to comment.