Skip to content

Commit 3e8aba9

Browse files
refactor: extract tag count validation logic
1 parent 19ffca7 commit 3e8aba9

File tree

3 files changed

+119
-18
lines changed

3 files changed

+119
-18
lines changed

extensions/tags/src/Listener/SaveTagsToDatabase.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Flarum\Settings\SettingsRepositoryInterface;
1515
use Flarum\Tags\Event\DiscussionWasTagged;
1616
use Flarum\Tags\Tag;
17+
use Flarum\Tags\TagCountValidator;
1718
use Flarum\User\Exception\PermissionDeniedException;
1819
use Illuminate\Contracts\Validation\Factory;
1920
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -35,16 +36,24 @@ class SaveTagsToDatabase
3536
*/
3637
protected $translator;
3738

39+
40+
/**
41+
* @var TagCountValidator
42+
*/
43+
protected $tagCountValidator;
44+
3845
/**
3946
* @param SettingsRepositoryInterface $settings
4047
* @param Factory $validator
4148
* @param TranslatorInterface $translator
49+
* @param TagCountValidator $tagCountValidator
4250
*/
43-
public function __construct(SettingsRepositoryInterface $settings, Factory $validator, TranslatorInterface $translator)
51+
public function __construct(SettingsRepositoryInterface $settings, Factory $validator, TranslatorInterface $translator, TagCountValidator $tagCountValidator)
4452
{
4553
$this->settings = $settings;
4654
$this->validator = $validator;
4755
$this->translator = $translator;
56+
$this->tagCountValidator = $tagCountValidator;
4857
}
4958

5059
/**
@@ -134,13 +143,10 @@ protected function validateTagCount($type, $count)
134143
$max = $this->settings->get('flarum-tags.max_'.$type.'_tags');
135144
$key = 'tag_count_'.$type;
136145

137-
$validator = $this->validator->make(
138-
[$key => $count],
139-
[$key => ['numeric', $min === $max ? "size:$min" : "between:$min,$max"]]
140-
);
146+
$this->tagCountValidator->setType($type);
147+
$this->tagCountValidator->setMin($min);
148+
$this->tagCountValidator->setMax($max);
141149

142-
if ($validator->fails()) {
143-
throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]);
144-
}
150+
$this->tagCountValidator->assertValid([$key => $count]);
145151
}
146152
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Flarum.
5+
*
6+
* For detailed copyright and license information, please view the
7+
* LICENSE file that was distributed with this source code.
8+
*/
9+
10+
namespace Flarum\Tags;
11+
12+
use Flarum\Foundation\AbstractValidator;
13+
14+
class TagCountValidator extends AbstractValidator
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected $type;
20+
21+
/**
22+
* @var int
23+
*/
24+
protected $min;
25+
26+
/**
27+
* @var int
28+
*/
29+
protected $max;
30+
31+
/**
32+
* @return string
33+
*/
34+
protected function getType()
35+
{
36+
return $this->type;
37+
}
38+
39+
/**
40+
* @param string $type
41+
* @return void
42+
*/
43+
public function setType($type)
44+
{
45+
$this->type = $type;
46+
}
47+
48+
/**
49+
* @return int
50+
*/
51+
protected function getMin()
52+
{
53+
return $this->min;
54+
}
55+
56+
/**
57+
* @param int $min
58+
* @return void
59+
*/
60+
public function setMin($min)
61+
{
62+
$this->min = $min;
63+
}
64+
65+
/**
66+
* @return int
67+
*/
68+
protected function getMax()
69+
{
70+
return $this->max;
71+
}
72+
73+
/**
74+
* @param int $max
75+
* @return void
76+
*/
77+
public function setMax($max)
78+
{
79+
$this->max = $max;
80+
}
81+
82+
/**
83+
* {@inheritdoc}
84+
*/
85+
protected function getRules()
86+
{
87+
$type = $this->type;
88+
$min = $this->min;
89+
$max = $this->max;
90+
91+
return [
92+
"tag_count_{$type}" => [
93+
'numeric',
94+
"size:{$min}",
95+
"between:{$min},{$max}"
96+
]
97+
];
98+
}
99+
}

framework/core/src/Foundation/AbstractValidator.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,14 @@ public function addConfiguration($callable)
4646
*/
4747
protected $translator;
4848

49-
/**
50-
* @var Cache
51-
*/
52-
protected $cache;
53-
5449
/**
5550
* @param Factory $validator
5651
* @param TranslatorInterface $translator
5752
*/
58-
public function __construct(Factory $validator, TranslatorInterface $translator, Cache $cache)
53+
public function __construct(Factory $validator, TranslatorInterface $translator)
5954
{
6055
$this->validator = $validator;
6156
$this->translator = $translator;
62-
$this->cache = $cache;
6357
}
6458

6559
/**
@@ -97,8 +91,10 @@ protected function getMessages()
9791
*/
9892
protected function getAttributeNames()
9993
{
100-
if ($this->cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) {
101-
return $this->cache->get(self::$CORE_VALIDATION_CACHE_KEY);
94+
$cache = resolve(Cache::class);
95+
96+
if ($cache->get(self::$CORE_VALIDATION_CACHE_KEY) !== null) {
97+
return $cache->get(self::$CORE_VALIDATION_CACHE_KEY);
10298
}
10399

104100
$extId = $this->getClassExtensionId();
@@ -109,7 +105,7 @@ protected function getAttributeNames()
109105
$attributeNames[$attribute] = $this->translator->trans($key);
110106
}
111107

112-
$this->cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);
108+
$cache->forever(self::$CORE_VALIDATION_CACHE_KEY, $attributeNames);
113109

114110
return $attributeNames;
115111
}

0 commit comments

Comments
 (0)