Skip to content

Commit

Permalink
[TASK] Replace carbon and collections by php native alternatives
Browse files Browse the repository at this point in the history
  • Loading branch information
Lacr1ma committed Oct 3, 2021
1 parent 5b6a803 commit b54f5ff
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 38 deletions.
8 changes: 6 additions & 2 deletions Classes/Controller/Api/AbstractApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* ************************************************************* */

use LMS\Flogin\Support\Redirect;
use TYPO3\CMS\Extbase\Validation\Error;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Extbase\Mvc\Controller\Arguments;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
Expand Down Expand Up @@ -55,7 +56,7 @@ public function errorAction(): ResponseInterface
$this->getControllerContext()->getArguments()
);

$body = collect(compact('errors'))->toJson();
$body = (string)json_encode(compact('errors'));

return $this->jsonResponse($body);
}
Expand All @@ -65,7 +66,10 @@ public function parseErrors(Arguments $args): array
$errors = [];

foreach ($args->validate()->getFlattenedErrors() as $propertyName => $propertyErrors) {
$errors[$propertyName] = collect($propertyErrors)->map->getMessage()->all();
/** @var Error $error */
foreach ($propertyErrors as $error) {
$errors[$propertyName] = $error->getMessage();
}
}

return $errors;
Expand Down
2 changes: 1 addition & 1 deletion Classes/Controller/Api/ForgotPasswordApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function sendResetLinkEmailAction(string $email): ResponseInterface
$redirect = $this->redirect->uriFor($pid, true);

return $this->jsonResponse(
collect(compact('redirect'))->toJson()
(string)json_encode(compact('redirect'))
);
}
}
4 changes: 2 additions & 2 deletions Classes/Controller/Api/LoginApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function authAction(string $username, string $password, bool $remember):
$redirect = $this->redirect->uriFor($pid, true);

return $this->jsonResponse(
collect(compact('redirect'))->toJson()
(string)json_encode(compact('redirect'))
);
}

Expand All @@ -77,7 +77,7 @@ public function logoutAction(): ResponseInterface
$redirect = $this->redirect->uriFor($pid, true);

return $this->jsonResponse(
collect(compact('redirect'))->toJson()
(string)json_encode(compact('redirect'))
);
}
}
2 changes: 1 addition & 1 deletion Classes/Controller/Api/MagicLinkApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function sendMagicLinkEmailAction(string $email): ResponseInterface
$redirect = $this->redirect->uriFor($pid, true);

return $this->jsonResponse(
collect(compact('redirect'))->toJson()
(string)json_encode(compact('redirect'))
);
}
}
4 changes: 2 additions & 2 deletions Classes/Domain/Validator/DefaultValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ protected function requestProps(): array

protected function isJson(): bool
{
$accepts = collect(Request::createFromGlobals()->getAcceptableContentTypes());
$accepts = Request::createFromGlobals()->getAcceptableContentTypes();

return $accepts->contains('application/json');
return in_array('application/json', $accepts);
}

protected function dispatcher(): EventDispatcher
Expand Down
14 changes: 6 additions & 8 deletions Classes/Middleware/Api/VerifyAccountCreationHash.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
/** @noinspection PhpUnhandledExceptionInspection */

declare(strict_types = 1);

namespace LMS\Flogin\Middleware\Api;
Expand All @@ -25,11 +27,12 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use LMS\Flogin\Support\Registry;
use LMS\Flogin\Support\TypoScript;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Registry as CoreRegistry;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use LMS\Routes\Middleware\Api\AbstractRouteMiddleware;

/**
Expand All @@ -56,16 +59,11 @@ public function process(): void

private function redirectToHashErrorPage(): void
{
HttpUtility::redirect(
$this->invalidHashUrl()
throw new PropagateResponseException(
$this->redirect->toPage($this->hashErrorPage())
);
}

private function invalidHashUrl(): string
{
return "/index.php?id={$this->hashErrorPage()}";
}

private function hashErrorPage(): int
{
return (int)TypoScript::getSettings()['redirect.']['error.']['whenOneTimeAccountHashNotFoundPage'];
Expand Down
6 changes: 3 additions & 3 deletions Classes/Support/Domain/Property/CreationDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;

/**
* @author Sergey Borulko <borulkosergey@icloud.com>
Expand All @@ -45,8 +45,8 @@ public function setCrdate(int $crdate): void
$this->crdate = $crdate;
}

public function getCreatedAt(): Carbon
public function getCreatedAt(): DateTime
{
return Carbon::createFromTimestamp($this->crdate);
return DateTime::createFromFormat('U', (string)$this->crdate);
}
}
12 changes: 6 additions & 6 deletions Classes/Support/Domain/Property/Expirable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;

/**
* @author Sergey Borulko <borulkosergey@icloud.com>
Expand All @@ -42,17 +42,17 @@ trait Expirable
*/
public function isExpired(): bool
{
return $this->getExpirationTime()->isPast();
return new DateTime() > $this->getExpirationTime();
}

/**
* Get the exact time when entity expires
*/
public function getExpirationTime(): Carbon
public function getExpirationTime(): DateTime
{
return $this->getCreatedAt()->addMinutes(
$this->getLifetimeInMinutes()
);
$interval = $this->getLifetimeInMinutes();

return $this->getCreatedAt()->modify("+{$interval} minutes");
}

/**
Expand Down
9 changes: 7 additions & 2 deletions Classes/Support/Domain/Property/IsOnline.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Database\ConnectionPool;

Expand Down Expand Up @@ -67,6 +67,11 @@ public function hasActiveSession(): bool

public function wasActiveRecently(): bool
{
return $this->isOnline > 0 && Carbon::createFromTimestamp($this->isOnline)->diffInMinutes(Carbon::now()) <= 1;
$now = new DateTime();
$online = DateTime::createFromFormat('U', (string)$this->isOnline);

$diff = date_diff($online, $now);

return $this->isOnline > 0 && $diff->d <= 1;
}
}
10 changes: 6 additions & 4 deletions Classes/Support/Domain/Property/Locked.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;
use LMS\Flogin\Support\TypoScript;

/**
Expand All @@ -53,14 +53,16 @@ public function setLocked(bool $locked): void
$this->locked = $locked;
}

public function getUnlockTime(): Carbon
public function getUnlockTime(): DateTime
{
return $this->getUpdatedAt()->addMinutes(self::getLockMinutesInterval());
$interval = self::getLockMinutesInterval();

return $this->getUpdatedAt()->modify("+{$interval} minutes");
}

public function isTimeToUnlock(): bool
{
return $this->getUnlockTime()->isPast();
return new DateTime() > $this->getUnlockTime();
}

public static function getLockMinutesInterval(): int
Expand Down
6 changes: 3 additions & 3 deletions Classes/Support/Domain/Property/UpdateDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;

/**
* @author Sergey Borulko <borulkosergey@icloud.com>
Expand All @@ -45,8 +45,8 @@ public function setTstamp(int $unix): void
$this->tstamp = $unix;
}

public function getUpdatedAt(): Carbon
public function getUpdatedAt(): DateTime
{
return Carbon::createFromTimestamp($this->tstamp);
return DateTime::createFromFormat('U', (string)$this->tstamp);
}
}
7 changes: 5 additions & 2 deletions Classes/Support/Helper/OneTimeAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;
use LMS\Flogin\{Hash\Hash, Support\TypoScript};

/**
Expand Down Expand Up @@ -66,7 +66,10 @@ protected function calculateTerminationTime(): int
{
$lifeTime = $this->accountSettings()['lifetimeInMinutes'];

return (int)Carbon::now()->addMinutes($lifeTime)->timestamp;
$now = new DateTime();
$now->modify("+{$lifeTime} minutes");

return $now->getTimestamp();
}

/**
Expand Down
8 changes: 6 additions & 2 deletions Classes/Support/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This copyright notice MUST APPEAR in all copies of the script!
* ************************************************************* */

use Carbon\Carbon;
use DateTime;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface;
Expand Down Expand Up @@ -54,7 +54,11 @@ public function __construct()
*/
public function availableAt(int $delayInMinutes): int
{
return Carbon::now()->addMinutes($delayInMinutes)->getTimestamp();
$now = new DateTime();

$now->modify("+{$delayInMinutes} minutes");

return $now->getTimestamp();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ services:
public: true
shared: false

LMS\Flogin\Middleware\Api\VerifyAccountCreationHash:
public: true

LMS\Flogin\Command\UnlockUserCommand:
tags:
- name: 'console.command'
Expand Down
2 changes: 2 additions & 0 deletions Resources/Public/JavaScript/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ const loginAttempt = async (url, username, password, remember) => {
method: 'POST'
});

console.log(response);

return await response.json();
};

Expand Down

0 comments on commit b54f5ff

Please sign in to comment.