<commit_before><?php

require_once __DIR__ . '/../bootstrap.php';

use Broadway\EventSourcing\AggregateFactory\PublicConstructorAggregateFactory;

/**
 * Invitation aggregate root.
 *
 * The aggregate root will guard that the invitation can only be accepted OR
 * declined, but not both.
 */
class Invitation extends Broadway\EventSourcing\EventSourcedAggregateRoot
{
    private $accepted = false;
    private $declined = false;
    private $invitationId;

    /**
     * Factory method to create an invitation.
     */
    public static function invite($invitationId, $name)
    {
        $invitation = new Invitation();

        // After instantiation of the object we apply the "InvitedEvent".
        $invitation->apply(new InvitedEvent($invitationId, $name));

        return $invitation;
    }

    /**
     * Every aggregate root will expose its id.
     *
     * {@inheritDoc}
     */
    public function getAggregateRootId()
    {
        return $this->invitationId;
    }

    /*
     * The two methods below are part of the public API of the aggregate root.
     */

    public function accept()
    {
        // throw if already declined
        if ($this->declined) {
            throw new RuntimeException('Already declined.');
        }

        /* If the invitation is already accepted, nothing happens we do not
         * throw an exception, but also no event is recorded. This is one way of
         * implementing idempotency in your event sourced aggregate roots
         */
        if ($this->accepted) {
            return;
        }

        $this->apply(new AcceptedEvent($this->invitationId));
    }

    public function decline()
    {
        if ($this->accepted) {
            throw new RuntimeException('Already accepted.');
        }

        if ($this->declined) {
            return;
        }

        $this->apply(new DeclinedEvent($this->invitationId));
    }

    /*
     * The methods below are called as the aggregate root is reconstituted from
     * the previously recorded events.
     */

    protected function applyAcceptedEvent(AcceptedEvent $event)
    {
        /* if we encounter an AcceptedEvent we change the internal state of the
         * aggregate root. This happens if the aggregate root gets reconstituted.
         */
        $this->accepted = true;
    }

    protected function applyDeclinedEvent(DeclinedEvent $event)
    {
        $this->declined = true;
    }

    protected function applyInvitedEvent(InvitedEvent $event)
    {
        $this->invitationId = $event->invitationId;
    }
}

/**
 * A repository that will only store and retrieve Invitation aggregate roots.
 *
 * This repository uses the base class provided by the EventSourcing component.
 */
class InvitationRepository extends Broadway\EventSourcing\EventSourcingRepository
{
    public function __construct(Broadway\EventStore\EventStoreInterface $eventStore, Broadway\EventHandling\EventBusInterface $eventBus)
    {
        parent::__construct($eventStore, $eventBus, 'Invitation', new PublicConstructorAggregateFactory());
    }
}

/*
 * When using CQRS with commands, a lot of times you will find that you have a
 * command object and a "dual" event. Mind though that this is not always the
 * case. The following classes show the commands and events for our small
 * domain model.
 */

/* All commands and events below will cary the id of the aggregate root. For
 * our convenience and readability later on we provide base classes that hold
 * this data.
 */
abstract class InvitationCommand
{
    public $invitationId;
    public function __construct($invitationId)
    {
        $this->invitationId = $invitationId;
    }
}<commit_msg>User full namespace inline<commit_after><?php

require_once __DIR__ . '/../bootstrap.php';

/**
 * Invitation aggregate root.
 *
 * The aggregate root will guard that the invitation can only be accepted OR
 * declined, but not both.
 */
class Invitation extends Broadway\EventSourcing\EventSourcedAggregateRoot
{
    private $accepted = false;
    private $declined = false;
    private $invitationId;

    /**
     * Factory method to create an invitation.
     */
    public static function invite($invitationId, $name)
    {
        $invitation = new Invitation();

        // After instantiation of the object we apply the "InvitedEvent".
        $invitation->apply(new InvitedEvent($invitationId, $name));

        return $invitation;
    }

    /**
     * Every aggregate root will expose its id.
     *
     * {@inheritDoc}
     */
    public function getAggregateRootId()
    {
        return $this->invitationId;
    }

    /*
     * The two methods below are part of the public API of the aggregate root.
     */

    public function accept()
    {
        // throw if already declined
        if ($this->declined) {
            throw new RuntimeException('Already declined.');
        }

        /* If the invitation is already accepted, nothing happens we do not
         * throw an exception, but also no event is recorded. This is one way of
         * implementing idempotency in your event sourced aggregate roots
         */
        if ($this->accepted) {
            return;
        }

        $this->apply(new AcceptedEvent($this->invitationId));
    }

    public function decline()
    {
        if ($this->accepted) {
            throw new RuntimeException('Already accepted.');
        }

        if ($this->declined) {
            return;
        }

        $this->apply(new DeclinedEvent($this->invitationId));
    }

    /*
     * The methods below are called as the aggregate root is reconstituted from
     * the previously recorded events.
     */

    protected function applyAcceptedEvent(AcceptedEvent $event)
    {
        /* if we encounter an AcceptedEvent we change the internal state of the
         * aggregate root. This happens if the aggregate root gets reconstituted.
         */
        $this->accepted = true;
    }

    protected function applyDeclinedEvent(DeclinedEvent $event)
    {
        $this->declined = true;
    }

    protected function applyInvitedEvent(InvitedEvent $event)
    {
        $this->invitationId = $event->invitationId;
    }
}

/**
 * A repository that will only store and retrieve Invitation aggregate roots.
 *
 * This repository uses the base class provided by the EventSourcing component.
 */
class InvitationRepository extends Broadway\EventSourcing\EventSourcingRepository
{
    public function __construct(Broadway\EventStore\EventStoreInterface $eventStore, Broadway\EventHandling\EventBusInterface $eventBus)
    {
        parent::__construct($eventStore, $eventBus, 'Invitation', new Broadway\EventSourcing\AggregateFactory\PublicConstructorAggregateFactory());
    }
}

/*
 * When using CQRS with commands, a lot of times you will find that you have a
 * command object and a "dual" event. Mind though that this is not always the
 * case. The following classes show the commands and events for our small
 * domain model.
 */

/* All commands and events below will cary the id of the aggregate root. For
 * our convenience and readability later on we provide base classes that hold
 * this data.
 */
abstract class InvitationCommand
{
    public $invitationId;
    public function __construct($invitationId)
    {
        $this->invitationId = $invitationId;
    }
}
{'fraction_non_alphanumeric': 0.08482447753039202, 'fraction_numerical': 0.0, 'mean_word_length': 2.651870324189526, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 33, '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': '1456419', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1876, 'n_words': 667}