APIPresenter.php 975 Bytes
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1 2 3 4
<?php

namespace Source\UseCases\Users\Authenticate;

Jacob Priddy's avatar
Jacob Priddy committed
5
use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
6 7
use Source\UseCases\BasePresenter;

Jacob Priddy's avatar
Jacob Priddy committed
8 9
class APIPresenter extends BasePresenter implements Presenter
{
Jacob Priddy's avatar
Jacob Priddy committed
10 11 12
    protected array $viewModel = [];

    /** @inheritDoc */
Jacob Priddy's avatar
Jacob Priddy committed
13 14
    public function present(ResponseModel $responseModel): void
    {
Jacob Priddy's avatar
Jacob Priddy committed
15 16
        $user = $responseModel->getUser();
        $token = $responseModel->getToken();
17 18 19 20 21 22 23 24
        $expires = $token->getExpiresAt();

        if (!$expires) {
            $expires = 0;
        } else {
            $expires = $expires->diffInMinutes(Carbon::now());
        }

25
        $this->viewModel['user'] = $this->formatFullUser($user);
Jacob Priddy's avatar
Jacob Priddy committed
26
        $this->viewModel['token'] = [
27
            'value' => $responseModel->getRawToken(),
Jacob Priddy's avatar
Jacob Priddy committed
28
            'expires_at' => $this->formatDateTime($token->getExpiresAt()),
29
            'minutes' => $expires,
Jacob Priddy's avatar
Jacob Priddy committed
30 31 32 33
        ];
    }

    /** @inheritDoc */
Jacob Priddy's avatar
Jacob Priddy committed
34 35
    public function getViewModel(): array
    {
Jacob Priddy's avatar
Jacob Priddy committed
36 37 38
        return $this->viewModel;
    }
}