Commit e389365f authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

make cast methods static and use them as static methods

parent 4ca0b794
......@@ -33,7 +33,7 @@ class DatabaseAttemptsRepository implements AttemptsRepository
$query = \App\Attempt::query()->orderByDesc('created_at');
if ($doorId) {
$query->where('door_id', $this->castToInt($doorId));
$query->where('door_id', self::castToInt($doorId));
}
if ($begin && $end) {
......
......@@ -20,7 +20,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function getGroupsForDoor(string $doorId): array
{
/** @var Door|null $door */
$door = Door::query()->find(($this->castToInt($doorId)));
$door = Door::query()->find((self::castToInt($doorId)));
if (!$door) {
throw new EntityNotFoundException();
......@@ -37,7 +37,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function getDoorsForGroup(string $groupId): array
{
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
......@@ -54,9 +54,9 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function addDoorToGroup(string $doorId, string $groupId): void
{
/** @var Door|null $door */
$door = Door::query()->find($this->castToInt($doorId));
$door = Door::query()->find(self::castToInt($doorId));
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$door) {
throw new EntityNotFoundException('Door not found.');
......@@ -66,7 +66,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
throw new EntityNotFoundException('Group not found.');
}
$door->groups()->attach($this->castToInt($groupId));
$door->groups()->attach(self::castToInt($groupId));
}
/**
......@@ -75,9 +75,9 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function removeDoorFromGroup(string $doorId, string $groupId): void
{
/** @var Door|null $door */
$door = Door::query()->find($this->castToInt($doorId));
$door = Door::query()->find(self::castToInt($doorId));
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$door) {
throw new EntityNotFoundException('Door not found.');
......@@ -87,6 +87,6 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
throw new EntityNotFoundException('Group not found.');
}
$door->groups()->detach($this->castToInt($groupId));
$door->groups()->detach(self::castToInt($groupId));
}
}
......@@ -31,10 +31,10 @@ class DatabaseDoorScheduleRepository implements DoorScheduleRepository
$schedule->rset,
$schedule->duration,
$schedule->description,
$this->castToCarbon($schedule->start),
$this->castToCarbon($schedule->end),
$this->castToCarbon($schedule->created_at),
$this->castToCarbon($schedule->updated_at)
self::castToCarbon($schedule->start),
self::castToCarbon($schedule->end),
self::castToCarbon($schedule->created_at),
self::castToCarbon($schedule->updated_at)
);
}
......@@ -52,8 +52,8 @@ WHERE S.type = :TYPE AND ((CURRENT_DATE BETWEEN S.start AND S.end) OR (CURRENT_D
QUERY;
$schedules = $this->db->select($query, [
':DOOR_ID' => $this->castToInt($doorId),
':TYPE' => $this->castToInt($type),
':DOOR_ID' => self::castToInt($doorId),
':TYPE' => self::castToInt($type),
]);
return array_map(function ($schedule) {
......@@ -75,8 +75,8 @@ WHERE S.type = :TYPE AND (((:BEGIN, :END) OVERLAPS (s.start, S.end))
QUERY;
$schedules = $this->db->select($query, [
':DOOR_ID' => $this->castToInt($doorId),
':TYPE' => $this->castToInt($type),
':DOOR_ID' => self::castToInt($doorId),
':TYPE' => self::castToInt($type),
':BEGIN' => $begin,
':END' => $end,
]);
......@@ -101,9 +101,9 @@ WHERE S.type = :TYPE AND (((:BEGIN, :END) OVERLAPS (s.start, S.end))
QUERY;
$schedules = $this->db->select($query, [
':DOOR_ID' => $this->castToInt($doorId),
':USER_ID' => $this->castToInt($userId),
':TYPE' => $this->castToInt(Schedule::TYPE_USER_ACCESS),
':DOOR_ID' => self::castToInt($doorId),
':USER_ID' => self::castToInt($userId),
':TYPE' => self::castToInt(Schedule::TYPE_USER_ACCESS),
':BEGIN' => $begin,
':END' => $end,
]);
......
......@@ -43,8 +43,8 @@ QUERY;
$group->id,
$group->title,
$group->description,
$this->castToCarbon($group->created_at),
$this->castToCarbon($group->updated_at)
self::castToCarbon($group->created_at),
self::castToCarbon($group->updated_at)
);
}, $commonGroups);
}
......
......@@ -102,7 +102,7 @@ class DatabaseDoorsRepository implements DoorsRepository
$updated['version'] = $version;
}
\App\Door::query()->where('id', $this->castToInt($doorId))->update($updated);
\App\Door::query()->where('id', self::castToInt($doorId))->update($updated);
}
/**
......@@ -111,7 +111,7 @@ class DatabaseDoorsRepository implements DoorsRepository
public function get(string $doorId): ?Door
{
/** @var \App\Door|null $door */
$door = \App\Door::query()->find($this->castToInt($doorId));
$door = \App\Door::query()->find(self::castToInt($doorId));
if (!$door) {
return null;
}
......@@ -167,7 +167,7 @@ class DatabaseDoorsRepository implements DoorsRepository
public function update(string $doorId, Door $door): Door
{
/** @var \App\Door|null $dbDoor */
$dbDoor = \App\Door::query()->find($this->castToInt($doorId));
$dbDoor = \App\Door::query()->find(self::castToInt($doorId));
if (!$dbDoor) {
throw new EntityNotFoundException('Could not update door. Door not found.');
......@@ -187,7 +187,7 @@ class DatabaseDoorsRepository implements DoorsRepository
*/
public function delete(string $doorId): int
{
return \App\Door::destroy($this->castToInt($doorId));
return \App\Door::destroy(self::castToInt($doorId));
}
/**
......
......@@ -52,11 +52,11 @@ class DatabaseEntriesRepository implements EntriesRepository
$query = \App\Entry::query()->orderByDesc('created_at');
if ($userId) {
$query->where('user_id', $this->castToInt($userId));
$query->where('user_id', self::castToInt($userId));
}
if ($doorId) {
$query->where('door_id', $this->castToInt($doorId));
$query->where('door_id', self::castToInt($doorId));
}
if ($begin && $end) {
......
......@@ -20,7 +20,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function getGroupsForUser(string $userId): array
{
/** @var User|null $user */
$user = User::query()->find($this->castToInt($userId));
$user = User::query()->find(self::castToInt($userId));
if (!$user) {
throw new EntityNotFoundException();
......@@ -37,7 +37,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function getUsersForGroup(string $groupId): array
{
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
......@@ -54,9 +54,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function addUserToGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::query()->find($this->castToInt($userId));
$user = User::query()->find(self::castToInt($userId));
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$user) {
throw new EntityNotFoundException('User not found.');
......@@ -75,9 +75,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function removeUserFromGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::query()->find($this->castToInt($userId));
$user = User::query()->find(self::castToInt($userId));
/** @var Group|null $group */
$group = Group::query()->find($this->castToInt($groupId));
$group = Group::query()->find(self::castToInt($groupId));
if (!$user) {
throw new EntityNotFoundException('User not found.');
......
......@@ -42,7 +42,7 @@ class DatabaseGroupsRepository implements GroupsRepository
public function get(string $groupId): ?Group
{
/** @var \App\Group|null $group */
$group = \App\Group::query()->find($this->castToInt($groupId));
$group = \App\Group::query()->find(self::castToInt($groupId));
if (!$group) {
return null;
......@@ -111,7 +111,7 @@ class DatabaseGroupsRepository implements GroupsRepository
public function update(string $groupId, Group $group): ?Group
{
/** @var \App\Group|null $dbGroup */
$dbGroup = \App\Group::query()->find($this->castToInt($groupId));
$dbGroup = \App\Group::query()->find(self::castToInt($groupId));
if (!$dbGroup) {
return null;
......@@ -127,7 +127,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function delete(string $groupId): bool
{
return \App\Group::destroy($this->castToInt($groupId));
return \App\Group::destroy(self::castToInt($groupId));
}
/**
......
......@@ -57,10 +57,10 @@ class DatabaseOverridesRepository implements OverridesRepository
$override->user_id,
$override->door_id,
$override->type,
$this->castToCarbon($override->start),
$this->castToCarbon($override->end),
$this->castToCarbon($override->created_at),
$this->castToCarbon($override->updated_at)
self::castToCarbon($override->start),
self::castToCarbon($override->end),
self::castToCarbon($override->created_at),
self::castToCarbon($override->updated_at)
);
}
......@@ -72,11 +72,11 @@ class DatabaseOverridesRepository implements OverridesRepository
$query = \App\Override::query()->orderByDesc('created_at');
if ($doorId) {
$query->where('door_id', $this->castToInt($doorId));
$query->where('door_id', self::castToInt($doorId));
}
if ($userId) {
$query->where('user_id', $this->castToInt($userId));
$query->where('user_id', self::castToInt($userId));
}
if ($begin && $end) {
......@@ -107,7 +107,7 @@ WHERE A.door_id = :DOOR_ID
QUERY;
$override = $this->db->selectOne($query, [
':DOOR_ID' => $this->castToInt($doorId),
':DOOR_ID' => self::castToInt($doorId),
':BEGIN' => $begin,
':END' => $end,
]);
......@@ -168,7 +168,7 @@ QUERY;
public function updateOverride(string $overrideId, Override $override): Override
{
/** @var \App\Override $o */
$o = \App\Override::query()->find($this->castToInt($overrideId));
$o = \App\Override::query()->find(self::castToInt($overrideId));
if (!$o) {
throw new EntityNotFoundException('Override with id "' . $overrideId . '" does not exist.');
......@@ -185,7 +185,7 @@ QUERY;
public function get(string $overrideId): ?Override
{
/** @var \App\Override $o */
$o = \App\Override::query()->find($this->castToInt($overrideId));
$o = \App\Override::query()->find(self::castToInt($overrideId));
if (!$o) {
return null;
......
......@@ -91,7 +91,7 @@ class DatabaseSchedulesRepository implements SchedulesRepository
public function get(string $scheduleId): ?Schedule
{
/** @var \App\Schedule|null $schedule */
$schedule = \App\Schedule::query()->find($this->castToInt($scheduleId));
$schedule = \App\Schedule::query()->find(self::castToInt($scheduleId));
if (!$schedule) {
return null;
}
......@@ -108,7 +108,7 @@ class DatabaseSchedulesRepository implements SchedulesRepository
if ($type !== null) {
$query->where('type', $type);
}
$schedules = $query->where('group_id', $this->castToInt($groupId))
$schedules = $query->where('group_id', self::castToInt($groupId))
->where(static function (Builder $query) use ($date) {
/*
* We have a few different cases.
......@@ -136,7 +136,7 @@ class DatabaseSchedulesRepository implements SchedulesRepository
$query = \App\Schedule::query()->orderByDesc('created_at');
if ($groupId) {
$query->where('group_id', $this->castToInt($groupId));
$query->where('group_id', self::castToInt($groupId));
}
if ($type !== null) {
......@@ -168,7 +168,7 @@ class DatabaseSchedulesRepository implements SchedulesRepository
public function update(string $scheduleId, Schedule $schedule): Schedule
{
/** @var \App\Schedule|null $dbSchedule */
$dbSchedule = \App\Schedule::query()->find($this->castToInt($scheduleId));
$dbSchedule = \App\Schedule::query()->find(self::castToInt($scheduleId));
if (!$scheduleId) {
throw new EntityNotFoundException('Schedule does not exist.');
......
......@@ -77,7 +77,7 @@ class DatabaseTokensRepository implements TokensRepository
public function update(string $tokenId, Token $token): Token
{
/** @var \App\Token|null $dbToken */
$dbToken = \App\Token::query()->find($this->castToInt($tokenId));
$dbToken = \App\Token::query()->find(self::castToInt($tokenId));
if (!$dbToken) {
throw new EntityNotFoundException('Token with id "' . $tokenId . '" not found.');
......@@ -94,7 +94,7 @@ class DatabaseTokensRepository implements TokensRepository
public function get(string $tokenId): ?Token
{
/** @var \App\Token|null $token */
$token = \App\Token::query()->find($this->castToInt($tokenId));
$token = \App\Token::query()->find(self::castToInt($tokenId));
if (!$token) {
return null;
......@@ -180,7 +180,7 @@ class DatabaseTokensRepository implements TokensRepository
public function invalidateTokenById(string $tokenId): void
{
/** @var \App\Token|null $dbToken */
$dbToken = \App\Token::query()->find($this->castToInt($tokenId));
$dbToken = \App\Token::query()->find(self::castToInt($tokenId));
if (!$dbToken) {
throw new EntityNotFoundException('Token not found.');
......@@ -202,7 +202,7 @@ class DatabaseTokensRepository implements TokensRepository
$query = \App\Token::query()->whereNotNull('name')->orderByDesc('created_at');
if ($userId) {
$query->where('user_id', $this->castToInt($userId));
$query->where('user_id', self::castToInt($userId));
}
if ($validAt) {
......
......@@ -22,7 +22,7 @@ class DatabaseUsersRepository implements UsersRepository
public function get(string $userId): ?User
{
/** @var \App\User|null $user */
$user = \App\User::query()->find($this->castToInt($userId));
$user = \App\User::query()->find(self::castToInt($userId));
if (!$user) {
return null;
......@@ -133,7 +133,7 @@ class DatabaseUsersRepository implements UsersRepository
public function update(string $userId, User $user): ?User
{
/** @var \App\User|null $dbUser */
$dbUser = \App\User::query()->find($this->castToInt($userId));
$dbUser = \App\User::query()->find(self::castToInt($userId));
if (!$dbUser) {
return null;
......@@ -149,7 +149,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function delete(string $userId): bool
{
return \App\User::destroy($this->castToInt($userId));
return \App\User::destroy(self::castToInt($userId));
}
/**
......
......@@ -27,8 +27,8 @@ class GetAttempts implements GetAttemptsUseCase
public function getBetweenDates(?string $start, ?string $end, ?string $doorId, Presenter $presenter): void
{
$attempts = $this->attempts->getBetween(
$this->liberalCastToCarbon($start),
$this->liberalCastToCarbon($end),
self::liberalCastToCarbon($start),
self::liberalCastToCarbon($end),
$doorId
);
......
......@@ -76,7 +76,7 @@ class Access implements AccessUseCase
*/
protected function logDoorAttempt(string $doorId): void
{
$this->attempts->add(new Attempt(0, $this->castToInt($doorId)));
$this->attempts->add(new Attempt(0, self::castToInt($doorId)));
}
/**
......
......@@ -27,8 +27,8 @@ class GetEntries implements GetEntriesUseCase
public function getEntries(?string $userId, ?string $doorId, ?string $begin, ?string $end, Presenter $presenter): void
{
$entries = $this->entries->get(
$this->liberalCastToCarbon($begin),
$this->liberalCastToCarbon($end),
self::liberalCastToCarbon($begin),
self::liberalCastToCarbon($end),
$userId,
$doorId
);
......
......@@ -42,8 +42,8 @@ class OverrideCreate implements OverrideCreateUseCase
public function create(array $attributes, Presenter $presenter): void
{
$response = new ResponseModel();
$userId = $this->castToInt($attributes['user_id']);
$doorId = $this->castToInt($attributes['door_id']);
$userId = self::castToInt($attributes['user_id']);
$doorId = self::castToInt($attributes['door_id']);
if (!$this->users->get($userId)) {
$response->addError('User does not exist!');
......
......@@ -29,8 +29,8 @@ class OverridesGet implements OverridesGetUseCase
$overrides = $this->overrides->filterHistory(
$doorId,
$userId,
$this->liberalCastToCarbon($start),
$this->liberalCastToCarbon($end)
self::liberalCastToCarbon($start),
self::liberalCastToCarbon($end)
);
$response = new ResponseModel();
......
......@@ -30,8 +30,8 @@ class SchedulesGet implements SchedulesGetUseCase
$schedules = $this->schedules->filter(
$groupId,
$type,
$this->liberalCastToCarbon($begin),
$this->liberalCastToCarbon($end)
self::liberalCastToCarbon($begin),
self::liberalCastToCarbon($end)
);
$response = new ResponseModel($schedules);
......
......@@ -41,12 +41,12 @@ class CreateToken implements CreateTokenUseCase
$tokenString = $this->tokens::generateTokenString();
$date = $this->liberalCastToCarbon($attributes['expires_at'] ?? null);
$date = self::liberalCastToCarbon($attributes['expires_at'] ?? null);
$token = $this->tokens->create(
new Token(
0,
$this->castToInt($attributes['user_id']),
self::castToInt($attributes['user_id']),
HashedSearchable::hash($this->salt, $tokenString),
$name,
$date
......
......@@ -24,7 +24,7 @@ class GetTokens implements GetTokensUseCase
*/
public function filter(?string $userId, ?string $validAt, Presenter $presenter): void
{
$tokens = $this->tokens->filter($userId, $this->liberalCastToCarbon($validAt));
$tokens = $this->tokens->filter($userId, self::liberalCastToCarbon($validAt));
$response = new ResponseModel($tokens);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment