withoutGlobalScope(OrganisationScope::class) ->where('public_token', $token) ->first(); if ($current !== null) { return $current; } $previous = FormSchema::query() ->withoutGlobalScope(OrganisationScope::class) ->where('public_token_previous', $token) ->first(); if ($previous === null) { throw new SchemaNotFoundException; } $rotatedAt = $previous->public_token_rotated_at; if ($rotatedAt === null) { return $previous; } if ($rotatedAt->addDays(self::GRACE_DAYS)->isPast()) { throw new TokenExpiredException; } return $previous; } }