read model doctrine symfony

class StatusFinder extends FinderAbstract
{
    public function getTableName(): string
    {
        return 'payment_mokka_status';
    }

    public function getAlias(): string
    {
        return 'pmos';
    }

    /**
     * @param string $orderNumber
     *
     * @return StatusReadModel[]
     */
    public function getStatusCollection(string $orderNumber): array
    {
        return array_map(
            static fn($order) => StatusReadModel::create($order),
            $this->getQueryBuilder()
                ->andWhere(sprintf('%s.order_number = :order_id', $this->getAlias()))
                ->setParameter('order_id', $orderNumber, PDO::PARAM_STR)
                ->execute()
                ->fetchAll()
        );
    }

    public function getLatestStatus(string $orderNumber): ?StatusReadModel
    {
        $qb = $this->getQueryBuilder()
            ->andWhere(sprintf('%s.order_number = :order_id', $this->getAlias()))
            ->orderBy(sprintf('%s.id', $this->getAlias()), 'desc')
            ->setMaxResults(1)
            ->setParameter('order_id', $orderNumber, PDO::PARAM_STR)
            ->execute();
        $data = $qb->fetch();

        if ($data) {
            return StatusReadModel::create($data);
        }

        return null;
    }
}
class StatusReadModel
{
    private int $id;

    private string $orderNumber;

    private array $response;

    private DateTimeInterface $createdAt;

    private function __construct(int $id, string $orderNumber, array $response, DateTimeInterface $createdAt)
    {
        $this->id = $id;
        $this->orderNumber = $orderNumber;
        $this->response = $response;
        $this->createdAt = $createdAt;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getOrderNumber(): string
    {
        return $this->orderNumber;
    }

    public function getResponse(): array
    {
        return $this->response;
    }

    public function getCreatedAt(): DateTimeInterface
    {
        return $this->createdAt;
    }

    public static function create(array $data): self
    {
        return new self(
            (int)($data['id'] ?? 0),
            $data['order_number'],
            json_decode($data['response'], true),
            new DateTime($data['created_at'])
        );
    }

    public function getOrderData(): array
    {
        return $this->response[ApiParameterEnum::CURRENT_ORDER] ?? [];
    }

    public function getOrderStatus(): ?string
    {
        return $this->response[ApiParameterEnum::CURRENT_ORDER][ApiParameterEnum::STATUS] ?? null;
    }

    public function paymentHasBeenApproved(): bool
    {
        return ($this->getOrderData()[ApiParameterEnum::DECISION] ?? null) === PaymentDecisionEnum::APPROVED
            && !empty($this->getPaymentId());
    }

    public function getPaymentId(): ?string
    {
        return $this->getOrderData()[ApiParameterEnum::PAYMENT_ID] ?? null;
    }
}
Dodawanie komentarzy wyłączone

Komentarze 1