Skip to content

Commit

Permalink
closes #47
Browse files Browse the repository at this point in the history
  • Loading branch information
ticktackk committed Mar 9, 2021
1 parent 5d1ef95 commit b19d7b0
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CHANGELOG
- **New:** Seed for rejected users (#42)
- **New:** Seed for server error logs (#45)
- **New:** Seed for spam trigger log (#46)
- **New:** Seed for spam cleaner log (#47)
- **Fix:** Threads cannot be seeded (#36)

## 1.1.0 Alpha 3 (`1010013`)
Expand Down
18 changes: 18 additions & 0 deletions Cli/Command/Seed/SeedSpamCleanerLog.php
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';
}
}
83 changes: 83 additions & 0 deletions Seed/SpamCleanerLog.php
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();
}
}

0 comments on commit b19d7b0

Please sign in to comment.