BasePresenter.php 4.13 KB
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Source\UseCases;

use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Entities\Door;
Jacob Priddy's avatar
Jacob Priddy committed
8
use Source\Entities\User;
9
use Source\Entities\Entry;
Jacob Priddy's avatar
Jacob Priddy committed
10
use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
11
use Source\Entities\Token;
12
use Source\Entities\Attempt;
Jacob Priddy's avatar
Jacob Priddy committed
13

Jacob Priddy's avatar
Jacob Priddy committed
14
15
abstract class BasePresenter
{
Jacob Priddy's avatar
Jacob Priddy committed
16
17
18
19
20
    /**
     * @param Carbon|null $datetime
     * @param string      $format
     * @return string|null
     */
Jacob Priddy's avatar
Jacob Priddy committed
21
22
    public function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
    {
Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
28
29
        if ($datetime === null) {
            return null;
        }

        return $datetime->format($format);
    }

Jacob Priddy's avatar
Jacob Priddy committed
30
    /**
31
     * @param \Source\Entities\User $user
Jacob Priddy's avatar
Jacob Priddy committed
32
33
     * @return array
     */
34
    public function formatFullUser(User $user): array
Jacob Priddy's avatar
Jacob Priddy committed
35
    {
Jacob Priddy's avatar
Jacob Priddy committed
36
37
38
39
40
41
42
        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'emplid' => $user->getEmplid(),
            'email' => $user->getEmail(),
43
44
45
            'expires_at' => $this->formatDateTime($user->getExpiresAt()),
            'created_at' => $this->formatDateTime($user->getCreatedAt()),
            'updated_at' => $this->formatDateTime($user->getUpdatedAt()),
Jacob Priddy's avatar
Jacob Priddy committed
46
47
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
48

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    /**
     * @param \Source\Entities\User $user
     * @return array
     */
    public function formatPartialUser(User $user): array
    {
        $expired = false;
        if ($user->getExpiresAt()) {
            $expired = $user->getExpiresAt() < Carbon::now();
        }

        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'email' => $user->getEmail(),
            'expired' => $expired,
        ];
    }

    /**
     * @param \Source\Entities\Group $group
     * @return array
     */
Jacob Priddy's avatar
Jacob Priddy committed
74
75
76
77
78
79
80
81
82
83
    public function formatGroup(Group $group): array
    {
        return [
            'id' => $group->getId(),
            'title' => $group->getTitle(),
            'description' => $group->getDescription(),
            'created_at' => $this->formatDateTime($group->getCreatedAt()),
            'updated_at' => $this->formatDateTime($group->getUpdatedAt()),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
84
85

    /**
86
     * @param \Source\Entities\Door|null $door
Jacob Priddy's avatar
Jacob Priddy committed
87
88
     * @return array
     */
89
    public function formatDoor(?Door $door): array
Jacob Priddy's avatar
Jacob Priddy committed
90
    {
91
92
93
94
        if (!$door) {
            return null;
        }

Jacob Priddy's avatar
Jacob Priddy committed
95
96
97
98
99
100
        return [
            'id' => $door->getId(),
            'name' => $door->getName(),
            'location' => $door->getLocation(),
            'created_at' => $this->formatDateTime($door->getCreatedAt()),
            'updated_at' => $this->formatDateTime($door->getUpdatedAt()),
101
            'last_seen_at' => $this->formatDateTime($door->getLastSeenAt()),
Jacob Priddy's avatar
Jacob Priddy committed
102
103
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123

    /**
     * @param \Source\Entities\Token|null $token
     * @return array
     */
    public function formatToken(?Token $token): array
    {
        if (!$token) {
            return [];
        }

        return [
            'id' => $token->getId(),
            'name' => $token->getName(),
            'user_id' => $token->getUserId(),
            'expires_at' => $this->formatDateTime($token->getExpiresAt()),
            'created_at' => $this->formatDateTime($token->getCreatedAt()),
            'updated_at' => $this->formatDateTime($token->getUpdatedAt()),
        ];
    }
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

    /**
     * @param \Source\Entities\Attempt|null $attempt
     * @return array
     */
    public function formatAttempt(?Attempt $attempt): array
    {
        if (!$attempt) {
            return [];
        }

        return [
            'id' => $attempt->getId(),
            'door_id' => $attempt->getDoorId(),
            'created_at' => $attempt->getCreatedAt(),
        ];
    }

    public function formatEntry(?Entry $entry): array
    {
        if (!$entry) {
            return [];
        }

        return [
            'id' => $entry->getId(),
            'user_id' => $entry->getUserId(),
            'door_id' => $entry->getDoorId(),
            'success' => $entry->wasSuccessful(),
            'created_at' => $entry->getCreatedAt(),
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
156
}