Skip to content

Commit

Permalink
No Rows (#262)
Browse files Browse the repository at this point in the history
Stop using `Row` when another object should be used.
  • Loading branch information
spaze committed Jan 2, 2024
2 parents 5b11a52 + aed6f6b commit fed3b53
Show file tree
Hide file tree
Showing 51 changed files with 1,063 additions and 315 deletions.
2 changes: 1 addition & 1 deletion site/app/Admin/Presenters/SignPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function actionIn(): void
$this->getSession()->start();
$token = $this->authenticator->verifyPermanentLogin();
if ($token !== null) {
$this->user->login($this->authenticator->getIdentity($token->userId, $token->username));
$this->user->login($this->authenticator->getIdentity($token->getUserId(), $token->getUsername()));
$this->authenticator->regeneratePermanentLogin($this->user);
$this->restoreRequest($this->backlink);
$this->redirect('Homepage:');
Expand Down
6 changes: 4 additions & 2 deletions site/app/Admin/Presenters/TrainingsPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
use MichalSpacekCz\Training\Reviews\TrainingReviewInputs;
use MichalSpacekCz\Training\Reviews\TrainingReviewInputsFactory;
use MichalSpacekCz\Training\Reviews\TrainingReviews;
use MichalSpacekCz\Training\Statuses;
use MichalSpacekCz\Training\Statuses\Statuses;
use MichalSpacekCz\Training\Statuses\TrainingStatusHistory;
use MichalSpacekCz\Training\Trainings\Trainings;
use Nette\Application\BadRequestException;
use Nette\Utils\Html;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function __construct(
private readonly TrainingDates $trainingDates,
private readonly UpcomingTrainingDates $upcomingTrainingDates,
private readonly Statuses $trainingStatuses,
private readonly TrainingStatusHistory $trainingStatusHistory,
private readonly Trainings $trainings,
private readonly TrainingReviews $trainingReviews,
private readonly DateTimeFormatter $dateTimeFormatter,
Expand Down Expand Up @@ -192,7 +194,7 @@ public function actionApplication(int $param): void
$this->template->allowFiles = in_array($this->application->getStatus(), $this->trainingStatuses->getAllowFilesStatuses());
$this->template->toBeInvited = in_array($this->application->getStatus(), $this->trainingStatuses->getParentStatuses(Statuses::STATUS_INVITED));
$this->template->accessToken = $this->application->getAccessToken();
$this->template->history = $this->trainingStatuses->getStatusHistory($param);
$this->template->history = $this->trainingStatusHistory->getStatusHistory($param);
}


Expand Down
6 changes: 3 additions & 3 deletions site/app/Admin/Presenters/templates/Emails/default.latte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</td>
<td><small><code><span title="{$application->getStatusTime()|localeDay} {$application->getStatusTime()|date:'H:i:s'}">{$application->getStatus()}</span>{if $application->getNextStatus()}{$application->getNextStatus()}{/if}</code></small></td>
<td>
{if in_array($application->getNextStatus(), [\MichalSpacekCz\Training\Statuses::STATUS_INVOICE_SENT, \MichalSpacekCz\Training\Statuses::STATUS_INVOICE_SENT_AFTER])}
{if in_array($application->getNextStatus(), [MichalSpacekCz\Training\Statuses\Statuses::STATUS_INVOICE_SENT, MichalSpacekCz\Training\Statuses\Statuses::STATUS_INVOICE_SENT_AFTER])}
{if $application->getPrice() && $application->getVatRate() && $application->getPriceVat()}
{input invoiceId}
{input invoice}
Expand All @@ -56,7 +56,7 @@
{/if}
</td>
</tr>
<tr n:if="$application->getNextStatus() == \MichalSpacekCz\Training\Statuses::STATUS_MATERIALS_SENT">
<tr n:if="$application->getNextStatus() == \MichalSpacekCz\Training\Statuses\Statuses::STATUS_MATERIALS_SENT">
<td></td>
<td></td>
<td colspan="5">{input feedbackRequest}</td>
Expand All @@ -68,7 +68,7 @@
<p>
Template: <code>{$application->getMailMessage()->getBasename()}</code><br>
Subject: <em>{$application->getMailMessage()->getSubject()}</em>
{if $application->getNextStatus() === \MichalSpacekCz\Training\Statuses::STATUS_INVOICE_SENT || $application->getNextStatus() === \MichalSpacekCz\Training\Statuses::STATUS_INVOICE_SENT_AFTER}
{if $application->getNextStatus() === \MichalSpacekCz\Training\Statuses\Statuses::STATUS_INVOICE_SENT || $application->getNextStatus() === \MichalSpacekCz\Training\Statuses\Statuses::STATUS_INVOICE_SENT_AFTER}
<br>{label cc /} {input cc}
{/if}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
<tbody id="historie-container" class="hidden" data-display="table-row-group">
{formContainer statusHistoryDelete}
<tr n:foreach="$history as $record" class="row">
<td><code>{$record->status}</code></td>
<td><small>{$record->statusTime}</small><br></td>
{var $statusTime = $record->getStatusTime()->format('Y-m-d H:i:s')}
<td><code>{$record->getStatus()}</code></td>
<td><small>{$statusTime}</small><br></td>
<td class="center">
<button n:name="$record->id" class="confirm-click" title="Odstranit status z historie" data-confirm="Odstranit status {$record->status} {$record->statusTime} z historie?">{icon times}</button>
<button n:name="(string)$record->getId()" class="confirm-click" title="Odstranit status z historie" data-confirm="Odstranit status {$record->getStatus()} {$statusTime} z historie?">{icon times}</button>
</td>
</tr>
{/formContainer}
Expand Down
2 changes: 1 addition & 1 deletion site/app/Admin/Presenters/templates/Trainings/date.latte
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
</div>
<p>{label date /} {input date} {input submit id => submit}</p>
<p>
<button class="preset" data-preset="{\MichalSpacekCz\Training\Statuses::STATUS_ATTENDED}" data-start="{$trainingStart}">Přednastavit <small><code>{\MichalSpacekCz\Training\Statuses::STATUS_ATTENDED}</code></small></button>
<button class="preset" data-preset="{\MichalSpacekCz\Training\Statuses\Statuses::STATUS_ATTENDED}" data-start="{$trainingStart}">Přednastavit <small><code>{\MichalSpacekCz\Training\Statuses\Statuses::STATUS_ATTENDED}</code></small></button>
{capture $title}Tykat všem účastníkům ve stavu {foreach $attendedStatuses as $status}{$status}{sep}, {/sep}{/foreach}{/capture}
{input familiar title => $title}
</p>
Expand Down
18 changes: 11 additions & 7 deletions site/app/Form/Pulse/PasswordsStorageAlgorithmFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use MichalSpacekCz\Form\FormFactory;
use MichalSpacekCz\Form\UiForm;
use MichalSpacekCz\Pulse\Companies;
use MichalSpacekCz\Pulse\Passwords\Algorithms\PasswordHashingAlgorithms;
use MichalSpacekCz\Pulse\Passwords\Disclosures\PasswordHashingDisclosures;
use MichalSpacekCz\Pulse\Passwords\Passwords;
use MichalSpacekCz\Pulse\Passwords\Storage\StorageSpecificSite;
use MichalSpacekCz\Pulse\Sites;
Expand All @@ -21,6 +23,8 @@ public function __construct(
private Companies $companies,
private Sites $sites,
private Passwords $passwords,
private PasswordHashingAlgorithms $hashingAlgorithms,
private PasswordHashingDisclosures $hashingDisclosures,
) {
}

Expand All @@ -36,7 +40,7 @@ public function create(callable $onSuccess, int $newDisclosures): UiForm
$companyContainer = $form->addContainer('company');
$items = [];
foreach ($this->companies->getAll() as $company) {
$items[$company->id] = $company->name;
$items[$company->getId()] = $company->getCompanyName();
}
$selectCompany = $companyContainer->addSelect('id', 'Company:', $items)
->setPrompt('- select company -');
Expand All @@ -57,7 +61,7 @@ public function create(callable $onSuccess, int $newDisclosures): UiForm
$siteContainer = $form->addContainer('site');
$items = [Sites::ALL => 'all sites'];
foreach ($this->sites->getAll() as $site) {
$items[$site->id] = "{$site->alias} ({$site->url})";
$items[$site->getId()] = "{$site->getAlias()} ({$site->getUrl()})";
}
$selectSite = $siteContainer->addSelect('id', 'Site:', $items)
->setPrompt('- select site -');
Expand Down Expand Up @@ -88,8 +92,8 @@ public function create(callable $onSuccess, int $newDisclosures): UiForm
// Algo
$algoContainer = $form->addContainer('algo');
$items = [];
foreach ($this->passwords->getAlgorithms() as $algo) {
$items[$algo->id] = $algo->algo;
foreach ($this->hashingAlgorithms->getAlgorithms() as $algo) {
$items[$algo->getId()] = $algo->getName();
}
$selectAlgo = $algoContainer->addSelect('id', 'Algorithm:', $items)
->setPrompt('- select algorithm -');
Expand Down Expand Up @@ -120,8 +124,8 @@ public function create(callable $onSuccess, int $newDisclosures): UiForm

// Disclosures
$items = [];
foreach ($this->passwords->getDisclosureTypes() as $disclosure) {
$items[$disclosure->id] = $disclosure->type;
foreach ($this->hashingDisclosures->getDisclosureTypes() as $disclosure) {
$items[$disclosure->getId()] = $disclosure->getType();
}
$disclosureContainer = $form->addContainer('disclosure');
$disclosureNewContainer = $disclosureContainer->addContainer('new');
Expand Down Expand Up @@ -201,7 +205,7 @@ private function validatePasswordsStorages(UiForm $form, ArrayHash $values): voi
if (!empty($values->site->new->url) && $this->sites->getByUrl($values->site->new->url)) {
$form->addError('Can\'t add new site, duplicated URL');
}
if (!empty($values->algo->new->algo) && $this->passwords->getAlgorithmByName($values->algo->new->algo)) {
if (!empty($values->algo->new->algo) && $this->hashingAlgorithms->getAlgorithmByName($values->algo->new->algo)) {
$form->addError('Can\'t add new algorithm, duplicated name');
}
}
Expand Down
10 changes: 5 additions & 5 deletions site/app/Form/TrainingApplicationAdminFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use MichalSpacekCz\Training\Dates\UpcomingTrainingDates;
use MichalSpacekCz\Training\Exceptions\TrainingDateDoesNotExistException;
use MichalSpacekCz\Training\Exceptions\TrainingDateNotRemoteNoVenueException;
use MichalSpacekCz\Training\Statuses;
use MichalSpacekCz\Training\Statuses\TrainingStatusHistory;
use Nette\Forms\Controls\BaseControl;
use Nette\Forms\Controls\Checkbox;
use Nette\Forms\Controls\SubmitButton;
Expand All @@ -25,7 +25,7 @@ public function __construct(
private TrainingDates $trainingDates,
private UpcomingTrainingDates $upcomingTrainingDates,
private TrainingControlsFactory $trainingControlsFactory,
private Statuses $trainingStatuses,
private TrainingStatusHistory $trainingStatusHistory,
) {
}

Expand Down Expand Up @@ -79,12 +79,12 @@ public function create(callable $onSuccess, callable $onStatusHistoryDeleteSucce

$containerName = 'statusHistoryDelete';
$historyContainer = $form->addContainer($containerName);
foreach ($this->trainingStatuses->getStatusHistory($application->getId()) as $history) {
foreach ($this->trainingStatusHistory->getStatusHistory($application->getId()) as $history) {
$historyContainer
->addSubmit((string)$history->id)
->addSubmit((string)$history->getId())
->setValidationScope([$form[$containerName]])
->onClick[] = function (SubmitButton $button) use ($application, $onStatusHistoryDeleteSuccess): void {
$this->trainingStatuses->deleteHistoryRecord($application->getId(), (int)$button->getName());
$this->trainingStatusHistory->deleteHistoryRecord($application->getId(), (int)$button->getName());
$onStatusHistoryDeleteSuccess();
};
}
Expand Down
2 changes: 1 addition & 1 deletion site/app/Form/TrainingApplicationMultipleFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use MichalSpacekCz\Http\HttpInput;
use MichalSpacekCz\Training\Applications\TrainingApplicationStorage;
use MichalSpacekCz\Training\Dates\TrainingDate;
use MichalSpacekCz\Training\Statuses;
use MichalSpacekCz\Training\Statuses\Statuses;

readonly class TrainingApplicationMultipleFormFactory
{
Expand Down
2 changes: 1 addition & 1 deletion site/app/Form/TrainingMailsOutboxFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use MichalSpacekCz\Training\Applications\TrainingApplication;
use MichalSpacekCz\Training\Applications\TrainingApplicationStorage;
use MichalSpacekCz\Training\Mails\TrainingMails;
use MichalSpacekCz\Training\Statuses;
use MichalSpacekCz\Training\Statuses\Statuses;
use Nette\Application\Application as NetteApplication;
use Nette\Application\UI\Presenter;
use stdClass;
Expand Down
2 changes: 1 addition & 1 deletion site/app/Form/TrainingStatusesFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use MichalSpacekCz\Form\Controls\TrainingControlsFactory;
use MichalSpacekCz\Training\Applications\TrainingApplication;
use MichalSpacekCz\Training\Applications\TrainingApplications;
use MichalSpacekCz\Training\Statuses;
use MichalSpacekCz\Training\Statuses\Statuses;
use Nette\Utils\Html;

readonly class TrainingStatusesFormFactory
Expand Down
36 changes: 29 additions & 7 deletions site/app/Pulse/Companies.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use DateTime;
use Nette\Database\Explorer;
use Nette\Database\Row;

readonly class Companies
{
Expand All @@ -17,19 +16,42 @@ public function __construct(


/**
* Get all companies.
*
* @return Row[]
* @return list<Company>
*/
public function getAll(): array
{
return $this->database->fetchAll('SELECT id, name, alias FROM companies ORDER BY name');
$rows = $this->database->fetchAll(
'SELECT
id,
name,
trade_name AS tradeName,
alias,
COALESCE(trade_name, name) AS sortName
FROM companies
ORDER BY name',
);
$companies = [];
foreach ($rows as $row) {
$companies[] = new Company($row->id, $row->name, $row->tradeName, $row->alias, $row->sortName);
}
return $companies;
}


public function getByName(string $name): ?Row
public function getByName(string $name): ?Company
{
return $this->database->fetch('SELECT id, name, alias FROM companies WHERE name = ?', $name);
$row = $this->database->fetch(
'SELECT
id,
name,
trade_name AS tradeName,
alias,
COALESCE(trade_name, name) AS sortName
FROM companies
WHERE name = ?',
$name,
);
return $row ? new Company($row->id, $row->name, $row->tradeName, $row->alias, $row->sortName) : null;
}


Expand Down
48 changes: 48 additions & 0 deletions site/app/Pulse/Passwords/Algorithms/PasswordHashingAlgorithm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Pulse\Passwords\Algorithms;

readonly class PasswordHashingAlgorithm
{

public function __construct(
private int $id,
private string $name,
private string $alias,
private bool $salted,
private bool $stretched,
) {
}


public function getId(): int
{
return $this->id;
}


public function getName(): string
{
return $this->name;
}


public function getAlias(): string
{
return $this->alias;
}


public function isSalted(): bool
{
return $this->salted;
}


public function isStretched(): bool
{
return $this->stretched;
}

}
69 changes: 69 additions & 0 deletions site/app/Pulse/Passwords/Algorithms/PasswordHashingAlgorithms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
declare(strict_types = 1);

namespace MichalSpacekCz\Pulse\Passwords\Algorithms;

use MichalSpacekCz\Pulse\Passwords\Rating;
use Nette\Database\Explorer;

readonly class PasswordHashingAlgorithms
{

public function __construct(
private Explorer $database,
private Rating $rating,
) {
}


/**
* @return list<PasswordHashingAlgorithm>
*/
public function getAlgorithms(): array
{
$rows = $this->database->fetchAll('SELECT id, algo, alias, salted, stretched FROM password_algos ORDER BY algo');
$algorithms = [];
foreach ($rows as $row) {
$algorithms[] = new PasswordHashingAlgorithm($row->id, $row->algo, $row->alias, (bool)$row->salted, (bool)$row->stretched);
}
return $algorithms;
}


public function getAlgorithmByName(string $name): ?PasswordHashingAlgorithm
{
$row = $this->database->fetch('SELECT id, algo, alias, salted, stretched FROM password_algos WHERE algo = ?', $name);
if (!$row) {
return null;
}
return new PasswordHashingAlgorithm($row->id, $row->algo, $row->alias, (bool)$row->salted, (bool)$row->stretched);
}


/**
* @return int The id of the newly inserted algorithm
*/
public function addAlgorithm(string $name, string $alias, bool $salted, bool $stretched): int
{
$this->database->query('INSERT INTO password_algos', [
'algo' => $name,
'alias' => $alias,
'salted' => $salted,
'stretched' => $stretched,
]);
return (int)$this->database->getInsertId();
}


/**
* @return array<string, string> of alias => name
*/
public function getSlowHashes(): array
{
return $this->database->fetchPairs(
'SELECT alias, algo FROM password_algos WHERE alias IN (?) ORDER BY algo',
$this->rating->getSlowHashes(),
);
}

}
Loading

0 comments on commit fed3b53

Please sign in to comment.