Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
50 / 50
Order
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
12 / 12
18
100.00% covered (success)
100.00%
50 / 50
 register
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
9 / 9
 deliver
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 cancel
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 status
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 items
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 aggregateId
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 establishment
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 catalogFlow
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 tableIdentifier
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 equals
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 toArray
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
6 / 6
 apply
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
16 / 16
<?php
declare(strict_types=1);
namespace App\Order\Domain\Model;
use App\Order\Domain\Event\OrderWasCanceled;
use App\Order\Domain\Event\OrderWasDelivered;
use App\Order\Domain\Event\OrderWasRequested;
use App\Order\Domain\Exception\CannotCancelDeliveredOrderException;
use App\Order\Domain\Exception\CannotDeliverCanceledOrderException;
use App\Order\Domain\ValueObject\CatalogFlow;
use App\Order\Domain\ValueObject\Establishment;
use App\Order\Domain\ValueObject\ItemCollection;
use App\Order\Domain\ValueObject\OrderId;
use App\Order\Domain\ValueObject\Status;
use App\Order\Domain\ValueObject\TableIdentifier;
use App\Shared\EntityInterface;
use Prooph\EventSourcing\AggregateChanged;
use Prooph\EventSourcing\AggregateRoot;
final class Order extends AggregateRoot implements EntityInterface
{
    private OrderId $orderId;
    private Establishment $establishment;
    private CatalogFlow $catalogFlow;
    private TableIdentifier $tableIdentifier;
    private ItemCollection $items;
    private Status $status;
    public static function register(
        OrderId $orderId,
        Establishment $establishment,
        CatalogFlow $catalogFlow,
        TableIdentifier $tableIdentifier,
        ItemCollection $items
    ): Order {
        $self = new self();
        $self->recordThat(OrderWasRequested::withData(
            $orderId,
            $establishment,
            $catalogFlow,
            $tableIdentifier,
            $items,
            Status::waiting()
        ));
        return $self;
    }
    public function deliver(): Order
    {
        if ($this->status->status() === Status::canceled()->status()) {
            throw new CannotDeliverCanceledOrderException();
        }
        $this->recordThat(OrderWasDelivered::withData($this->orderId, Status::delivered()));
        $this->status = Status::delivered();
        return $this;
    }
    public function cancel(): Order
    {
        if ($this->status->status() === Status::delivered()->status()) {
            throw new CannotCancelDeliveredOrderException();
        }
        $this->recordThat(OrderWasCanceled::withData($this->orderId, Status::canceled()));
        $this->status = Status::canceled();
        return $this;
    }
    public function status(): Status
    {
        return $this->status;
    }
    public function items(): ItemCollection
    {
        return $this->items;
    }
    public function aggregateId(): string
    {
        return $this->orderId->toString();
    }
    public function establishment(): Establishment
    {
        return $this->establishment;
    }
    public function catalogFlow(): CatalogFlow
    {
        return $this->catalogFlow;
    }
    public function tableIdentifier(): TableIdentifier
    {
        return $this->tableIdentifier;
    }
    public function equals(EntityInterface $other): bool
    {
        if (!$other instanceof self) {
            return false;
        }
        return $this->toArray() === $other->toArray();
    }
    public function toArray(): array
    {
        return [
            'id' => $this->orderId->toString(),
            'establishment' => $this->establishment->toArray(),
            'catalog_flow' => $this->catalogFlow->toArray(),
            'table_identifier' => $this->tableIdentifier->toString(),
            'items' => $this->items->toArray(),
            'status' => $this->status->toString(),
        ];
    }
    protected function apply(AggregateChanged $event): void
    {
        switch (get_class($event)) {
            case OrderWasRequested::class:
                assert($event instanceof OrderWasRequested);
                $this->orderId = $event->orderId();
                $this->establishment = $event->establishment();
                $this->catalogFlow = $event->catalogFlow();
                $this->tableIdentifier = $event->tableIdentifier();
                $this->items = $event->items();
                $this->status = $event->status();
                break;
            case OrderWasCanceled::class:
                assert($event instanceof OrderWasCanceled);
                $this->status = $event->status();
                break;
            case OrderWasDelivered::class:
                assert($event instanceof OrderWasDelivered);
                $this->status = $event->status();
                break;
        }
    }
}