-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace TickTackk\Seeder\Cli\Command\Seed; | ||
|
||
use Symfony\Component\Console\Input\InputInterface; | ||
|
||
class SeedSpamCleanerLog extends AbstractSeedCommand | ||
{ | ||
protected function getSeedName() : string | ||
{ | ||
return 'spam-cleaner-log'; | ||
} | ||
|
||
protected function getContentTypePlural(InputInterface $input = null) : string | ||
{ | ||
return 'Spam cleaner log'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace TickTackk\Seeder\Seed; | ||
|
||
use XF\Db\DuplicateKeyException; | ||
use XF\Entity\SpamCleanerLog as SpamCleanerLogEntity; | ||
use XF\Entity\User as UserEntity; | ||
use XF\Finder\User as UserFinder; | ||
use XF\Mvc\Entity\Entity; | ||
use XF\Util\Ip as IpUtil; | ||
|
||
class SpamCleanerLog extends AbstractSeed | ||
{ | ||
protected function setupVisitorFinder(UserFinder $userFinder): UserFinder | ||
{ | ||
return $userFinder | ||
->where('is_admin', false) | ||
->where('is_moderator', false) | ||
->where('is_staff', false) | ||
->where('is_banned', false); | ||
} | ||
|
||
protected function findRandomModOrAdmin() : UserEntity | ||
{ | ||
return $this->finderWithRandomOrder('XF:User') | ||
->whereOr(['is_admin', true], ['is_moderator', true]) | ||
->fetchOne(); | ||
} | ||
|
||
/** | ||
* @param array $params | ||
* | ||
* @return bool | ||
* | ||
* @throws \XF\PrintableException | ||
*/ | ||
protected function seed(array $params = []): bool | ||
{ | ||
$randomContent = $this->findRandomContent(); | ||
if (!$randomContent) | ||
{ | ||
return false; | ||
} | ||
|
||
$faker = $this->faker(); | ||
|
||
/** @var SpamCleanerLogEntity $spamCleanerLog */ | ||
$spamCleanerLog = $this->em()->create('XF:SpamCleanerLog'); | ||
$spamCleanerLog->data = $faker->randomElements(); | ||
$spamCleanerLog->application_date = $faker->dateTime->getTimestamp(); | ||
$spamCleanerLog->restored_date = $faker->boolean ? $faker->dateTime->getTimestamp() : 0; | ||
|
||
$visitor = \XF::visitor(); | ||
$spamCleanerLog->user_id = $visitor->user_id; | ||
$spamCleanerLog->username = $visitor->username; | ||
|
||
$applyingUser = $this->findRandomModOrAdmin(); | ||
$spamCleanerLog->applying_user_id = $applyingUser->user_id; | ||
$spamCleanerLog->applying_username = $applyingUser->username; | ||
|
||
try | ||
{ | ||
return $spamCleanerLog->save(); | ||
} | ||
catch (DuplicateKeyException $exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
protected function findRandomContent() :? Entity | ||
{ | ||
$contentTypes = $this->app()->getContentTypeField('spam_handler_class'); | ||
$contentType = \array_rand($contentTypes); | ||
$identifier = $this->app()->getContentTypeFieldValue($contentType, 'entity'); | ||
if ($identifier === null) | ||
{ | ||
return null; | ||
} | ||
|
||
return $this->finderWithRandomOrder($identifier)->fetchOne(); | ||
} | ||
} |