APIPresenter.php 1.19 KB
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'] = self::formatFullUser($user);
26
        $this->viewModel['is_new'] = $responseModel->isNewUser();
Jacob Priddy's avatar
Jacob Priddy committed
27
        $this->viewModel['token'] = [
28
            'value' => $responseModel->getRawToken(),
29
            'expires_at' => self::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
        return $this->viewModel;
    }
39 40 41 42 43 44 45 46 47 48

    /**
     * Returns if the user was newly created
     *
     * @return bool
     */
    public function isNewUser(): bool
    {
        return $this->viewModel['is_new'];
    }
Jacob Priddy's avatar
Jacob Priddy committed
49
}