Skip to content

Commit

Permalink
Merge pull request #72 from fykosak/dev-php81-period
Browse files Browse the repository at this point in the history
PHP81 period
  • Loading branch information
cevro authored Jul 4, 2024
2 parents 54a3a5c + 5f1cbc7 commit 3d60b39
Show file tree
Hide file tree
Showing 22 changed files with 117 additions and 129 deletions.
5 changes: 4 additions & 1 deletion app/Components/ApiResults/ApiResultsComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class ApiResultsComponent extends DIComponent
private readonly Connector $gameServerApiConnector;

Check failure on line 20 in app/Components/ApiResults/ApiResultsComponent.php

View workflow job for this annotation

GitHub Actions / PHPStan 8.1

Class App\Components\ApiResults\ApiResultsComponent has an uninitialized readonly property $gameServerApiConnector. Assign it in the constructor.
private readonly FKSDBDownloader $downloader;

Check failure on line 21 in app/Components/ApiResults/ApiResultsComponent.php

View workflow job for this annotation

GitHub Actions / PHPStan 8.1

Class App\Components\ApiResults\ApiResultsComponent has an uninitialized readonly property $downloader. Assign it in the constructor.

public function __construct(Container $container, private readonly int $eventId)
public function __construct(
Container $container,
private readonly int $eventId
)
{
parent::__construct($container);
}
Expand Down
3 changes: 2 additions & 1 deletion app/Components/Countdown/CountdownComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public function __construct(
Container $container,
private readonly \DateTimeInterface $countdownTo,
string $id = null
) {
)
{
parent::__construct($container);
$this->id = $id ?? uniqid();
}
Expand Down
10 changes: 1 addition & 9 deletions app/Components/UpperHomeMap/UpperHomeMapComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,19 @@

use App\Components\Countdown\CountdownComponent;
use App\Components\Map\MapComponent;
use App\Models\GamePhaseCalculator;
use App\Models\NetteDownloader\ORM\Models\ModelEvent;
use Fykosak\Utils\Components\DIComponent;
use Nette\DI\Container;

final class UpperHomeMapComponent extends DIComponent
{
private readonly GamePhaseCalculator $gamePhaseCalculator;

public function __construct(
Container $container,
private readonly ModelEvent $event
) {
parent::__construct($container);
}

public function inject(GamePhaseCalculator $calculator): void
{
$this->gamePhaseCalculator = $calculator;
}

/**
* @throws \Throwable
*/
Expand All @@ -47,6 +39,6 @@ protected function createComponentMap(): MapComponent
*/
protected function createComponentCountdown(): CountdownComponent
{
return new CountdownComponent($this->container, $this->gamePhaseCalculator->getGameBegin());
return new CountdownComponent($this->container, $this->event->game->begin);
}
}
19 changes: 3 additions & 16 deletions app/Components/UpperHomePrague/UpperHomePrague.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,27 @@
namespace App\Components\UpperHomePrague;

use App\Components\Countdown\CountdownComponent;
use App\Models\GamePhaseCalculator;
use App\Models\NetteDownloader\ORM\Models\ModelEvent;
use Fykosak\Utils\Components\DIComponent;
use Fykosak\Utils\DateTime\Phase;
use Nette\DI\Container;

final class UpperHomePrague extends DIComponent
{
private readonly GamePhaseCalculator $gamePhaseCalculator;

public function __construct(
Container $container,
private readonly ModelEvent $event
) {
parent::__construct($container);
}

public function injectGamePhaseCalculator(GamePhaseCalculator $gamePhaseCalculator): void
{
$this->gamePhaseCalculator = $gamePhaseCalculator;
}

/**
* @throws \Throwable
*/
public function render(): void
{
$this->template->lang = $this->translator->lang;
$this->template->event = $this->event;
$this->template->gamePhaseCalculator = $this->gamePhaseCalculator;
$this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'upperHomePrague.latte');
}

Expand All @@ -43,16 +34,12 @@ public function render(): void
*/
protected function createComponentCountdown(): CountdownComponent
{
if (
$this->gamePhaseCalculator->getFKSDBEvent()
->getRegistrationPeriod()
->is(Phase::before)
) {
if ($this->event->getRegistrationPeriod()->is(Phase::before)) {
return new CountdownComponent(
$this->container,
$this->gamePhaseCalculator->getFKSDBEvent()->registrationBegin
$this->event->registrationBegin
);
}
return new CountdownComponent($this->container, $this->gamePhaseCalculator->getGameBegin());
return new CountdownComponent($this->container, $this->event->game->begin);
}
}
6 changes: 3 additions & 3 deletions app/Components/UpperHomePrague/upperHomePrague.latte
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{varType App\Models\NetteDownloader\ORM\Models\ModelEvent $event}
{varType App\Models\GamePhaseCalculator $gamePhaseCalculator}
{varType App\Modules\Core\Language $lang}

{block content}
Expand Down Expand Up @@ -137,8 +136,9 @@
</div>
{elseif $event->getRegistrationPeriod()->is(Fykosak\Utils\DateTime\Phase::after)}
{switch $lang->value}
{case cs} Registrace je již uzavřena. Soutěžíme <strong>{$gamePhaseCalculator->getGameBegin() |date:'j. n. G:i'}</strong>.
{default} Registration is already closed. The competition starts <strong>{$gamePhaseCalculator->getGameBegin() |date:'F j, G:i'} CET</strong>.
{case cs} Registrace je již uzavřena. Soutěžíme <strong>{$event->game->begin|date:'j. n. G:i'}</strong>.
{default} Registration is already closed. The competition starts <strong>{$event->game->begin|date:'F j, G:i'}
CET</strong>.
{/switch}
{elseif !$event->getRegistrationPeriod()->is(Fykosak\Utils\DateTime\Phase::before)}
{switch $lang->value}
Expand Down
30 changes: 8 additions & 22 deletions app/Models/Downloader/NetteDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,22 @@ abstract class NetteDownloader
use SmartObject;

private Downloader $downloader;
private string $username;
private string $password;
private string $expiration;
private string $jsonApiUrl;
private Cache $cache;

public function __construct(
string $jsonApiUrl,
string $username,
#[\SensitiveParameter]
string $password,
string $expiration,
private readonly string $expiration,
Storage $storage
) {
$this->cache = new Cache($storage, self::class);
$this->username = $username;
$this->password = $password;
$this->jsonApiUrl = $jsonApiUrl;
$this->expiration = $expiration;
}

public function getDownloader(): Downloader
{
if (!isset($this->downloader)) {
$this->downloader = new Downloader(
$this->jsonApiUrl,
$this->username,
$this->password,
);
}
return $this->downloader;
$this->downloader = new Downloader(
$jsonApiUrl,
$username,
$password,
);
}

/**
Expand All @@ -56,7 +42,7 @@ public function download(Request $request, ?string $explicitExpiration = null):
$request->getCacheKey() . '-json',
function (&$dependencies) use ($request, $explicitExpiration): array {
$dependencies[Cache::EXPIRE] = $explicitExpiration ?? $this->expiration;
return $this->getDownloader()->download($request);
return $this->downloader->download($request);
}
);
}
Expand Down
23 changes: 5 additions & 18 deletions app/Models/GamePhaseCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,24 @@

use App\Models\NetteDownloader\ORM\Models\ModelEvent;
use App\Models\NetteDownloader\ORM\Services\ServiceEventList;
use Nette\DI\Container;
use Nette\SmartObject;
use Throwable;

/**
* @deprecated
*/
final class GamePhaseCalculator
{
use SmartObject;

public function __construct(
private readonly int $eventTypeId,
private readonly ServiceEventList $serviceEventList,
private readonly Container $container
private readonly ServiceEventList $serviceEventList
) {
}

/**
* @throws Throwable
*/
public function getGameBegin(): \DateTime
{
$time = new \DateTime($this->container->getParameters()['competitionBegin']);
$day = $this->getFKSDBEvent()->begin;

$time->setDate((int)$day->format('Y'), (int)$day->format('m'), (int)$day->format('d'));
return $time;
}

/**
* Returns newest FKSDB event. That means by creating a new one, the application automatically switches to the new
* year.
* @throws Throwable
* @throws \Throwable
*/
public function getFKSDBEvent(): ?ModelEvent
{
Expand Down
35 changes: 24 additions & 11 deletions app/Models/NetteDownloader/ORM/Models/ModelEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@

class ModelEvent
{
public int $eventId;
public int $eventTypeId;
public string $name;
public int $eventYear;
public int $year;
public \DateTimeImmutable $begin;
public \DateTimeImmutable $end;
public \DateTimeImmutable $registrationBegin;
public \DateTimeImmutable $registrationEnd;
public readonly int $eventId;
public readonly int $eventTypeId;
public readonly string $name;
public readonly int $eventYear;
public readonly int $year;
public readonly ?string $place;
public readonly ModelGame|null $game;
public readonly \DateTimeImmutable $begin;
public readonly \DateTimeImmutable $end;
public readonly \DateTimeImmutable $registrationBegin;
public readonly \DateTimeImmutable $registrationEnd;
public readonly Period $registration; // TODO magic?
public function getRegistrationPeriod(): Period
{
Expand All @@ -28,9 +31,9 @@ public function getEventPeriod(): Period
return new Period($this->begin, $this->end);
}

public function getGamePeriod(): Period
public function getGamePeriod(): ?Period
{
return $this->getEventPeriod(); // TODO!!!!
return $this->game?->getGamePeriod();
}

public function getNearEventPeriod(): Period
Expand All @@ -48,4 +51,14 @@ public function isLongAfterTheEvent(): bool
$event = $this->end->add(new \DateInterval('P7D'));
return new \DateTime() > $event;
}

public function getYear(): int
{
return (int)$this->begin->format('Y');
}

public function getMonth(): string
{
return $this->begin->format('m');
}
}
21 changes: 21 additions & 0 deletions app/Models/NetteDownloader/ORM/Models/ModelGame.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace App\Models\NetteDownloader\ORM\Models;

use Fykosak\Utils\DateTime\Period;

class ModelGame
{
public readonly ?array $availablePoints;
public readonly ?int $tasksOnBoard;
public readonly bool $hardVisible;
public readonly \DateTimeImmutable $begin;
public readonly \DateTimeImmutable $end;

public function getGamePeriod(): Period
{
return new Period($this->begin, $this->end);
}
}
4 changes: 2 additions & 2 deletions app/Models/NetteDownloader/ORM/Models/ModelTeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class ModelTeam
/**
* @var ModelMember[]
*/
public $members;
public array $members;

/**
* @var ModelPerson[]
*/
public $teachers;
public array $teachers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\NetteDownloader\ORM\Models\ModelEvent;
use Fykosak\FKSDBDownloaderCore\Requests\EventListRequest;
use Nette\Caching\Storage;
use Tracy\Debugger;

final class ServiceEventList extends AbstractJSONService
{
Expand Down
20 changes: 12 additions & 8 deletions app/Modules/Core/EventWebPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace App\Modules\Core;

use App\Models\GamePhaseCalculator;
use App\Models\NetteDownloader\ORM\Models\ModelEvent;
use App\Models\NetteDownloader\ORM\Services\DummyService;
use App\Models\NetteDownloader\ORM\Services\ServiceEventList;
use Nette\Application\UI\Template;

abstract class EventWebPresenter extends BasePresenter
{
protected GamePhaseCalculator $gamePhaseCalculator;
protected DummyService $dummyService;
protected readonly DummyService $dummyService;
protected readonly ServiceEventList $serviceEventList;

public function injectEventWebServices(
GamePhaseCalculator $calculator,
DummyService $dummyService
DummyService $dummyService,
ServiceEventList $serviceEventList
): void {
$this->gamePhaseCalculator = $calculator;
$this->dummyService = $dummyService;
$this->serviceEventList = $serviceEventList;
}

/**
Expand All @@ -28,7 +28,6 @@ public function injectEventWebServices(
protected function createTemplate(): Template
{
$template = parent::createTemplate();
$template->gamePhaseCalculator = $this->gamePhaseCalculator;
$template->newestEvent = $this->getNewestEvent();
return $template;
}
Expand All @@ -40,8 +39,13 @@ protected function getNewestEvent(): ModelEvent
{
static $newestEvent;
if (!isset($newestEvent)) {
$newestEvent = $this->gamePhaseCalculator->getFKSDBEvent();
$newestEvent = $this->serviceEventList->getNewest($this->getEventIds());
}
return $newestEvent;
}

/**
* @return int[]
*/
abstract protected function getEventIds(): array;
}
Loading

0 comments on commit 3d60b39

Please sign in to comment.