BasePresenter.php 6.17 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;
13
use Source\Entities\Override;
Jacob Priddy's avatar
Jacob Priddy committed
14
use Source\Entities\Schedule;
Jacob Priddy's avatar
Jacob Priddy committed
15

Jacob Priddy's avatar
Jacob Priddy committed
16
17
abstract class BasePresenter
{
18
    public const HUMAN_DATE_FORMAT = 'M j, Y H:i:s';
Jacob Priddy's avatar
Jacob Priddy committed
19
20
    public const HUMAN_DAY_FORMAT = 'M j, Y';
    public const DAY_FORMAT = 'Y-m-d';
Jacob Priddy's avatar
Jacob Priddy committed
21
    public const TIME_FORMAT = 'H:i';
22

Jacob Priddy's avatar
Jacob Priddy committed
23
24
25
26
27
    /**
     * @param Carbon|null $datetime
     * @param string      $format
     * @return string|null
     */
28
    public static function formatDateTime(?Carbon $datetime, string $format = 'c'): ?string
Jacob Priddy's avatar
Jacob Priddy committed
29
    {
Jacob Priddy's avatar
Jacob Priddy committed
30
31
32
33
34
35
36
        if ($datetime === null) {
            return null;
        }

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

Jacob Priddy's avatar
Jacob Priddy committed
37
    /**
38
     * @param \Source\Entities\User $user
Jacob Priddy's avatar
Jacob Priddy committed
39
40
     * @return array
     */
41
    public static function formatFullUser(User $user): array
Jacob Priddy's avatar
Jacob Priddy committed
42
    {
Jacob Priddy's avatar
Jacob Priddy committed
43
44
45
46
47
48
49
        return [
            'id' => $user->getId(),
            'first_name' => $user->getFirstName(),
            'last_name' => $user->getLastName(),
            'display_name' => $user->getDisplayName(),
            'emplid' => $user->getEmplid(),
            'email' => $user->getEmail(),
50
51
52
            'expires_at' => self::formatDateTime($user->getExpiresAt()),
            'created_at' => self::formatDateTime($user->getCreatedAt()),
            'updated_at' => self::formatDateTime($user->getUpdatedAt()),
Jacob Priddy's avatar
Jacob Priddy committed
53
54
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
55

56
57
58
59
    /**
     * @param \Source\Entities\User $user
     * @return array
     */
60
    public static function formatPartialUser(User $user): array
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    {
        $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
     */
81
    public static function formatGroup(Group $group): array
Jacob Priddy's avatar
Jacob Priddy committed
82
83
84
85
86
    {
        return [
            'id' => $group->getId(),
            'title' => $group->getTitle(),
            'description' => $group->getDescription(),
87
88
            'created_at' => self::formatDateTime($group->getCreatedAt()),
            'updated_at' => self::formatDateTime($group->getUpdatedAt()),
Jacob Priddy's avatar
Jacob Priddy committed
89
90
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
91
92

    /**
93
     * @param \Source\Entities\Door|null $door
Jacob Priddy's avatar
Jacob Priddy committed
94
95
     * @return array
     */
96
    public static function formatDoor(?Door $door): array
Jacob Priddy's avatar
Jacob Priddy committed
97
    {
98
        if (!$door) {
99
            return [];
100
101
        }

Jacob Priddy's avatar
Jacob Priddy committed
102
103
104
105
        return [
            'id' => $door->getId(),
            'name' => $door->getName(),
            'location' => $door->getLocation(),
106
            'version' => $door->getVersion() ?? 'Unknown',
107
108
109
            'created_at' => self::formatDateTime($door->getCreatedAt()),
            'updated_at' => self::formatDateTime($door->getUpdatedAt()),
            'last_seen_at' => self::formatDateTime($door->getLastSeenAt()),
Jacob Priddy's avatar
Jacob Priddy committed
110
111
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
112
113
114
115
116

    /**
     * @param \Source\Entities\Token|null $token
     * @return array
     */
117
    public static function formatToken(?Token $token): array
Jacob Priddy's avatar
Jacob Priddy committed
118
119
120
121
122
123
124
125
126
    {
        if (!$token) {
            return [];
        }

        return [
            'id' => $token->getId(),
            'name' => $token->getName(),
            'user_id' => $token->getUserId(),
127
128
129
            'expires_at' => self::formatDateTime($token->getExpiresAt()),
            'created_at' => self::formatDateTime($token->getCreatedAt()),
            'updated_at' => self::formatDateTime($token->getUpdatedAt()),
Jacob Priddy's avatar
Jacob Priddy committed
130
131
        ];
    }
132

133
134
135
136
    /**
     * @param \Source\Entities\Schedule|null $schedule
     * @return array
     */
137
    public static function formatSchedule(?Schedule $schedule): array
138
139
140
141
142
143
144
145
146
147
    {
        if (!$schedule) {
            return [];
        }

        return [
            'id' => $schedule->getId(),
            'type' => $schedule->getType(),
            'rset' => $schedule->getRset(),
            'duration' => $schedule->getDuration(),
Jacob Priddy's avatar
Jacob Priddy committed
148
            'description' => $schedule->getDescription(),
149
150
151
152
            'start_date' => self::formatDateTime($schedule->getStart()),
            'end_date' => self::formatDateTime($schedule->getEnd()),
            'created_at' => self::formatDateTime($schedule->getCreatedAt()),
            'updated_at' => self::formatDateTime($schedule->getUpdatedAt()),
153
154
155
        ];
    }

156
157
158
159
    /**
     * @param \Source\Entities\Attempt|null $attempt
     * @return array
     */
160
    public static function formatAttempt(?Attempt $attempt): array
161
162
163
164
165
166
167
    {
        if (!$attempt) {
            return [];
        }

        return [
            'id' => $attempt->getId(),
168
            'door_id' => $attempt->getDoorId(),
169
            'created_at' => self::formatDateTime($attempt->getCreatedAt()),
170
171
172
        ];
    }

173
174
175
176
    /**
     * @param \Source\Entities\Entry|null $entry
     * @return array
     */
177
    public static function formatEntry(?Entry $entry): array
178
179
180
181
182
183
184
185
    {
        if (!$entry) {
            return [];
        }

        return [
            'id' => $entry->getId(),
            'user_id' => $entry->getUserId(),
186
            'door_id' => $entry->getDoorId(),
187
            'success' => $entry->wasSuccessful(),
Jacob Priddy's avatar
Jacob Priddy committed
188
            'reason' => $entry->getReason(),
189
            'created_at' => self::formatDateTime($entry->getCreatedAt()),
190
191
        ];
    }
192
193
194
195
196

    /**
     * @param \Source\Entities\Override|null $override
     * @return array
     */
197
    public static function formatOverride(?Override $override): array
198
199
200
201
202
203
204
205
206
207
208
    {
        if (!$override) {
            return [];
        }

        return [
            'id' => $override->getId(),
            'reason' => $override->getReason(),
            'user_id' => $override->getUserId(),
            'door_id' => $override->getDoorId(),
            'type' => $override->getType(),
209
210
211
212
            'start' => self::formatDateTime($override->getStart()),
            'end' => self::formatDateTime($override->getEnd()),
            'created_at' => self::formatDateTime($override->getCreatedAt()),
            'updated_at' => self::formatDateTime($override->getUpdatedAt()),
213
214
        ];
    }
Jacob Priddy's avatar
Jacob Priddy committed
215
}