APIPresenter.php 971 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());
        }

Jacob Priddy's avatar
Jacob Priddy committed
25

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

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