<commit_before><?php namespace EventSourcing\Laravel\EventStore;

use EventSourcing\EventDispatcher\TransferObject;
use EventSourcing\EventStore\EventStore;
use EventSourcing\Exceptions\NoEventsFoundException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\QueryException;

final class MysqlEventStore extends EventStore
{
    /**
     * @var
     */
    protected $db;

    /**
     * @var string
     */
    private $table;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        parent::__construct($app);

        $this->db = $app->make('db')->connection($this->getConnectionName());
        $this->table = $this->getTableName();
    }

    /**
     * @param $id
     * @return array
     * @throws NoEventsFoundException
     */
    public function getEventsFor($id)
    {
        $rows = $this->db->table($this->table)
            ->select(['uuid', 'version', 'payload', 'type', 'recorded_on'])
            ->where('uuid', $id)
            ->orderBy('version', 'asc')
            ->get();

        if (! $rows) {
            throw new NoEventsFoundException();
        }

        $events = [];

        foreach ($rows as $row) {
            $events[] = $this->serializer->deserialize(json_decode($row->payload, true));
        }

        return $events;
    }

    /**
     * @return array
     */
    public function getAllEvents()
    {
        return $this->db->table($this->getTableName())
            ->select(['uuid', 'version', 'payload', 'type', 'recorded_on'])
            ->get();
    }

    /**
     * @return mixed
     */
    private function getTableName()
    {
        return $this->config->get('event_sourcing.table_name', 'eventstore');
    }

    /**
     * @return mixed
     */
    private function getConnectionName()
    {
        return $this->config->get('event_sourcing.connection_name', 'eventstore');
    }

    /**
     * @param TransferObject $transferObject
     * @return mixed
     */
    protected function storeEvent(TransferObject $transferObject)
    {
        $metadata = $transferObject->getMetadata();

        $this->db->beginTransaction();

        try {
            $this->db->table($this->table)->insert([
                'uuid' => $metadata['uuid'],
                'version' => $metadata['version'],
                'payload' => json_encode($this->serializer->serialize($transferObject->getEvent())),
                'metadata' => json_encode($this->serializer->serialize($transferObject->getMetadata())),
                'recorded_on' => $metadata['recorded_on'],
                'type' => $metadata['type']
            ]);

            $this->db->commit();
        } catch (QueryException $ex) {
            $this->db->rollBack();
            throw $ex;
        }
    }
}
<commit_msg>Add MetaData in select<commit_after><?php namespace EventSourcing\Laravel\EventStore;

use EventSourcing\EventDispatcher\TransferObject;
use EventSourcing\EventStore\EventStore;
use EventSourcing\Exceptions\NoEventsFoundException;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Database\QueryException;

final class MysqlEventStore extends EventStore
{
    /**
     * @var
     */
    protected $db;

    /**
     * @var string
     */
    private $table;

    /**
     * @param Application $app
     */
    public function __construct(Application $app)
    {
        parent::__construct($app);

        $this->db = $app->make('db')->connection($this->getConnectionName());
        $this->table = $this->getTableName();
    }

    /**
     * @param $id
     * @return array
     * @throws NoEventsFoundException
     */
    public function getEventsFor($id)
    {
        $rows = $this->db->table($this->table)
            ->select(['uuid', 'version', 'payload', 'metadata', 'type', 'recorded_on'])
            ->where('uuid', $id)
            ->orderBy('version', 'asc')
            ->get();

        if (! $rows) {
            throw new NoEventsFoundException();
        }

        $events = [];

        foreach ($rows as $row) {
            $events[] = $this->serializer->deserialize(json_decode($row->payload, true));
        }

        return $events;
    }

    /**
     * @return array
     */
    public function getAllEvents()
    {
        return $this->db->table($this->getTableName())
            ->select(['uuid', 'version', 'payload', 'metadata', 'type', 'recorded_on'])
            ->get();
    }

    /**
     * @return mixed
     */
    private function getTableName()
    {
        return $this->config->get('event_sourcing.table_name', 'eventstore');
    }

    /**
     * @return mixed
     */
    private function getConnectionName()
    {
        return $this->config->get('event_sourcing.connection_name', 'eventstore');
    }

    /**
     * @param TransferObject $transferObject
     * @return mixed
     */
    protected function storeEvent(TransferObject $transferObject)
    {
        $metadata = $transferObject->getMetadata();

        $this->db->beginTransaction();

        try {
            $this->db->table($this->table)->insert([
                'uuid' => $metadata['uuid'],
                'version' => $metadata['version'],
                'payload' => json_encode($this->serializer->serialize($transferObject->getEvent())),
                'metadata' => json_encode($this->serializer->serialize($transferObject->getMetadata())),
                'recorded_on' => $metadata['recorded_on'],
                'type' => $metadata['type']
            ]);

            $this->db->commit();
        } catch (QueryException $ex) {
            $this->db->rollBack();
            throw $ex;
        }
    }
}

{'fraction_non_alphanumeric': 0.16076150185087257, 'fraction_numerical': 0.0, 'mean_word_length': 2.1698324022346367, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 99, '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': '2223519', 'n_tokens_mistral': 1615, 'n_tokens_neox': 1564, 'n_words': 275}