Commit 8a3d09e9 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

use CastsTo trait

parent 1f31472e
......@@ -5,19 +5,22 @@ namespace Source\Gateways\DoorGroup;
use App\Door;
use App\Group;
use Source\Sanitize\CastsTo;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Doors\DatabaseDoorsRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
class DatabaseDoorGroupRepository implements DoorGroupRepository
{
use CastsTo;
/**
* @inheritDoc
*/
public function getGroupsForDoor(string $doorId): array
{
/** @var Door|null $door */
$door = Door::find((int)$doorId);
$door = Door::find(($this->castToInt($doorId));
if (!$door) {
throw new EntityNotFoundException();
......@@ -34,7 +37,7 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function getDoorsForGroup(string $groupId): array
{
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
......@@ -51,9 +54,9 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function addDoorToGroup(string $doorId, string $groupId): void
{
/** @var Door|null $door */
$door = Door::find((int)$doorId);
$door = Door::find($this->castToInt($doorId));
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$door) {
throw new EntityNotFoundException('Door not found.');
......@@ -72,9 +75,9 @@ class DatabaseDoorGroupRepository implements DoorGroupRepository
public function removeDoorFromGroup(string $doorId, string $groupId): void
{
/** @var Door|null $door */
$door = Door::find((int)$doorId);
$door = Door::find($this->castToInt($doorId));
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$door) {
throw new EntityNotFoundException('Door not found.');
......
......@@ -4,11 +4,14 @@
namespace Source\Gateways\Doors;
use Source\Entities\Door;
use Source\Sanitize\CastsTo;
use Source\Entities\HashedSearchable;
use Source\Exceptions\EntityExistsException;
class DatabaseDoorsRepository implements DoorsRepository
{
use CastsTo;
/**
* @inheritDoc
*/
......@@ -48,6 +51,10 @@ class DatabaseDoorsRepository implements DoorsRepository
*/
public function getByToken(?HashedSearchable $token): ?Door
{
if (!$token) {
return null;
}
$dbDoor = \App\Door::where('api_token', $token->getHash())->first();
if (!$dbDoor) {
......@@ -62,7 +69,7 @@ class DatabaseDoorsRepository implements DoorsRepository
*/
public function get(string $doorId): ?Door
{
if (!($door = \App\Door::find((int)$doorId))) {
if (!($door = \App\Door::find($this->castToInt($doorId)))) {
return null;
}
......@@ -101,7 +108,7 @@ class DatabaseDoorsRepository implements DoorsRepository
*/
public function update(string $doorId, Door $door): ?Door
{
$dbDoor = \App\Door::find((int)$doorId);
$dbDoor = \App\Door::find($this->castToInt($doorId));
if (!$dbDoor) {
return null;
......@@ -121,7 +128,7 @@ class DatabaseDoorsRepository implements DoorsRepository
*/
public function delete(string $doorId): bool
{
return \App\Door::destroy((int)$doorId);
return \App\Door::destroy($this->castToInt($doorId));
}
/**
......
......@@ -5,19 +5,22 @@ namespace Source\Gateways\GroupUser;
use App\User;
use App\Group;
use Source\Sanitize\CastsTo;
use Source\Exceptions\EntityNotFoundException;
use Source\Gateways\Users\DatabaseUsersRepository;
use Source\Gateways\Groups\DatabaseGroupsRepository;
class DatabaseGroupUserRepository implements GroupUserRepository
{
use CastsTo;
/**
* @inheritDoc
*/
public function getGroupsForUser(string $userId): array
{
/** @var User|null $user */
$user = User::find((int)$userId);
$user = User::find($this->castToInt($userId));
if (!$user) {
throw new EntityNotFoundException();
......@@ -34,7 +37,7 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function getUsersForGroup(string $groupId): array
{
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$group) {
throw new EntityNotFoundException();
......@@ -51,9 +54,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function addUserToGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::find((int)$userId);
$user = User::find($this->castToInt($userId));
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$user) {
throw new EntityNotFoundException('User not found.');
......@@ -72,9 +75,9 @@ class DatabaseGroupUserRepository implements GroupUserRepository
public function removeUserFromGroup(string $userId, string $groupId): void
{
/** @var User|null $user */
$user = User::find((int)$userId);
$user = User::find($this->castToInt($userId));
/** @var Group|null $group */
$group = Group::find((int)$groupId);
$group = Group::find($this->castToInt($groupId));
if (!$user) {
throw new EntityNotFoundException('User not found.');
......
......@@ -4,15 +4,18 @@
namespace Source\Gateways\Groups;
use Source\Entities\Group;
use Source\Sanitize\CastsTo;
class DatabaseGroupsRepository implements GroupsRepository
{
use CastsTo;
/**
* @inheritDoc
*/
public function get(string $groupId): ?Group
{
$group = \App\Group::find((int)$groupId);
$group = \App\Group::find($this->castToInt($groupId));
if (!$group) {
return null;
......@@ -73,7 +76,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function update(string $groupId, Group $group): ?Group
{
$dbGroup = \App\Group::find((int)$groupId);
$dbGroup = \App\Group::find($this->castToInt($groupId));
if (!$dbGroup) {
return null;
......@@ -91,7 +94,7 @@ class DatabaseGroupsRepository implements GroupsRepository
*/
public function delete(string $groupId): bool
{
return \App\Group::destroy((int)$groupId);
return \App\Group::destroy($this->castToInt($groupId));
}
/**
......
......@@ -4,17 +4,20 @@
namespace Source\Gateways\Users;
use Source\Entities\User;
use Source\Sanitize\CastsTo;
use Source\Entities\Password;
use Source\Entities\HashedSearchable;
class DatabaseUsersRepository implements UsersRepository
{
use CastsTo;
/**
* @inheritDoc
*/
public function get(string $userId): ?User
{
$user = \App\User::find((int)$userId);
$user = \App\User::find($this->castToInt($userId));
if (!$user) {
return null;
......@@ -95,7 +98,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function update(string $userId, User $user): ?User
{
$dbUser = \App\User::find((int)$userId);
$dbUser = \App\User::find($this->castToInt($userId));
if (!$dbUser) {
return null;
......@@ -113,7 +116,7 @@ class DatabaseUsersRepository implements UsersRepository
*/
public function delete(string $userId): bool
{
return \App\User::destroy((int)$userId);
return \App\User::destroy($this->castToInt($userId));
}
/**
......
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