<commit_before><?php namespace App\Database;

use App\Database\Seeds\Seeder;
use App\Database\Seeds\Testing\BoardsSeeder;
use App\Database\Seeds\Testing\CommentsSeeder;
use App\Database\Seeds\Testing\PostsSeeder;
use App\Database\Seeds\Testing\RolesSeeder;
use App\Database\Seeds\Testing\UsersSeeder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Faker\Factory;
use Faker\Generator;
use Interop\Container\ContainerInterface;
use Limoncello\ContainerLight\Container;

/**
 * @package App
 */
class SeedsRunner
{
    /**
     * @return string[]
     */
    protected function getSeedsList()
    {
        return [
            RolesSeeder::class,
            UsersSeeder::class,
            BoardsSeeder::class,
            PostsSeeder::class,
            CommentsSeeder::class,
        ];
    }

    /**
     * @param Container $container
     *
     * @return void
     */
    public function run(Container $container)
    {
        $faker                       = Factory::create();
        $container[Generator::class] = $faker;

        foreach ($this->getSeeds($container) as $seederClass) {
            $faker->seed(crc32($seederClass));
            /** @var Seeder $seeder */
            $seeder = new $seederClass($container);
            $seeder->run();
        }
    }

    /** Seeds table name */
    const SEEDS_TABLE = '_seeds';

    /** Seed column name */
    const SEEDS_COLUMN_ID = 'id';

    /** Seed column name */
    const SEEDS_COLUMN_CLASS = 'class';

    /**
     * @param ContainerInterface $container
     *
     * @return Generator
     */
    protected function getSeeds(ContainerInterface $container)
    {
        $connection = $this->getConnection($container);
        $manager    = $connection->getSchemaManager();

        if ($manager->tablesExist(static::SEEDS_TABLE) === true) {
            $seeded = $this->readSeeded($connection);
        } else {
            $this->createSeedsTable($manager);
            $seeded = [];
        }

        $notYetSeeded = array_diff($this->getSeedsList(), $seeded);

        foreach ($notYetSeeded as $class) {
            yield $class;
            $this->saveSeed($connection, $class);
        }
    }

    /**
     * @param ContainerInterface $container
     *
     * @return Connection
     */
    private function getConnection(ContainerInterface $container)
    {
        return $container->get(Connection::class);
    }

    /**
     * @param AbstractSchemaManager $manager
     *
     * @return void
     */
    private function createSeedsTable(AbstractSchemaManager $manager)
    {
        $table = new Table(static::SEEDS_TABLE);

        $table
            ->addColumn(static::SEEDS_COLUMN_ID, Type::INTEGER)
            ->setUnsigned(true)
            ->setAutoincrement(true);
        $table
            ->addColumn(static::SEEDS_COLUMN_CLASS, Type::STRING)
            ->setLength(256);

        $table->setPrimaryKey([static::SEEDS_COLUMN_ID]);
        $table->addUniqueIndex([static::SEEDS_COLUMN_CLASS]);

        $manager->createTable($table);
    }

    /**
     * @param Connection $connection
     *
     * @return array
     */
    private function readSeeded($connection)
    {
        $builder = $connection->createQueryBuilder();
        $seeded  = [];

        if ($connection->getSchemaManager()->tablesExist(static::SEEDS_TABLE) === true) {
            $seeds = $builder
                ->select(static::SEEDS_COLUMN_ID, static::SEEDS_COLUMN_CLASS)
                ->from(static::SEEDS_TABLE)
                ->orderBy(static::SEEDS_COLUMN_ID)
                ->execute()
                ->fetchAll();
            foreach ($seeds as $seed) {
                $index          = $seed[static::SEEDS_COLUMN_ID];
                $class          = $seed[static::SEEDS_COLUMN_CLASS];
                $seeded[$index] = $class;
            }
        }

        return $seeded;
    }

    /**
     * @param Connection $connection
     * @param string     $class
     *
     * @return void
     */
    private function saveSeed(Connection $connection, $class)
    {
        $connection->insert(static::SEEDS_TABLE, [static::SEEDS_COLUMN_CLASS => $class]);
    }
}
<commit_msg>Close #12<commit_after><?php namespace App\Database;

use App\Database\Seeds\Seeder;
use App\Database\Seeds\Testing\BoardsSeeder;
use App\Database\Seeds\Testing\CommentsSeeder;
use App\Database\Seeds\Testing\PostsSeeder;
use App\Database\Seeds\Testing\RolesSeeder;
use App\Database\Seeds\Testing\UsersSeeder;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Types\Type;
use Faker\Factory;
use Faker\Generator;
use Interop\Container\ContainerInterface;
use Limoncello\ContainerLight\Container;

/**
 * @package App
 */
class SeedsRunner
{
    /**
     * @return string[]
     */
    protected function getSeedsList()
    {
        return [
            RolesSeeder::class,
            UsersSeeder::class,
            BoardsSeeder::class,
            PostsSeeder::class,
            CommentsSeeder::class,
        ];
    }

    /**
     * @param Container $container
     *
     * @return void
     */
    public function run(Container $container)
    {
        $faker                       = Factory::create();
        $container[Generator::class] = $faker;

        foreach ($this->getSeeds($container) as $seederClass) {
            $faker->seed(crc32($seederClass));
            /** @var Seeder $seeder */
            $seeder = new $seederClass($container);
            $seeder->run();
        }
    }

    /** Seeds table name */
    const SEEDS_TABLE = '_seeds';

    /** Seed column name */
    const SEEDS_COLUMN_ID = 'id';

    /** Seed column name */
    const SEEDS_COLUMN_CLASS = 'class';

    /**
     * @param ContainerInterface $container
     *
     * @return Generator
     */
    protected function getSeeds(ContainerInterface $container)
    {
        $connection = $this->getConnection($container);
        $manager    = $connection->getSchemaManager();

        if ($manager->tablesExist(static::SEEDS_TABLE) === true) {
            $seeded = $this->readSeeded($connection);
        } else {
            $this->createSeedsTable($manager);
            $seeded = [];
        }

        $notYetSeeded = array_diff($this->getSeedsList(), $seeded);

        foreach ($notYetSeeded as $class) {
            yield $class;
            $this->saveSeed($connection, $class);
        }
    }

    /**
     * @param ContainerInterface $container
     *
     * @return Connection
     */
    private function getConnection(ContainerInterface $container)
    {
        return $container->get(Connection::class);
    }

    /**
     * @param AbstractSchemaManager $manager
     *
     * @return void
     */
    private function createSeedsTable(AbstractSchemaManager $manager)
    {
        $table = new Table(static::SEEDS_TABLE);

        $table
            ->addColumn(static::SEEDS_COLUMN_ID, Type::INTEGER)
            ->setUnsigned(true)
            ->setAutoincrement(true);
        $table
            ->addColumn(static::SEEDS_COLUMN_CLASS, Type::STRING)
            ->setLength(255);

        $table->setPrimaryKey([static::SEEDS_COLUMN_ID]);
        $table->addUniqueIndex([static::SEEDS_COLUMN_CLASS]);

        $manager->createTable($table);
    }

    /**
     * @param Connection $connection
     *
     * @return array
     */
    private function readSeeded($connection)
    {
        $builder = $connection->createQueryBuilder();
        $seeded  = [];

        if ($connection->getSchemaManager()->tablesExist(static::SEEDS_TABLE) === true) {
            $seeds = $builder
                ->select(static::SEEDS_COLUMN_ID, static::SEEDS_COLUMN_CLASS)
                ->from(static::SEEDS_TABLE)
                ->orderBy(static::SEEDS_COLUMN_ID)
                ->execute()
                ->fetchAll();
            foreach ($seeds as $seed) {
                $index          = $seed[static::SEEDS_COLUMN_ID];
                $class          = $seed[static::SEEDS_COLUMN_CLASS];
                $seeded[$index] = $class;
            }
        }

        return $seeded;
    }

    /**
     * @param Connection $connection
     * @param string     $class
     *
     * @return void
     */
    private function saveSeed(Connection $connection, $class)
    {
        $connection->insert(static::SEEDS_TABLE, [static::SEEDS_COLUMN_CLASS => $class]);
    }
}

{'fraction_non_alphanumeric': 0.13022545796148427, 'fraction_numerical': 0.0014091122592766556, 'mean_word_length': 2.161469933184855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6410025', 'n_tokens_mistral': 2570, 'n_tokens_neox': 2512, 'n_words': 413}