Skip to content

Commit

Permalink
Code cleanup [SLE-192]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed May 6, 2024
1 parent bc79f77 commit 517d2b0
Show file tree
Hide file tree
Showing 43 changed files with 214 additions and 266 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __invoke(
): ResponseInterface {
$clientValues = (array)$request->getParsedBody();

$insertId = $this->clientCreatorFromClientSubmit->createClientFromClientSubmit($clientValues);
$insertId = $this->clientCreatorFromClientSubmit->createClientFromApi($clientValues);

if (0 !== $insertId) {
return $this->jsonResponder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201);
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Client/Enum/ClientStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum ClientStatus: string
case CANNOT_HELP = 'Cannot help';

/**
* Get the enum case name that can be displayed by the frontend.
* Returns the enum case name that can be displayed by the frontend.
*
* @return string
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Client/Enum/ClientVigilanceLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum ClientVigilanceLevel: string
case HIGH = 'high';

/**
* Get the enum case name that can be displayed by the frontend.
* Returns the enum case name that can be displayed by the frontend.
*
* @return string
*/
Expand Down
3 changes: 2 additions & 1 deletion src/Domain/Client/Repository/ClientFinderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function findClientsWithResultAggregate(array $whereArray = ['client.dele
'first_name' => 'client.first_name',
'last_name' => 'client.last_name',
'birthdate' => 'client.birthdate',
'email' => 'client.email',
'location' => 'client.location',
'phone' => 'client.phone',
'sex' => 'client.sex',
Expand Down Expand Up @@ -64,7 +65,7 @@ public function findClientsWithResultAggregate(array $whereArray = ['client.dele
}

/**
* Return post with given id if it exists
* Return client with given id if it exists
* otherwise null.
*
* @param string|int $id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public function findAllClientStatusesMappedByIdName(bool $withoutTranslation = f
$resultRows = $query->execute()->fetchAll('assoc') ?: [];
$statuses = [];
foreach ($resultRows as $resultRow) {
// If status is required without the translation provide value directly from db
// Translation key is created in ClientStatus enum
// If status is required without the translation, provide value directly from db
// Translation is made in ClientStatus enum
$statusName = $withoutTranslation ? $resultRow['name']
: ClientStatus::tryFrom($resultRow['name'])?->getDisplayName();
$statuses[(int)$resultRow['id']] = $statusName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

/**
* For the frontend to know when to display edit and delete icons.
* Admins can edit all notes, users only their own.
*/
final readonly class ClientPrivilegeDeterminer
{
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Client/Service/ClientCreatorFromApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(
*
* @return int
*/
public function createClientFromClientSubmit(array $clientValues): int
public function createClientFromApi(array $clientValues): int
{
// Add default client status (action pending)
$clientValues['client_status_id'] = $this->clientStatusFinderRepository->findClientStatusByName(
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Client/Service/ClientDeleter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(
}

/**
* Delete one post logic.
* Delete one client.
*
* @param int $clientId
*
Expand All @@ -31,7 +31,7 @@ public function __construct(
*/
public function deleteClient(int $clientId): bool
{
// Find post in db to get its ownership
// Find client in db to get its ownership
$clientFromDb = $this->clientFinder->findClient($clientId);

if ($this->clientPermissionVerifier->isGrantedToDelete($clientFromDb->userId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function __construct(

/**
* Build cakephp query builder where array with filter params.
* Filter test examples: @see ClientListActionTest::testClientListWithFilterAction
*
* @param array $filterParams default deleted_at null
*
Expand All @@ -34,10 +35,12 @@ public function buildWhereArrayWithFilterParams(array $filterParams = ['deleted_
// Check if name part are from first name or last name
$namePartsConditions['OR'] = [
[
// First name LIKE first part of name AND last name LIKE second part of name
[$this->columnPrefix . 'first_name LIKE' => '%' . $nameParts[0] . '%'],
[$this->columnPrefix . 'last_name LIKE' => '%' . $nameParts[1] . '%'],
],
[
// First name LIKE second part of name AND last name LIKE first part of name
[$this->columnPrefix . 'first_name LIKE' => '%' . $nameParts[1] . '%'],
[$this->columnPrefix . 'last_name LIKE' => '%' . $nameParts[0] . '%'],
],
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Client/Service/ClientFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function findClient(int $id, bool $includeDeleted = false): ClientData
}

/**
* Find one client in the database with aggregate.
* Find one client in the database with aggregate (main note, assigned user, status, privileges, notes amount).
*
* @param int $clientId
*
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Client/Service/ClientUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public function __construct(
}

/**
* Change something or multiple things on post.
* Change client values.
*
* @param int $clientId id of post being changed
* @param int $clientId id of client being changed
* @param array $clientValues values that user wants to change
*
* @return array update infos containing status and optionally other
Expand Down
2 changes: 0 additions & 2 deletions src/Domain/Exception/Persistence/PersistenceException.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types = 1);

namespace App\Domain\Exception\Persistence;

use Exception;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types = 1);

namespace App\Domain\Exception\Persistence;

class PersistenceRecordNotFoundException extends PersistenceException
Expand Down
6 changes: 5 additions & 1 deletion src/Domain/FilterSetting/Data/FilterData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace App\Domain\FilterSetting\Data;

/**
* Filter chip data.
*/
class FilterData
{
public string $name;
public string $paramName;
public ?string $paramValue;
public ?string $category; // May be null
// Category is the title of the group of filters, e.g. "Status" or "User"
public ?string $category;
public bool $authorized;

public function __construct(array $data)
Expand Down
6 changes: 3 additions & 3 deletions src/Domain/FilterSetting/FilterSettingFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace App\Domain\FilterSetting;

use App\Domain\FilterSetting\Data\FilterData;
use App\Domain\FilterSetting\Repository\UserFilterHandlerRepository;
use App\Domain\FilterSetting\Repository\UserFilterCrudRepository;
use Odan\Session\SessionInterface;

final readonly class FilterSettingFinder
{
public function __construct(
private UserFilterHandlerRepository $userFilterHandlerRepository,
private UserFilterCrudRepository $userFilterCrudRepository,
private FilterSettingSaver $filterSettingSaver,
private SessionInterface $session,
) {
Expand Down Expand Up @@ -77,7 +77,7 @@ public function getActiveAndInactiveFilters(array $allAvailableFilters, FilterMo
*/
public function findFiltersFromAuthenticatedUser(FilterModule $userFilterModule): array
{
return $this->userFilterHandlerRepository->findFiltersFromUser(
return $this->userFilterCrudRepository->findFiltersFromUser(
$this->session->get('user_id'),
$userFilterModule->value
);
Expand Down
10 changes: 5 additions & 5 deletions src/Domain/FilterSetting/FilterSettingSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
namespace App\Domain\FilterSetting;

use App\Application\Data\UserNetworkSessionData;
use App\Domain\FilterSetting\Repository\UserFilterHandlerRepository;
use App\Domain\FilterSetting\Repository\UserFilterCrudRepository;

final readonly class FilterSettingSaver
{
public function __construct(
private UserFilterHandlerRepository $userFilterHandlerRepository,
private UserFilterCrudRepository $userFilterCrudRepository,
private UserNetworkSessionData $userNetworkSessionData,
) {
}

/**
* Remove old filters from db and save given filters.
* This function should really be called only once per request otherwise
* This function should really be called only once per request, otherwise
* only the filters from the last call will be saved.
*
* @param array|null $filters
Expand All @@ -28,7 +28,7 @@ public function saveFilterSettingForAuthenticatedUser(
FilterModule $userFilterModule,
): void {
// Delete previous active filters in database before adding the new ones
$this->userFilterHandlerRepository->deleteFilterSettingFromUser(
$this->userFilterCrudRepository->deleteFilterSettingFromUser(
$this->userNetworkSessionData->userId,
$userFilterModule->value
);
Expand All @@ -39,7 +39,7 @@ public function saveFilterSettingForAuthenticatedUser(
$userFilterRow[$key]['filter_id'] = $filterId;
$userFilterRow[$key]['module'] = $userFilterModule->value;
}
$this->userFilterHandlerRepository->insertUserClientListFilterSetting($userFilterRow);
$this->userFilterCrudRepository->insertUserClientListFilterSetting($userFilterRow);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use App\Infrastructure\Factory\QueryFactory;

final readonly class UserFilterHandlerRepository
final readonly class UserFilterCrudRepository
{
public function __construct(
private QueryFactory $queryFactory,
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/General/Enum/SexOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ enum SexOption: string
// case NULL = '';

/**
* Get the enum case name that can be displayed by the frontend.
* Returns the enum case name that can be displayed by the frontend.
*
* @return string
*/
Expand Down
38 changes: 19 additions & 19 deletions src/Domain/Note/Data/NoteData.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,27 @@ public function __construct(?array $noteValues = null)
*
* @return array
*/
public function toArray(): array
{
$note = [];
// Not include required, from db non-nullable values if they are null -> for update
if ($this->id !== null) {
$note['id'] = $this->id;
}
if ($this->userId !== null) {
$note['user_id'] = $this->userId;
}
if ($this->clientId !== null) {
$note['client_id'] = $this->clientId;
}
/* public function toArrayForDatabase(): array
{
$note = [];
// Not include required, from db non-nullable values if they are null -> for update
if ($this->id !== null) {
$note['id'] = $this->id;
}
if ($this->userId !== null) {
$note['user_id'] = $this->userId;
}
if ($this->clientId !== null) {
$note['client_id'] = $this->clientId;
}
// Message is nullable and null is a valid value, so it has to be included
$note['message'] = $this->message;
$note['is_main'] = $this->isMain;
$note['hidden'] = $this->hidden;
// "message" is nullable, and null is a valid db value, so it has to be included
$note['message'] = $this->message;
$note['is_main'] = $this->isMain;
$note['hidden'] = $this->hidden;
return $note;
}
return $note;
}*/

public function jsonSerialize(): array
{
Expand Down
4 changes: 2 additions & 2 deletions src/Domain/Note/Repository/NoteDeleterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(
}

/**
* Delete note from database.
* Soft delete note from database.
*
* @param int $id
*
Expand All @@ -26,7 +26,7 @@ public function deleteNote(int $id): bool
}

/**
* Delete notes that are linked to client.
* Soft delete notes that are linked to given client.
*
* @param int $clientId
*
Expand Down
46 changes: 2 additions & 44 deletions src/Domain/Note/Repository/NoteFinderRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,6 @@ public function __construct(
) {
}

/**
* Return all notes with users attribute loaded.
*
* @return NoteResultData[]
*/
public function findAllNotesWithUsers(): array
{
$query = $this->queryFactory->selectQuery()->from('note');
$concatName = $query->func()->concat(['user.first_name' => 'identifier', ' ', 'user.surname' => 'identifier']);
$query->select(array_merge($this->noteResultFields, ['user_full_name' => $concatName]))
->join(['table' => 'user', 'conditions' => 'note.user_id = user.id'])
->andWhere(['note.deleted_at IS' => null]);
$resultRows = $query->execute()->fetchAll('assoc') ?: [];

// Convert to list of Note objects with associated User info
return $this->hydrator->hydrate($resultRows, NoteResultData::class);
}

/**
* Return note with given id if it exists
* otherwise null.
Expand All @@ -63,30 +45,6 @@ public function findNoteById(string|int $id): NoteData
return new NoteData($noteRow);
}

/**
* Return all notes with users attribute loaded.
*
* @param int $id
*
* @throws \Exception
*
* @return NoteResultData
*/
public function findNoteWithUserById(int $id): NoteResultData
{
$query = $this->queryFactory->selectQuery()->from('note');

$concatName = $query->func()->concat(['user.first_name' => 'identifier', ' ', 'user.surname' => 'identifier']);

$query->select(array_merge($this->noteResultFields, ['user_full_name' => $concatName]))
->join(['table' => 'user', 'conditions' => 'note.user_id = user.id'])
->andWhere(['note.id' => $id, 'note.deleted_at IS' => null]);
$resultRows = $query->execute()->fetch('assoc') ?: [];

// Instantiate UserNote DTO
return new NoteResultData($resultRows);
}

/**
* Return all notes which are linked to the given user except the main note.
*
Expand Down Expand Up @@ -153,7 +111,7 @@ public function findAllNotesExceptMainWithUserByClientId(int $clientId): array
}

/**
* Returns $notesAmount most recent notes.
* Returns given amount of notes ordered by most recent.
*
* @param int $notesAmount
*
Expand Down Expand Up @@ -189,7 +147,7 @@ public function findMostRecentNotes(int $notesAmount): array
}

/**
* Find amount of notes without counting the main note.
* Find the amount of notes without counting the main note.
*
* @param int $clientId
*
Expand Down
Loading

0 comments on commit 517d2b0

Please sign in to comment.