<commit_before><?php

declare(strict_types=1);

namespace Inowas\ScenarioAnalysis\Infrastructure\Projection;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManager;
use Inowas\AppBundle\Model\User;
use Inowas\Common\Id\UserId;
use Inowas\Common\Projection\AbstractDoctrineConnectionProjector;
use Inowas\ModflowModel\Infrastructure\Projection\ModelList\ModelFinder;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisDescriptionWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisNameWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisVisibilityWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasCloned;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasCreated;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasDeleted;

class ScenarioAnalysisProjector extends AbstractDoctrineConnectionProjector
{

    /** @var  ModelFinder */
    private $modelFinder;

    /** @var  EntityManager */
    private $entityManager;

    public function __construct(Connection $connection, ModelFinder $modelFinder, EntityManager $entityManager)
    {

        $this->entityManager = $entityManager;
        $this->modelFinder = $modelFinder;

        parent::__construct($connection);

        $schema = new Schema();
        $table = $schema->createTable(Table::SCENARIO_ANALYSIS_LIST);
        $table->addColumn('scenario_analysis_id', 'string', ['length' => 36]);
        $table->addColumn('base_model_id', 'string', ['length' => 36]);
        $table->addColumn('user_id', 'string', ['length' => 36]);
        $table->addColumn('user_name', 'string', ['length' => 255]);
        $table->addColumn('name', 'string', ['length' => 255]);
        $table->addColumn('description', 'string', ['length' => 255]);
        $table->addColumn('geometry', 'text');
        $table->addColumn('grid_size', 'text');
        $table->addColumn('bounding_box', 'text');
        $table->addColumn('created_at', 'string', ['length' => 255, 'notnull' => false]);
        $table->addColumn('public', 'smallint', ['default' => 1]);
        $table->setPrimaryKey(['scenario_analysis_id']);
        $table->addIndex(array('base_model_id'));
        $this->addSchema($schema);
    }

    public function onScenarioAnalysisWasCreated(ScenarioAnalysisWasCreated $event): void
    {
        $areaGeometry = $this->modelFinder->getAreaPolygonByModflowModelId($event->baseModelId());
        $boundingBox = $this->modelFinder->getBoundingBoxByModflowModelId($event->baseModelId());
        $gridSize = $this->modelFinder->getGridSizeByModflowModelId($event->baseModelId());

        $this->connection->insert(Table::SCENARIO_ANALYSIS_LIST, [
            'scenario_analysis_id' => $event->scenarioAnalysisId()->toString(),
            'user_id' => $event->userId()->toString(),
            'user_name' => $this->getUserNameByUserId($event->userId()),
            'name' => $event->name()->toString(),
            'description' => $event->description()->toString(),<commit_msg>Fix ScenarioAnalysisProjector<commit_after><?php

declare(strict_types=1);

namespace Inowas\ScenarioAnalysis\Infrastructure\Projection;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\ORM\EntityManager;
use Inowas\AppBundle\Model\User;
use Inowas\Common\Id\UserId;
use Inowas\Common\Projection\AbstractDoctrineConnectionProjector;
use Inowas\ModflowModel\Infrastructure\Projection\ModelList\ModelFinder;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisDescriptionWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisNameWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisVisibilityWasChanged;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasCloned;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasCreated;
use Inowas\ScenarioAnalysis\Model\Event\ScenarioAnalysisWasDeleted;

class ScenarioAnalysisProjector extends AbstractDoctrineConnectionProjector
{

    /** @var  ModelFinder */
    private $modelFinder;

    /** @var  EntityManager */
    private $entityManager;

    public function __construct(Connection $connection, ModelFinder $modelFinder, EntityManager $entityManager)
    {

        $this->entityManager = $entityManager;
        $this->modelFinder = $modelFinder;

        parent::__construct($connection);

        $schema = new Schema();
        $table = $schema->createTable(Table::SCENARIO_ANALYSIS_LIST);
        $table->addColumn('scenario_analysis_id', 'string', ['length' => 36]);
        $table->addColumn('base_model_id', 'string', ['length' => 36]);
        $table->addColumn('user_id', 'string', ['length' => 36]);
        $table->addColumn('user_name', 'string', ['length' => 255]);
        $table->addColumn('name', 'string', ['length' => 255]);
        $table->addColumn('description', 'string', ['length' => 255]);
        $table->addColumn('geometry', 'text');
        $table->addColumn('grid_size', 'text');
        $table->addColumn('bounding_box', 'text');
        $table->addColumn('created_at', 'string', ['length' => 255, 'notnull' => false]);
        $table->addColumn('public', 'smallint', ['default' => 1]);
        $table->setPrimaryKey(['scenario_analysis_id']);
        $table->addIndex(array('base_model_id'));
        $this->addSchema($schema);
    }

    public function onScenarioAnalysisWasCreated(ScenarioAnalysisWasCreated $event): void
    {
        $areaGeometry = $this->modelFinder->getAreaPolygonByModflowModelId($event->baseModelId());
        $boundingBox = $this->modelFinder->getBoundingBoxByModflowModelId($event->baseModelId());
        $gridSize = $this->modelFinder->getGridSizeByModflowModelId($event->baseModelId());

        if (null === $areaGeometry || null === $boundingBox || null === $gridSize) {
            return;
        }

        $this->connection->insert(Table::SCENARIO_ANALYSIS_LIST, [
            'scenario_analysis_id' => $event->scenarioAnalysisId()->toString(),
            'user_id' => $event->userId()->toString(),
            'user_name' => $this->getUserNameByUserId($event->userId()),
            'name' => $event->name()->toString(),
            'description' => $event->description()->toString(),
{'fraction_non_alphanumeric': 0.15430314707771356, 'fraction_numerical': 0.006422607578676943, 'mean_word_length': 4.551693404634581, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, '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': '4048556', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1824, 'n_words': 258}