Skip to content

Commit

Permalink
throw http exceptions when possible, try to pass around arrays instea…
Browse files Browse the repository at this point in the history
…d of stringified arrays
  • Loading branch information
DAcodedBEAT committed Dec 13, 2023
1 parent 623677a commit ff946b2
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 225 deletions.
6 changes: 3 additions & 3 deletions src/ChurchCRM/dto/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static function addPerson($PersonID)
throw new \Exception(gettext('PersonID for Cart must be numeric'), 400);
}
if ($PersonID !== null && !in_array($PersonID, $_SESSION['aPeopleCart'], false)) {
array_push($_SESSION['aPeopleCart'], (int) $PersonID);
$_SESSION['aPeopleCart'][] = (int)$PersonID;
}
}

Expand Down Expand Up @@ -194,7 +194,7 @@ public static function getEmailLink()
$emailAddressArray = [];
foreach ($people as $cartPerson) {
if (!empty($cartPerson->getEmail())) {
array_push($emailAddressArray, $cartPerson->getEmail());
$emailAddressArray[] = $cartPerson->getEmail();
}
}
$delimiter = AuthenticationManager::getCurrentUser()->getUserConfigString('sMailtoDelimiter');
Expand All @@ -213,7 +213,7 @@ public static function getSMSLink()
$SMSNumberArray = [];
foreach ($people as $cartPerson) {
if (!empty($cartPerson->getCellPhone())) {
array_push($SMSNumberArray, $cartPerson->getCellPhone());
$SMSNumberArray[] = $cartPerson->getCellPhone();
}
}
$sSMSLink = implode(',', $SMSNumberArray);
Expand Down
4 changes: 1 addition & 3 deletions src/ChurchCRM/dto/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,9 @@ public function send()
$send = (bool) $this->sendProjector();
array_push($methods, 'projector: ' . $send);
}
$sendStatus = [
return [
'status' => '',
'methods' => $methods,
];

return json_encode($sendStatus, JSON_THROW_ON_ERROR);
}
}
80 changes: 54 additions & 26 deletions src/api/routes/calendar/calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Propel\Runtime\Collection\ObjectCollection;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Slim\Routing\RouteCollectorProxy;

$app->group('/calendars', function (RouteCollectorProxy $group) {
Expand All @@ -19,7 +21,7 @@
$group->post('/', 'NewCalendar')->add(AddEventsRoleAuthMiddleware::class);
$group->get('/{id}', 'getUserCalendars');
$group->delete('/{id}', 'deleteUserCalendar');
$group->get('/{id}/events', 'UserCalendar');
$group->get('/{id}/events', 'getUserCalendarEvents');
$group->get('/{id}/fullcalendar', 'getUserCalendarFullCalendarEvents');
$group->post('/{id}/NewAccessToken', 'NewAccessToken')->add(AddEventsRoleAuthMiddleware::class);
$group->delete('/{id}/AccessToken', 'DeleteAccessToken')->add(AddEventsRoleAuthMiddleware::class);
Expand All @@ -41,35 +43,42 @@ function getSystemCalendars(Request $request, Response $response, array $args):
function getSystemCalendarEvents(Request $request, Response $response, array $args): Response
{
$Calendar = SystemCalendars::getCalendarById($args['id']);
$start = $request->getQueryParams()['start'];
$end = $request->getQueryParams()['end'];
if ($Calendar) {
$events = $Calendar->getEvents($start, $end);
return SlimUtils::renderJSON($response, $events);
if (!$Calendar) {
throw new HttpNotFoundException($request, 'Calendar ID not found!');
}

$start = $request->getQueryParams()['start'] ?? '';
$end = $request->getQueryParams()['end'] ?? '';

$events = $Calendar->getEvents($start, $end);

return SlimUtils::renderJSON($response, $events);
}

function getSystemCalendarEventById(Request $request, Response $response, array $args): Response
{
$Calendar = SystemCalendars::getCalendarById($args['id']);

if ($Calendar) {
$event = $Calendar->getEventById($args['eventid']);
return SlimUtils::renderJSON($response, $event);
if (!$Calendar) {
throw new HttpNotFoundException($request, 'Calendar ID not found!');
}

$event = $Calendar->getEventById($args['eventid']);

return SlimUtils::renderJSON($response, $event);
}

function getSystemCalendarFullCalendarEvents(Request $request, Response $response, array $args): Response
{
$Calendar = SystemCalendars::getCalendarById($args['id']);
$start = $request->getQueryParams()['start'];
$end = $request->getQueryParams()['end'];
if (!$Calendar) {
return $response->withStatus(404);
throw new HttpNotFoundException($request, 'Calendar ID not found!');
}

$start = $request->getQueryParams()['start'] ?? '';
$end = $request->getQueryParams()['end'] ?? '';
$Events = $Calendar->getEvents($start, $end);
if (!$Events) {
return $response->withStatus(404);
throw new HttpNotFoundException($request, 'Events not found!');
}

return SlimUtils::renderJSON($response, EventsObjectCollectionToFullCalendar($Events, SystemCalendars::toPropelCalendar($Calendar)));
Expand All @@ -82,9 +91,11 @@ function getUserCalendars(Request $request, Response $response, array $args): Re
$CalendarQuery->filterById($args['id']);
}
$Calendars = $CalendarQuery->find();
if ($Calendars) {
return SlimUtils::renderStringJSON($response, $Calendars->toJSON());
if (!$Calendars) {
throw new HttpNotFoundException($request, 'No calendars returned');
}

return SlimUtils::renderStringJSON($response, $Calendars->toJSON());
}

function getUserCalendarFullCalendarEvents(Request $request, Response $response, array $args): Response
Expand All @@ -93,7 +104,7 @@ function getUserCalendarFullCalendarEvents(Request $request, Response $response,
$calendar = CalendarQuery::create()
->findOneById($CalendarID);
if (!$calendar) {
return $response->withStatus(404);
throw new HttpNotFoundException($request, 'Calendar ID not found!');
}
$start = $request->getQueryParams()['start'];
$end = $request->getQueryParams()['end'];
Expand All @@ -103,12 +114,29 @@ function getUserCalendarFullCalendarEvents(Request $request, Response $response,
->filterByCalendar($calendar)
->find();
if (!$Events) {
return $response->withStatus(404);
throw new HttpNotFoundException($request, 'Events not found!');
}

return SlimUtils::renderJSON($response, EventsObjectCollectionToFullCalendar($Events, $calendar));
}


function getUserCalendarEvents(Request $request, Response $response, array $args): Response
{
$CalendarID = $args['id'];
$calendar = CalendarQuery::create()
->findOneById($CalendarID);
if (!$calendar) {
throw new HttpNotFoundException($request, 'Calendar ID not found!');
}
$start = $request->getQueryParams()['start'] ?? '';
$end = $request->getQueryParams()['end'] ?? '';

$events = $calendar->getEvents($start, $end);

return SlimUtils::renderJSON($response, $events);
}

function EventsObjectCollectionToFullCalendar(ObjectCollection $events, Calendar $calendar): array
{
$formattedEvents = [];
Expand All @@ -123,33 +151,33 @@ function EventsObjectCollectionToFullCalendar(ObjectCollection $events, Calendar
function NewAccessToken(Request $request, Response $response, array $args): Response
{
if (!isset($args['id'])) {
return $response->withStatus(400, gettext('Invalid request: Missing calendar id'));
throw new HttpBadRequestException($request, gettext('Invalid request: Missing calendar id'));
}
$Calendar = CalendarQuery::create()
->findOneById($args['id']);
if (!$Calendar) {
return $response->withStatus(404, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
throw new HttpBadRequestException($request, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
}
$Calendar->setAccessToken(ChurchCRM\Utils\MiscUtils::randomToken());
$Calendar->save();

return SlimUtils::renderJSON($response, $Calendar);
return SlimUtils::renderJSON($response, $Calendar->toArray());
}

function DeleteAccessToken(Request $request, Response $response, array $args): Response
{
if (!isset($args['id'])) {
return $response->withStatus(400, gettext('Invalid request: Missing calendar id'));
throw new HttpBadRequestException($request, gettext('Invalid request: Missing calendar id'));
}
$Calendar = CalendarQuery::create()
->findOneById($args['id']);
if (!$Calendar) {
return $response->withStatus(404, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
throw new HttpBadRequestException($request, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
}
$Calendar->setAccessToken(null);
$Calendar->save();

return SlimUtils::renderJSON($response, $Calendar);
return SlimUtils::renderJSON($response, $Calendar->toArray());
}

function NewCalendar(Request $request, Response $response, $args)
Expand All @@ -167,12 +195,12 @@ function NewCalendar(Request $request, Response $response, $args)
function deleteUserCalendar(Request $request, Response $response, $args)
{
if (!isset($args['id'])) {
return $response->withStatus(400, gettext('Invalid request: Missing calendar id'));
throw new HttpBadRequestException($request, gettext('Invalid request: Missing calendar id'));
}
$Calendar = CalendarQuery::create()
->findOneById($args['id']);
if (!$Calendar) {
return $response->withStatus(404, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
throw new HttpBadRequestException($request, gettext('Not Found: Unknown calendar id') . ': ' . $args['id']);
}
$Calendar->delete();

Expand Down
55 changes: 30 additions & 25 deletions src/api/routes/calendar/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use Propel\Runtime\ActiveQuery\Criteria;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Slim\Routing\RouteCollectorProxy;

$app->group('/events', function (RouteCollectorProxy $group) {
Expand All @@ -37,35 +39,37 @@ function getAllEvents(Request $request, Response $response, array $args): Respon
{
$Events = EventQuery::create()
->find();
if (!empty($Events)) {
return SlimUtils::renderStringJSON($response, $Events->toJSON());
if (empty($Events)) {
throw new HttpNotFoundException($request);
}
return $response->withStatus(404);

return SlimUtils::renderStringJSON($response, $Events->toJSON());
}

function getEventTypes(Request $request, Response $response, array $args): Response
{
$EventTypes = EventTypeQuery::Create()
->orderByName()
->find();
if (!empty($EventTypes)) {
return SlimUtils::renderStringJSON($response, $EventTypes->toJSON());
if (empty($EventTypes)) {
throw new HttpNotFoundException($request);
}
return $response->withStatus(404);
return SlimUtils::renderStringJSON($response, $EventTypes->toJSON());
}

function getEvent(Request $request, Response $response, $args)
{
$Event = $request->getAttribute('event');

if (!empty($Event)) {
return SlimUtils::renderStringJSON($response, $Event->toJSON());
if (empty($Event)) {
throw new HttpNotFoundException($request);
}
return $response->withStatus(404);
return SlimUtils::renderStringJSON($response, $Event->toJSON());
}

function getEventPrimaryContact(Request $request, Response $response, array $args): Response
{
/** @var Event $Event */
$Event = EventQuery::create()
->findOneById($args['id']);
if (!empty($Event)) {
Expand All @@ -74,7 +78,7 @@ function getEventPrimaryContact(Request $request, Response $response, array $arg
return SlimUtils::renderStringJSON($response, $Contact->toJSON());
}
}
return $response->withStatus(404);
throw new HttpNotFoundException($request);
}

function getEventSecondaryContact(Request $request, Response $response, array $args): Response
Expand All @@ -83,33 +87,33 @@ function getEventSecondaryContact(Request $request, Response $response, array $a
->findOneById($args['id'])
->getPersonRelatedBySecondaryContactPersonId();
if (!empty($Contact)) {
return SlimUtils::renderStringJSON($response, $Contact->toJSON());
throw new HttpNotFoundException($request);
}
return $response->withStatus(404);
return SlimUtils::renderStringJSON($response, $Contact->toJSON());
}

function getEventLocation(Request $request, Response $response, array $args): Response
{
$Location = EventQuery::create()
->findOneById($args['id'])
->getLocation();
if (!empty($Location)) {
return SlimUtils::renderStringJSON($response, $Location->toJSON());
if (empty($Location)) {
throw new HttpNotFoundException($request);
}

return $response->withStatus(404);
return SlimUtils::renderStringJSON($response, $Location->toJSON());
}

function getEventAudience(Request $request, Response $response, array $args): Response
{
$Audience = EventQuery::create()
->findOneById($args['id'])
->getEventAudiencesJoinGroup();
if (!empty($Audience)) {
return SlimUtils::renderStringJSON($response, $Audience->toJSON());
if (empty($Audience)) {
throw new HttpNotFoundException($request);
}

return $response->withStatus(404);
return SlimUtils::renderStringJSON($response, $Audience->toJSON());
}

function newEvent(Request $request, Response $response, array $args): Response
Expand All @@ -120,14 +124,14 @@ function newEvent(Request $request, Response $response, array $args): Response
$type = EventTypeQuery::Create()
->findOneById($input['Type']);
if (empty($type)) {
return $response->withStatus(400, gettext('invalid event type id'));
throw new HttpBadRequestException($request, gettext('invalid event type id'));
}

$calendars = CalendarQuery::create()
->filterById($input['PinnedCalendars'])
->find();
if (count($calendars) != count($input['PinnedCalendars'])) {
return $response->withStatus(400, gettext('invalid calendar pinning'));
if (count($calendars) !== count($input['PinnedCalendars'])) {
throw new HttpBadRequestException($request, gettext('invalid calendar pinning'));
}

// we have event type and pined calendars. now create the event.
Expand All @@ -146,9 +150,8 @@ function newEvent(Request $request, Response $response, array $args): Response

function updateEvent(Request $request, Response $response, array $args): Response
{
$e = new Event();
//$e->getId();
$input = $request->getParsedBody();
/** @var Event $Event */
$Event = $request->getAttribute('event');
$id = $Event->getId();
$Event->fromArray($input);
Expand All @@ -159,6 +162,8 @@ function updateEvent(Request $request, Response $response, array $args): Respons
$Event->setCalendars($PinnedCalendars);

$Event->save();

return SlimUtils::renderSuccessJSON($response);
}

function setEventTime(Request $request, Response $response, array $args): Response
Expand All @@ -168,7 +173,7 @@ function setEventTime(Request $request, Response $response, array $args): Respon
$event = EventQuery::Create()
->findOneById($args['id']);
if (!$event) {
return $response->withStatus(404);
throw new HttpNotFoundException($request);
}
$event->setStart($input['startTime']);
$event->setEnd($input['endTime']);
Expand Down Expand Up @@ -210,7 +215,7 @@ function deleteEvent(Request $request, Response $response, array $args): Respons
{
$event = EventQuery::Create()->findOneById($args['id']);
if (!$event) {
return $response->withStatus(404);
throw new HttpNotFoundException($request);
}
$event->delete();

Expand Down
Loading

0 comments on commit ff946b2

Please sign in to comment.