Skip to content

Commit

Permalink
Merge develop into feat/static-analysis
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/module/autotest/app/user/UserTest.php
#	app/module/user/manager/UserManager.php
#	composer.lock
  • Loading branch information
KminekMatej committed Sep 17, 2024
2 parents d3d1a7c + 28d4687 commit df27b78
Show file tree
Hide file tree
Showing 37 changed files with 167 additions and 184 deletions.
4 changes: 0 additions & 4 deletions app/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ public static function boot(): Container
$configurator->enableTracy($logDir);
$configurator->setTempDirectory($tmpDir);

$configurator->createRobotLoader()
->addDirectory(__DIR__)
->register();

$configurator->addConfig(TEAM_DIR . '/app/config/config.neon');
$configurator->addConfig(TEAM_DIR . '/local/' . ($autotestMode ? 'config.autotest.neon' : 'config.neon'));

Expand Down
34 changes: 17 additions & 17 deletions app/module/admin/manager/MigrationManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tymy\Module\Admin\Manager;

use Closure;
use Exception;
use Nette\Database\Explorer;
use Nette\Utils\DateTime;
Expand All @@ -10,6 +11,8 @@
use Tymy\Module\Admin\Entity\Migration;
use Tymy\Module\Core\Model\BaseModel;

use function count;

/**
* Description of MigrationManager
*/
Expand All @@ -21,6 +24,7 @@ class MigrationManager
private array $log = [];
private bool $tableExists;
private array $migrationsCache = [];
public Closure $logger;

public function __construct(private Explorer $teamDatabase)
{
Expand Down Expand Up @@ -272,9 +276,19 @@ private function splitSqlFile(string $sql, string $delimiter): array
return $output;
}

private function logg($text): void
/**
* Log string using defined logger or to internal log array
*
* @param string $text
* @return void
*/
private function logg(string $text): void
{
$this->log[] = (new DateTime())->format(BaseModel::DATETIME_CZECH_FORMAT) . " " . $text;
if (isset($this->logger)) {
($this->logger)($text);
} else {
$this->log[] = (new DateTime())->format(BaseModel::DATETIME_CZECH_FORMAT) . " " . $text;
}
}

/** @return Migration[] Migrations to perform. First migration might be base migration */
Expand Down Expand Up @@ -404,7 +418,6 @@ public function migrateDown(): void

private function migrateBatch(array $migrations): bool
{
$this->teamDatabase->beginTransaction();
Debugger::timer("migration");
$ok = true;
$mig = null;
Expand All @@ -417,13 +430,6 @@ private function migrateBatch(array $migrations): bool
$msg = "An ERROR happened occured migration: [" . $exc->getMessage() . "], performing rollback.";
Debugger::log($msg);
$this->logg($msg);
try {
$this->teamDatabase->rollBack();
} catch (PDOException $exc) {
if ($exc->getMessage() !== "There is no active transaction") {//avoid throwing errors on autocommit mode or when someone already commits the transaction
throw $exc;
}
}
$ok = false;
$this->saveMigrationRecord($mig);
}
Expand All @@ -432,13 +438,7 @@ private function migrateBatch(array $migrations): bool
return false;
}

try {
$this->teamDatabase->commit();
} catch (PDOException $exc) {
if ($exc->getMessage() !== "There is no active transaction") {//avoid throwing errors on autocommit mode or when someone already commits the transaction
throw $exc;
}
}
$this->teamDatabase->getStructure()->rebuild();

$this->saveMigrationsCache();
$this->logg("Database migrated in " . Debugger::timer("migration") * 1000 . " ms");
Expand Down
27 changes: 27 additions & 0 deletions app/module/admin/migrations/2024-09-15T17-03-48.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

-- SQL Migration file
-- Author: Matěj Kmínek <[email protected]>
-- Created: 15.09.2024 17:03:48


/*
Comment for this migration: (not neccessary, but can be handy)
*/


-- UP:
-- commands that updates database shall be written here:

DELETE FROM `user` WHERE `status` = 'DELETED';
ALTER TABLE `user` CHANGE `status` `status` ENUM('INIT','PLAYER','MEMBER','SICK') CHARACTER SET utf8mb3 COLLATE utf8mb3_general_ci NULL DEFAULT NULL;

-- DOWN:
-- commands that reverts updates from UP section shall be written here:

CALL 'Migrations DOWN are for security reasons temporarily DISABLED. Impossible to migrate down from 2024-09-15T17-03-48, in file 2024-09-15T17-03-48.sql';

-- DOWN migrations are disabled for security reasons.
-- These migrations might often include removal of some existent database columns, which is very dangerous to do on production servers.
-- Whenever we would migrate DOWN, then it would be impossible to migrate UP again, without loss of data.
30 changes: 30 additions & 0 deletions app/module/admin/migrations/2024-09-15T17-16-04.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

-- SQL Migration file
-- Author: Matěj Kmínek <[email protected]>
-- Created: 15.09.2024 17:16:04


/*
Comment for this migration: (not neccessary, but can be handy)
*/


-- UP:
-- commands that updates database shall be written here:

ALTER TABLE `user` ADD `email` VARCHAR(1023) NULL DEFAULT NULL AFTER `password`;
UPDATE `user` SET `email` = (SELECT `email` FROM `usr_mails` WHERE `user_id` = `user`.`id` LIMIT 1);
DROP TABLE `usr_mails`;
DROP VIEW IF EXISTS `v_user`;


-- DOWN:
-- commands that reverts updates from UP section shall be written here:

CALL 'Migrations DOWN are for security reasons temporarily DISABLED. Impossible to migrate down from 2024-09-15T17-16-04, in file 2024-09-15T17-16-04.sql';

-- DOWN migrations are disabled for security reasons.
-- These migrations might often include removal of some existent database columns, which is very dangerous to do on production servers.
-- Whenever we would migrate DOWN, then it would be impossible to migrate UP again, without loss of data.
1 change: 0 additions & 1 deletion app/module/attendance/manager/HistoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ public function getEventHistory(int $eventId): array
{
return $this->mapAll($this->database->table(History::TABLE)
->where("event_id", $eventId)
->where(User::TABLE . ".status != ?", User::STATUS_DELETED)
->order("created DESC")
->order("id ASC")
->fetchAll());
Expand Down
16 changes: 8 additions & 8 deletions app/module/attendance/presenter/api/StatusPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function actionStatusSet(?int $resourceId, ?int $subResourceId): void
$this->respondNotAllowed();
}

private function requestStatusGet(int $resourceId, ?int $subResourceId): void
private function requestStatusGet(int $resourceId, ?int $subResourceId): never
{
$record = null;
try {
Expand All @@ -65,7 +65,7 @@ private function requestStatusGet(int $resourceId, ?int $subResourceId): void
$this->respondOk($record->jsonSerialize());
}

private function requestStatusGetList(): void
private function requestStatusGetList(): never
{
$statuses = null;
try {
Expand All @@ -77,7 +77,7 @@ private function requestStatusGetList(): void
$this->respondOk($this->arrayToJson($statuses));
}

private function requestStatusPost(?int $resourceId): void
private function requestStatusPost(?int $resourceId): never
{
$created = null;
try {
Expand All @@ -89,7 +89,7 @@ private function requestStatusPost(?int $resourceId): void
$this->respondOkCreated($created->jsonSerialize());
}

private function requestStatusPut(int $resourceId, ?int $subResourceId): void
private function requestStatusPut(int $resourceId, ?int $subResourceId): never
{
$updated = null;
try {
Expand All @@ -101,7 +101,7 @@ private function requestStatusPut(int $resourceId, ?int $subResourceId): void
$this->respondOk($updated->jsonSerialize());
}

private function requestStatusDelete(int $resourceId, ?int $subResourceId): void
private function requestStatusDelete(int $resourceId, ?int $subResourceId): never
{
$deletedId = null;
try {
Expand All @@ -113,7 +113,7 @@ private function requestStatusDelete(int $resourceId, ?int $subResourceId): void
$this->respondDeleted($deletedId);
}

private function requestStatusSetPost(?int $resourceId): void
private function requestStatusSetPost(?int $resourceId): never
{
$created = null;
try {
Expand All @@ -125,7 +125,7 @@ private function requestStatusSetPost(?int $resourceId): void
$this->respondOkCreated($created->jsonSerialize());
}

private function requestStatusSetPut(int $resourceId, ?int $subResourceId): void
private function requestStatusSetPut(int $resourceId, ?int $subResourceId): never
{
$updated = null;
try {
Expand All @@ -137,7 +137,7 @@ private function requestStatusSetPut(int $resourceId, ?int $subResourceId): void
$this->respondOk($updated->jsonSerialize());
}

private function requestStatusSetDelete(int $resourceId, ?int $subResourceId): void
private function requestStatusSetDelete(int $resourceId, ?int $subResourceId): never
{
$deletedId = null;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function actionDefault($resourceId): void
$this->requestGetList($resourceId);
}

private function requestGetList(int $userId): void
private function requestGetList(int $userId): never
{
$rights = null;
try {
Expand Down
12 changes: 0 additions & 12 deletions app/module/autotest/app/user/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ public function testCRUD(): void

$this->change($recordId);

$this->request($this->getBasePath() . "/$recordId", "DELETE")->expect(405);

$this->deleteRecord($recordId);
}

Expand Down Expand Up @@ -351,16 +349,6 @@ public function mockRecord(): array
return $this->recordManager->mockUser();
}

/**
* Override deleting function - user is not being deleted normally, he gets marked as deleted in status
*
* @param int $recordId
*/
public function deleteRecord($recordId): void
{
$this->request($this->getBasePath() . "/$recordId", "PUT", ["status" => "DELETED"])->expect(200, "array");
}

protected function mockChanges(): array
{
return [
Expand Down
20 changes: 6 additions & 14 deletions app/module/autotest/data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,18 @@ TRUNCATE `notes_history`;
TRUNCATE `push_notification`;
TRUNCATE `pwd_reset`;
TRUNCATE `rights`;
TRUNCATE `usr_mails`;
TRUNCATE `user`;

SET FOREIGN_KEY_CHECKS=1;

/** IMPORT SECTION */

INSERT INTO `user` (`user_name`, `password`, `can_login`, `status`, `roles`, `first_name`, `last_name`, `call_name`, `editable_call_name`, `email_name`,`language`, `sex`, `gdpr_accepted_at`, `last_read_news`) VALUES
('autotest_admin', 'f4ad5b4e691802fca51711dede771a36', 1, 'PLAYER', 'SUPER,USR,ATT', 'Autotest', 'admin', 'autotest-admin', 0, 'autotest-admin', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_user', '58d26e9a3381ace5e682dc26bf780dd4', 1, 'PLAYER', '', 'Autotest', 'user', 'autotest-user', 0, 'autotest-user', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_member', '58d26e9a3381ace5e682dc26bf780dd4', 1, 'MEMBER', '', 'Autotest', 'member', 'autotest-member', 0, 'autotest-member', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_init', '58d26e9a3381ace5e682dc26bf780dd4', 1, 'INIT', '', 'Autotest', 'init', 'autotest-init', 0, 'autotest-init', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_sick', '58d26e9a3381ace5e682dc26bf780dd4', 1, 'SICK', '', 'Autotest', 'sick', 'autotest-sick', 0, 'autotest-sick', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

INSERT INTO `usr_mails`(`user_id`, `email`, `type`) VALUES
(1,'[email protected]','DEF'),
(2,'[email protected]','DEF'),
(3,'[email protected]','DEF'),
(4,'[email protected]','DEF'),
(5,'[email protected]','DEF');
INSERT INTO `user` (`user_name`, `password`, `email`, `can_login`, `status`, `roles`, `first_name`, `last_name`, `call_name`, `editable_call_name`, `email_name`,`language`, `sex`, `gdpr_accepted_at`, `last_read_news`) VALUES
('autotest_admin', 'f4ad5b4e691802fca51711dede771a36', '[email protected]', 1, 'PLAYER', 'SUPER,USR,ATT', 'Autotest', 'admin', 'autotest-admin', 0, 'autotest-admin', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_user', '58d26e9a3381ace5e682dc26bf780dd4', '[email protected]', 1, 'PLAYER', '', 'Autotest', 'user', 'autotest-user', 0, 'autotest-user', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_member', '58d26e9a3381ace5e682dc26bf780dd4' ,'[email protected]', 1, 'MEMBER', '', 'Autotest', 'member', 'autotest-member', 0, 'autotest-member', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_init', '58d26e9a3381ace5e682dc26bf780dd4', '[email protected]', 1, 'INIT', '', 'Autotest', 'init', 'autotest-init', 0, 'autotest-init', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP),
('autotest_sick', '58d26e9a3381ace5e682dc26bf780dd4','[email protected]', 1, 'SICK', '', 'Autotest', 'sick', 'autotest-sick', 0, 'autotest-sick', 'CZ', 'MALE', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP);

INSERT INTO `rights` (`id`, `right_type`, `name`, `caption`, `a_roles`, `r_roles`, `a_statuses`, `r_statuses`, `a_users`, `r_users`, `updated`, `updated_user_id`) VALUES
(1, 'SYS', 'EVE_CREATE', 'Create event', 'ATT', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
Expand Down
1 change: 0 additions & 1 deletion app/module/core/factory/FormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ public function createUserConfigForm(Closure $onSuccess, ?User $user): Form
User::STATUS_PLAYER => $this->translator->translate("team.PLAYER", 1),
User::STATUS_MEMBER => $this->translator->translate("team.MEMBER", 1),
User::STATUS_SICK => $this->translator->translate("team.SICK", 1),
User::STATUS_DELETED => $this->translator->translate("team.DELETED", 1),
];

$rolesList = [
Expand Down
24 changes: 12 additions & 12 deletions app/module/core/presenter/api/BasePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private function decodeJsonData(): void
}
}

protected function requestGet(int $resourceId, ?int $subResourceId): void
protected function requestGet(int $resourceId, ?int $subResourceId): never
{
$record = null;
try {
Expand All @@ -109,7 +109,7 @@ protected function requestGet(int $resourceId, ?int $subResourceId): void
$this->respondOk($record->jsonSerialize());
}

protected function requestPost(?int $resourceId): void
protected function requestPost(?int $resourceId): never
{
$created = null;
try {
Expand All @@ -121,7 +121,7 @@ protected function requestPost(?int $resourceId): void
$this->respondOkCreated($created->jsonSerialize());
}

protected function requestPut(int $resourceId, ?int $subResourceId): void
protected function requestPut(int $resourceId, ?int $subResourceId): never
{
$updated = null;
try {
Expand All @@ -133,7 +133,7 @@ protected function requestPut(int $resourceId, ?int $subResourceId): void
$this->respondOk($updated->jsonSerialize());
}

protected function requestDelete(int $resourceId, ?int $subResourceId): void
protected function requestDelete(int $resourceId, ?int $subResourceId): never
{
$deletedId = null;
try {
Expand Down Expand Up @@ -179,42 +179,42 @@ protected function respondByException(Throwable $exc): void
throw $exc;
}

protected function respondOk($payload = null): void
protected function respondOk($payload = null): never
{
$this->responder->A200_OK($payload);
}

protected function respondOkCreated($payload = null): void
protected function respondOkCreated($payload = null): never
{
$this->responder->A201_CREATED($payload);
}

protected function respondDeleted($id): void
protected function respondDeleted($id): never
{
$this->respondOk(["id" => (int) $id]);
}

protected function respondBadRequest($message = null): void
protected function respondBadRequest($message = null): never
{
$this->responder->E400_BAD_REQUEST($message);
}

protected function respondUnauthorized(): void
protected function respondUnauthorized(): never
{
$this->responder->E401_UNAUTHORIZED();
}

protected function respondForbidden(): void
protected function respondForbidden(): never
{
$this->responder->E403_FORBIDDEN("Nedostatečná práva");
}

protected function respondNotFound(): void
protected function respondNotFound(): never
{
$this->responder->E404_NOT_FOUND();
}

protected function respondNotAllowed(): void
protected function respondNotAllowed(): never
{
$this->responder->E405_METHOD_NOT_ALLOWED();
}
Expand Down
Loading

0 comments on commit df27b78

Please sign in to comment.