Skip to content

Commit 696f6fd

Browse files
refactor: extract tag count validation logic
1 parent 19ffca7 commit 696f6fd

File tree

2 files changed

+140
-9
lines changed

2 files changed

+140
-9
lines changed

extensions/tags/src/Listener/SaveTagsToDatabase.php

Lines changed: 26 additions & 9 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,21 @@ 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-
);
141-
142-
if ($validator->fails()) {
143-
throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]);
144-
}
146+
$this->tagCountValidator->setType($type);
147+
$this->tagCountValidator->setMin($min);
148+
$this->tagCountValidator->setMax($max);
149+
// $this->tagCountValidator->setCount($count);
150+
151+
$this->tagCountValidator->assertValid([$key => $count]);
152+
153+
154+
// $validator = $this->validator->make(
155+
// [$key => $count],
156+
// [$key => ['numeric', $min === $max ? "size:$min" : "between:$min,$max"]]
157+
// );
158+
159+
// if ($validator->fails()) {
160+
// throw new ValidationException([], ['tags' => $validator->getMessageBag()->first($key)]);
161+
// }
145162
}
146163
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace Flarum\Tags;
4+
5+
use Flarum\Foundation\AbstractValidator;
6+
7+
class TagCountValidator extends AbstractValidator
8+
{
9+
/**
10+
* @var string
11+
*/
12+
protected $type;
13+
14+
/**
15+
* @var int
16+
*/
17+
protected $min;
18+
19+
/**
20+
* @var int
21+
*/
22+
protected $max;
23+
24+
// /**
25+
// * @var int
26+
// */
27+
// protected $count;
28+
29+
/**
30+
* @return string
31+
*/
32+
protected function getType()
33+
{
34+
return $this->type;
35+
}
36+
37+
/**
38+
* @param string $type
39+
* @return void
40+
*/
41+
public function setType($type)
42+
{
43+
$this->type = $type;
44+
}
45+
46+
/**
47+
* @return int
48+
*/
49+
protected function getMin()
50+
{
51+
return $this->min;
52+
}
53+
54+
/**
55+
* @param int $min
56+
* @return void
57+
*/
58+
public function setMin($min)
59+
{
60+
$this->min = $min;
61+
}
62+
63+
/**
64+
* @return int
65+
*/
66+
protected function getMax()
67+
{
68+
return $this->max;
69+
}
70+
71+
/**
72+
* @param int $max
73+
* @return void
74+
*/
75+
public function setMax($max)
76+
{
77+
$this->max = $max;
78+
}
79+
80+
// /**
81+
// * @return int
82+
// */
83+
// protected function getCount()
84+
// {
85+
// return $this->count;
86+
// }
87+
88+
// /**
89+
// * @param int $count
90+
// * @return void
91+
// */
92+
// public function setCount($count)
93+
// {
94+
// $this->count = $count;
95+
// }
96+
97+
/**
98+
* {@inheritdoc}
99+
*/
100+
protected function getRules()
101+
{
102+
$type = $this->type;
103+
$min = $this->min;
104+
$max = $this->max;
105+
106+
return [
107+
'tag_count_'.$type => [
108+
'numeric',
109+
'size:'.$min,
110+
'between:'.$min,$max
111+
]
112+
];
113+
}
114+
}

0 commit comments

Comments
 (0)