Skip to content

Commit

Permalink
use SlimUtils::render* functions in more places
Browse files Browse the repository at this point in the history
  • Loading branch information
DAcodedBEAT committed Dec 14, 2023
1 parent 70316e1 commit cffcd3f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 73 deletions.
2 changes: 1 addition & 1 deletion src/ChurchCRM/Slim/SlimUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function renderStringJSON(Response $response, string $json, int $s
}
public static function renderJSON(Response $response, array $obj, int $status = 200): Response
{
return self::renderStringJson($response, json_encode($obj), $status);
return self::renderStringJson($response, json_encode($obj, JSON_THROW_ON_ERROR), $status);
}

public static function renderRedirect(Response $response, string $url): Response
Expand Down
35 changes: 21 additions & 14 deletions src/api/routes/email/mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ function getMailchimpList(Request $request, Response $response, array $args): Re
$mailchimpService = $request->getAttribute('mailchimpService');
$list = $mailchimpService->getList($listId);

$response->getBody()->write(json_encode(['list' => $list]));

return $response->withHeader('Content-Type', 'application/json');
return SlimUtils::renderJSON($response, ['list' => $list]);
}

function getMailchimpEmailNotInCRM(Request $request, Response $response, array $args): Response
Expand Down Expand Up @@ -57,7 +55,14 @@ function getMailchimpEmailNotInCRM(Request $request, Response $response, array $
}
LoggerUtils::getAppLogger()->debug('MailChimp list ' . $listId . ' now has ' . count($mailchimpListMembers) . ' members');

return SlimUtils::renderJSON($response, ['id' => $list['id'], 'name' => $list['name'], 'members' => $mailchimpListMembers]);
return SlimUtils::renderJSON(
$response,
[
'id' => $list['id'],
'name' => $list['name'],
'members' => $mailchimpListMembers
]
);
}

function getMailChimpMissingSubscribed(Request $request, Response $response, array $args): Response
Expand Down Expand Up @@ -101,7 +106,14 @@ function getMailChimpMissingSubscribed(Request $request, Response $response, arr
}
LoggerUtils::getAppLogger()->debug('MailChimp list ' . $listId . ' now has ' . count($mailchimpListMembers) . ' members');

return SlimUtils::renderJSON($response, ['id' => $list['id'], 'name' => $list['name'], 'members' => $personsNotInMailchimp]);
return SlimUtils::renderJSON(
$response,
[
'id' => $list['id'],
'name' => $list['name'],
'members' => $personsNotInMailchimp
]
);
}

function getFamilyStatus(Request $request, Response $response, array $args): Response
Expand All @@ -116,9 +128,8 @@ function getFamilyStatus(Request $request, Response $response, array $args): Res
'list' => $mailchimpService->isEmailInMailChimp($family->getEmail())
];
}
$response->getBody()->write(json_encode($emailToLists));

return $response->withHeader('Content-Type', 'application/json');
return SlimUtils::renderJSON($response, $emailToLists);
}

function getPersonStatus(Request $request, Response $response, array $args): Response
Expand All @@ -140,9 +151,8 @@ function getPersonStatus(Request $request, Response $response, array $args): Res
'list' => $mailchimpService->isEmailInMailChimp($person->getWorkEmail())
];
}
$response->getBody()->write(json_encode($emailToLists));

return $response->withHeader('Content-Type', 'application/json');
return SlimUtils::renderJSON($response, $emailToLists);
}

function getPeopleWithEmails()
Expand All @@ -157,13 +167,10 @@ function getPeopleWithEmails()
return $list;
}

function checkEmailInList($email, $memberList)
function checkEmailInList(string $email, array $memberList): bool
{
$email = trim(strtolower($email));
$key = array_search($email, array_column($memberList, 'email'));
if ($key > 0) {
return true;
}

return false;
return $key > 0;
}
10 changes: 7 additions & 3 deletions src/api/routes/finance/finance-deposits.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@
return SlimUtils::renderJSON($response, $list->toArray());
});

$group->get('', function (Request $request, Response $response, array $args): Response {
return SlimUtils::renderStringJSON($response, DepositQuery::create()->find()->toJSON());
});
$group->get(
'',
fn(Request $request, Response $response, array $args): Response => SlimUtils::renderStringJSON(
$response,
DepositQuery::create()->find()->toJSON()
)
);

$group->get('/{id:[0-9]+}', function (Request $request, Response $response, array $args): Response {
$id = $args['id'];
Expand Down
32 changes: 22 additions & 10 deletions src/api/routes/people/people-groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
use Slim\Routing\RouteCollectorProxy;

$app->group('/groups', function (RouteCollectorProxy $group) {
$group->get('/', function (Request $request, Response $response) {
return SlimUtils::renderJSON($response, GroupQuery::create()->find()->toArray());
});
$group->get(
'/',
fn(Request $request, Response $response) => SlimUtils::renderJSON(
$response,
GroupQuery::create()->find()->toArray()
)
);

// get the group for the calendar, it's planned to only have the personan calendar and the calendar groups the user belongs to
$group->get('/calendars', function (Request $request, Response $response, array $args): Response {
Expand Down Expand Up @@ -47,13 +51,21 @@
return SlimUtils::renderJSON($response, ['groupsInCart' => $groupsInCart]);
});

$group->get('/{groupID:[0-9]+}', function (Request $request, Response $response, array $args): Response {
return SlimUtils::renderJSON($response, GroupQuery::create()->findOneById($args['groupID'])->toArray());
});

$group->get('/{groupID:[0-9]+}/cartStatus', function (Request $request, Response $response, array $args): Response {
return SlimUtils::renderJSON($response, GroupQuery::create()->findOneById($args['groupID'])->checkAgainstCart());
});
$group->get(
'/{groupID:[0-9]+}',
fn(Request $request, Response $response, array $args): Response => SlimUtils::renderJSON(
$response,
GroupQuery::create()->findOneById($args['groupID'])->toArray()
)
);

$group->get(
'/{groupID:[0-9]+}/cartStatus',
fn(Request $request, Response $response, array $args): Response => SlimUtils::renderJSON(
$response,
GroupQuery::create()->findOneById($args['groupID'])->checkAgainstCart()
)
);

$group->get('/{groupID:[0-9]+}/members', function (Request $request, Response $response, array $args): Response {
$groupID = $args['groupID'];
Expand Down
2 changes: 1 addition & 1 deletion src/api/routes/people/people-person.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$group->get('', function (Request $request, Response $response, array $args): Response {
$person = $request->getAttribute('person');

return $response->withHeader('Content-Type', 'application/json')->write($person->exportTo('JSON'));
return SlimUtils::renderStringJSON($response, $person->exportTo('JSON'));
});

$group->delete('', function (Request $request, Response $response, array $args): Response {
Expand Down
84 changes: 47 additions & 37 deletions src/api/routes/public/public-register.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,41 +38,45 @@ function registerFamilyAPI(Request $request, Response $response, array $args): R
$family->setDateEntered(new DateTime());

$familyMembers = [];
if (!$family->validate()) {
return SlimUtils::renderJSON(
$response,
[
'error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($family->getValidationFailures())
],
400
);
}
foreach ($familyMetadata['people'] as $personMetaData) {
$person = new Person();
$person->setEnteredBy(Person::SELF_REGISTER);
$person->setDateEntered(new DateTime());
$person->setFirstName($personMetaData['firstName']);
$person->setLastName($personMetaData['lastName']);
$person->setGender($personMetaData['gender']);
$person->setFmrId($personMetaData['role']);
$person->setEmail($personMetaData['email']);
$person->setCellPhone($personMetaData['cellPhone']);
$person->setHomePhone($personMetaData['homePhone']);
$person->setWorkPhone($personMetaData['workPhone']);
$person->setFlags($personMetaData['hideAge'] ? '1' : 0);

if ($family->validate()) {
foreach ($familyMetadata['people'] as $personMetaData) {
$person = new Person();
$person->setEnteredBy(Person::SELF_REGISTER);
$person->setDateEntered(new DateTime());
$person->setFirstName($personMetaData['firstName']);
$person->setLastName($personMetaData['lastName']);
$person->setGender($personMetaData['gender']);
$person->setFmrId($personMetaData['role']);
$person->setEmail($personMetaData['email']);
$person->setCellPhone($personMetaData['cellPhone']);
$person->setHomePhone($personMetaData['homePhone']);
$person->setWorkPhone($personMetaData['workPhone']);
$person->setFlags($personMetaData['hideAge'] ? '1' : 0);

$birthday = $personMetaData['birthday'];
if (!empty($birthday)) {
$birthdayDate = DateTime::createFromFormat('m/d/Y', $birthday);
$person->setBirthDay($birthdayDate->format('d'));
$person->setBirthMonth($birthdayDate->format('m'));
$person->setBirthYear($birthdayDate->format('Y'));
}
$birthday = $personMetaData['birthday'];
if (!empty($birthday)) {
$birthdayDate = DateTime::createFromFormat('m/d/Y', $birthday);
$person->setBirthDay($birthdayDate->format('d'));
$person->setBirthMonth($birthdayDate->format('m'));
$person->setBirthYear($birthdayDate->format('Y'));
}

if (!$person->validate()) {
LoggerUtils::getAppLogger()->error('Public Reg Error with the following data: ' . json_encode($personMetaData, JSON_THROW_ON_ERROR));
if (!$person->validate()) {
LoggerUtils::getAppLogger()->error('Public Reg Error with the following data: ' . json_encode($personMetaData, JSON_THROW_ON_ERROR));

return SlimUtils::renderJSON($response, ['error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($person->getValidationFailures())], 401);
}
$familyMembers[] = $person;
return SlimUtils::renderJSON($response, ['error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($person->getValidationFailures())], 401);
}
} else {
return SlimUtils::renderJSON($response, ['error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($family->getValidationFailures())], 400);
$familyMembers[] = $person;
}

$family->save();
Expand All @@ -92,12 +96,18 @@ function registerPersonAPI(Request $request, Response $response, array $args): R
$person->fromJSON($request->getBody());
$person->setEnteredBy(Person::SELF_REGISTER);
$person->setDateEntered(new DateTime());
if ($person->validate()) {
$person->save();

return $response->withHeader('Content-Type', 'application/json')->write($person->exportTo('JSON'));
if (!$person->validate()) {
return SlimUtils::renderJSON(
$response,
[
'error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($person->getValidationFailures())
],
400
);
}

return SlimUtils::renderJSON($response, ['error' => gettext('Validation Error'),
'failures' => ORMUtils::getValidationErrors($person->getValidationFailures())], 400);
$person->save();

return SlimUtils::renderStringJSON($response, $person->exportTo('JSON'));
}
24 changes: 21 additions & 3 deletions src/api/routes/system/system-database.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,15 @@

return SlimUtils::renderJSON(
$response,
json_decode(json_encode($Backup), (bool) JSON_OBJECT_AS_ARRAY)
json_decode(
json_encode(
$Backup,
JSON_THROW_ON_ERROR
),
(bool) JSON_OBJECT_AS_ARRAY,
512,
JSON_THROW_ON_ERROR
)
);
});

Expand Down Expand Up @@ -85,13 +93,23 @@

return SlimUtils::renderJSON(
$response,
json_decode(json_encode($RestoreJob), (bool) JSON_OBJECT_AS_ARRAY)
json_decode(
json_encode(
$RestoreJob,
JSON_THROW_ON_ERROR
),
(bool) JSON_OBJECT_AS_ARRAY,
512,
JSON_THROW_ON_ERROR
)
);
});

$group->get('/download/{filename}', function (Request $request, Response $response, array $args): Response {
$filename = $args['filename'];
BackupDownloader::downloadBackup($filename);

return SlimUtils::renderSuccessJSON($response);
});
})->add(AdminRoleAuthMiddleware::class);

Expand Down Expand Up @@ -199,7 +217,7 @@ function exportChMeetings(Request $request, Response $response, array $args): Re
*
* @return Response The augmented response.
*/
function resetDatabase(Request $request, Response $response)
function resetDatabase(Request $request, Response $response): Response
{
$connection = Propel::getConnection();
$logger = LoggerUtils::getAppLogger();
Expand Down
4 changes: 4 additions & 0 deletions src/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
__DIR__ . '/sundayschool',
__DIR__ . '/v2',
]);
$rectorConfig->skip([
__DIR__ . '/ChurchCRM/model/ChurchCRM/Base',
__DIR__ . '/ChurchCRM/model/ChurchCRM/Map',
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
Expand Down
7 changes: 3 additions & 4 deletions src/setup/routes/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use ChurchCRM\dto\SystemURLs;
use ChurchCRM\Service\AppIntegrityService;
use ChurchCRM\Slim\Request\SlimUtils;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Routing\RouteCollectorProxy;
Expand All @@ -20,16 +21,14 @@

$group->get('SystemIntegrityCheck', function (Request $request, Response $response, array $args): Response {
$AppIntegrity = AppIntegrityService::verifyApplicationIntegrity();
$response->getBody()->write(json_encode($AppIntegrity['status']));

return $response->withHeader('Content-Type', 'application/json');
return SlimUtils::renderJSON($response, $AppIntegrity['status']);
});

$group->get('SystemPrerequisiteCheck', function (Request $request, Response $response, array $args): Response {
$required = AppIntegrityService::getApplicationPrerequisites();
$response->getBody()->write(json_encode($required));

return $response->withHeader('Content-Type', 'application/json');
return SlimUtils::renderJSON($response, $required);
});

$group->post('', function (Request $request, Response $response, array $args): Response {
Expand Down

0 comments on commit cffcd3f

Please sign in to comment.