Skip to content

Commit

Permalink
ApiV1(*)Controller: Describe return types and varibale types
Browse files Browse the repository at this point in the history
  • Loading branch information
sukhwinder33445 committed Jun 14, 2024
1 parent fdbdc7a commit b61d60c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 22 deletions.
38 changes: 29 additions & 9 deletions application/controllers/ApiV1ContactgroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use Ramsey\Uuid\Uuid;
use stdClass;

class ApiV1ContactgroupsController extends CompatController
{
Expand Down Expand Up @@ -44,6 +45,8 @@ public function indexAction(): void
$results = [];
$responseCode = 200;
$db = Database::get();

/** @var ?string $identifier */
$identifier = $request->getParam('identifier');

if ($identifier && ! Uuid::isValid($identifier)) {
Expand Down Expand Up @@ -87,6 +90,8 @@ function (Filter\Condition $condition) {

if ($identifier !== null) {
$stmt->where(['external_uuid = ?' => $identifier]);

/** @var stdClass|false $result */
$result = $db->fetchOne($stmt);

if ($result === false) {
Expand Down Expand Up @@ -123,6 +128,7 @@ function (Filter\Condition $condition) {

$res = $db->select($stmt->offset($offset));
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
$users = $this->fetchUserIdentifiers($row->contactgroup_id);
if ($users) {
Expand Down Expand Up @@ -150,9 +156,7 @@ function (Filter\Condition $condition) {
$this->httpBadRequest('Cannot filter on POST');
}

$data = $request->getPost();

$this->assertValidData($data);
$data = $this->getValidatedData();

$db->beginTransaction();

Expand Down Expand Up @@ -187,9 +191,7 @@ function (Filter\Condition $condition) {
$this->httpBadRequest('Identifier is required');
}

$data = $request->getPost();

$this->assertValidData($data);
$data = $this->getValidatedData();

if ($identifier !== $data['id']) {
$this->httpBadRequest('Identifier mismatch');
Expand Down Expand Up @@ -278,6 +280,7 @@ private function fetchUserIdentifiers(int $contactgroupId): ?array
*/
private function getUserId(string $identifier): int
{
/** @var stdClass|false $user */
$user = Database::get()->fetchOne(
(new Select())
->from('contact')
Expand All @@ -301,6 +304,7 @@ private function getUserId(string $identifier): int
*/
private function getContactgroupId(string $identifier): ?int
{
/** @var stdClass|false $contactgroup */
$contactgroup = Database::get()->fetchOne(
(new Select())
->from('contactgroup')
Expand All @@ -314,7 +318,11 @@ private function getContactgroupId(string $identifier): ?int
/**
* Add a new contactgroup with the given data
*
* @param array<string, mixed> $data
* @param array{
* id: string,
* name: string,
* users?: string[],
* } $data
*/
private function addContactgroup(array $data): void
{
Expand Down Expand Up @@ -364,12 +372,17 @@ private function removeContactgroup(int $id): void
/**
* Assert that the given data contains the required fields
*
* @param array<string, mixed> $data
* @return array{
* id: string,
* name: string,
* users?: string[],
* }
*
* @throws HttpBadRequestException
*/
private function assertValidData(array $data): void
private function getValidatedData(): array
{
$data = $this->getRequest()->getPost();
$msgPrefix = 'Invalid request body: ';

if (! isset($data['id'], $data['name'])
Expand Down Expand Up @@ -398,5 +411,12 @@ private function assertValidData(array $data): void
//TODO: check if users exist, here?
}
}

/** @var array{
* id: string,
* name: string,
* users?: string[],
* } $data */
return $data;
}
}
60 changes: 47 additions & 13 deletions application/controllers/ApiV1ContactsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use ipl\Web\Filter\QueryString;
use ipl\Web\Url;
use Ramsey\Uuid\Uuid;
use stdClass;

class ApiV1ContactsController extends CompatController
{
Expand All @@ -44,6 +45,8 @@ public function indexAction(): void
$results = [];
$responseCode = 200;
$db = Database::get();

/** @var ?string $identifier */
$identifier = $request->getParam('identifier');

if ($identifier && ! Uuid::isValid($identifier)) {
Expand Down Expand Up @@ -91,6 +94,8 @@ function (Filter\Condition $condition) {

if ($identifier !== null) {
$stmt->where(['external_uuid = ?' => $identifier]);

/** @var stdClass|false $result */
$result = $db->fetchOne($stmt);

if ($result === false) {
Expand Down Expand Up @@ -136,6 +141,7 @@ function (Filter\Condition $condition) {

$res = $db->select($stmt->offset($offset));
do {
/** @var stdClass $row */
foreach ($res as $i => $row) {
if ($row->username === null) {
unset($row->username);
Expand Down Expand Up @@ -172,9 +178,7 @@ function (Filter\Condition $condition) {
$this->httpBadRequest('Cannot filter on POST');
}

$data = $request->getPost();

$this->assertValidData($data);
$data = $this->getValidatedData();

$db->beginTransaction();

Expand Down Expand Up @@ -209,9 +213,7 @@ function (Filter\Condition $condition) {
$this->httpBadRequest('Identifier is required');
}

$data = $request->getPost();

$this->assertValidData($data);
$data = $this->getValidatedData();

if ($identifier !== $data['id']) {
$this->httpBadRequest('Identifier mismatch');
Expand Down Expand Up @@ -295,6 +297,7 @@ function (Filter\Condition $condition) {
*/
private function getChannelId(string $channelName): int
{
/** @var stdClass|false $channel */
$channel = Database::get()->fetchOne(
(new Select())
->from('channel')
Expand All @@ -318,14 +321,19 @@ private function getChannelId(string $channelName): int
*/
private function fetchContactAddresses(int $contactId): ?string
{
/** @var array<string, string> $addresses */
$addresses = Database::get()->fetchPairs(
(new Select())
->from('contact_address')
->columns(['type', 'address'])
->where(['contact_id = ?' => $contactId])
);

return ! empty($addresses) ? json_encode($addresses) : null;
if (! empty($addresses)) {
return json_encode($addresses) ?: null;
}

return null;
}

/**
Expand Down Expand Up @@ -360,6 +368,7 @@ private function fetchGroupIdentifiers(int $contactId): ?array
*/
private function getGroupId(string $identifier): int
{
/** @var stdClass|false $group */
$group = Database::get()->fetchOne(
(new Select())
->from('contactgroup')
Expand All @@ -383,6 +392,7 @@ private function getGroupId(string $identifier): int
*/
protected function getContactId(string $identifier): ?int
{
/** @var stdClass|false $contact */
$contact = Database::get()->fetchOne(
(new Select())
->from('contact')
Expand All @@ -396,7 +406,14 @@ protected function getContactId(string $identifier): ?int
/**
* Add a new contact with the given data
*
* @param array<string, mixed> $data
* @param array{
* id: string,
* full_name: string,
* default_channel: string,
* username?: string,
* groups?: string[],
* addresses?: array<string, string>
* } $data
*
* @return void
*/
Expand Down Expand Up @@ -438,7 +455,7 @@ private function assertUniqueUsername(string $username): void
$user = Database::get()->fetchOne(
(new Select())
->from('contact')
->columns(1)
->columns('1')
->where(['username = ?' => $username])
);

Expand Down Expand Up @@ -503,14 +520,20 @@ private function removeContact(int $id): void
/**
* Assert that the given data contains the required fields
*
* @param array<string, mixed> $data
*
* @return void
* @return array{
* id: string,
* full_name: string,
* default_channel: string,
* username?: string,
* groups?: string[],
* addresses?: array<string,string>
* }
*
* @throws HttpBadRequestException
*/
private function assertValidData(array $data): void
private function getValidatedData(): array
{
$data = $this->getRequest()->getPost();
$msgPrefix = 'Invalid request body: ';

if (! isset($data['id'], $data['full_name'], $data['default_channel'])
Expand Down Expand Up @@ -574,5 +597,16 @@ private function assertValidData(array $data): void
$this->httpBadRequest($msgPrefix . 'an invalid email address given');
}
}

/** @var array{
* id: string,
* full_name: string,
* default_channel: string,
* username?: string,
* groups?: string[],
* addresses?: array<string,string>
* } $data
*/
return $data;
}
}

0 comments on commit b61d60c

Please sign in to comment.