Skip to content

Commit

Permalink
refactor: Apply coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
tweis committed Oct 22, 2024
1 parent f9da18f commit c781647
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
5 changes: 1 addition & 4 deletions Classes/Command/BatchCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;

#[AsCommand(
name: 'nxgooglelocations:runscheduledjobs',
description: 'Run batch processing for Google Locations'
)]
#[AsCommand(name: 'nxgooglelocations:runscheduledjobs', description: 'Run batch processing for Google Locations')]
class BatchCommand extends Command
{
public function __construct(
Expand Down
34 changes: 24 additions & 10 deletions Classes/Controller/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Netlogix\Nxgooglelocations\Domain\Model\Batch;
use Netlogix\Nxgooglelocations\Domain\Repository\BatchRepository;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
use SJBR\StaticInfoTables\Domain\Model\Country;
use SJBR\StaticInfoTables\Domain\Repository\CountryRepository;
use TYPO3\CMS\Backend\Attribute\AsController;
Expand All @@ -21,6 +22,7 @@
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Type\Bitmask\Permission;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
Expand All @@ -44,11 +46,12 @@ protected function initializeAction(): void
{
parent::initializeAction();

$id = (int)($this->request->getQueryParams()['id'] ?? 0);
$id = (int) ($this->request->getQueryParams()['id'] ?? 0);

$this->pageRecord = BackendUtility::readPageAccess(
$id,
$this->getBackendUser()->getPagePermsClause(Permission::PAGE_SHOW)
$this->getBackendUser()
->getPagePermsClause(Permission::PAGE_SHOW)
) ?: [];

$this->moduleTemplate = $this->moduleTemplateFactory->create($this->request);
Expand All @@ -61,9 +64,8 @@ protected function initializeAction(): void
$refreshButton = $buttonBar->makeLinkButton()
->setHref($this->request->getUri())
->setTitle(
$this->getLanguageService()->sL(
'LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload'
)
$this->getLanguageService()
->sL('LLL:EXT:core/Resources/Private/Language/locallang_core.xlf:labels.reload')
)
->setIcon($this->iconFactory->getIcon('actions-refresh', Icon::SIZE_SMALL));

Expand All @@ -73,6 +75,7 @@ protected function initializeAction(): void
protected function getModuleTemplateResponse(): ResponseInterface
{
$this->moduleTemplate->setContent($this->view->render());

return $this->htmlResponse($this->moduleTemplate->renderContent());
}

Expand Down Expand Up @@ -116,19 +119,28 @@ public function indexAction(int $id): ResponseInterface
return $this->getModuleTemplateResponse();
}

public function importAction(int $id, bool $deleteUnused, bool $cancelPrevious, Country $country = null): ResponseInterface
{
public function importAction(
int $id,
bool $deleteUnused,
bool $cancelPrevious,
Country $country = null
): ResponseInterface {
$file = $this->request->getUploadedFiles()['excelFile'] ?? null;
if ($file === null) {
throw new \RuntimeException('Uploading file failed.', 1702385736);
throw new RuntimeException('Uploading file failed.', 1702385736);
}

$batch = $this->mapRequestToBatch($id, $file, $deleteUnused, $cancelPrevious, $country);

try {
$batch->validate();
} catch (Exception $exception) {
$this->addFlashMessage($exception->getMessage(), '', \TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR);
$this->addFlashMessage(
$exception->getMessage(),
'',
ContextualFeedbackSeverity::ERROR
);

return $this->redirect('index');
}

Expand All @@ -153,6 +165,7 @@ public function importAction(int $id, bool $deleteUnused, bool $cancelPrevious,
$this->addFlashMessage(
LocalizationUtility::translate('module.flash-messages.new-job-scheduled.content', $extensionName)
);

return $this->redirect('index');
}

Expand All @@ -173,8 +186,9 @@ protected function forwardToErrorWithCannedMessage(string $reason): ResponseInte
$this->addFlashMessage(
LocalizationUtility::translate(sprintf('module.flash-messages.%s.content', $reason), $extensionName),
LocalizationUtility::translate(sprintf('module.flash-messages.%s.title', $reason), $extensionName),
\TYPO3\CMS\Core\Type\ContextualFeedbackSeverity::ERROR
ContextualFeedbackSeverity::ERROR
);

return $this->redirect('error');
}

Expand Down
14 changes: 12 additions & 2 deletions Classes/Domain/Model/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,14 @@ protected function executeDataHandler(array $tcaRecords): array

$this->impersonator->runAsBackendUser(
$backendUserId,
static function () use ($importer, $recordTableName, $tcaRecords, $storagePageId, $deleteUnused, &$recordUids): void {
static function () use (
$importer,
$recordTableName,
$tcaRecords,
$storagePageId,
$deleteUnused,
&$recordUids
): void {
$recordUids = $importer->import($recordTableName, $storagePageId, $tcaRecords);
if ($deleteUnused) {
$importer->removeRecordsExcept($recordTableName, $storagePageId, $recordUids);
Expand Down Expand Up @@ -253,7 +260,10 @@ protected function initializeServices(): void
}

if (!$this->importer instanceof \Netlogix\Nxgooglelocations\Service\Importer) {
$this->importer = GeneralUtility::makeInstance($this->serviceClasses[Importer::class], $this->storagePageId);
$this->importer = GeneralUtility::makeInstance(
$this->serviceClasses[Importer::class],
$this->storagePageId
);
}

if (!$this->locationFactory instanceof \Netlogix\Nxgooglelocations\Service\LocationFactory) {
Expand Down
8 changes: 4 additions & 4 deletions Classes/Domain/Model/CodingResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public function __get($propertyName)
{
return match ($propertyName) {
'rawData' => $this->rawData,
'status' => (string)ObjectAccess::getPropertyPath($this->rawData, 'status'),
'formattedAddress', 'addressResultFromGeocoding' => (string)ObjectAccess::getPropertyPath(
'status' => (string) ObjectAccess::getPropertyPath($this->rawData, 'status'),
'formattedAddress', 'addressResultFromGeocoding' => (string) ObjectAccess::getPropertyPath(
$this->rawData,
'results.0.formatted_address'
),
'latitude' => (float)ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lat'),
'longitude' => (float)ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lng'),
'latitude' => (float) ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lat'),
'longitude' => (float) ObjectAccess::getPropertyPath($this->rawData, 'results.0.geometry.location.lng'),
'position' => [
'latitude' => $this->latitude,
'longitude' => $this->longitude,
Expand Down
8 changes: 5 additions & 3 deletions Classes/Domain/Repository/BatchRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

namespace Netlogix\Nxgooglelocations\Domain\Repository;

use Netlogix\Nxgooglelocations\Enumerations\BatchState;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use Netlogix\Nxgooglelocations\Domain\Model\Batch;
use Netlogix\Nxgooglelocations\Enumerations\BatchState;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Generic\QuerySettingsInterface;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Persistence\Repository;

Expand Down Expand Up @@ -39,6 +39,8 @@ public function findOpenInFolder(int $storagePageId): QueryResultInterface

public function findOneByState(string $state): ?Batch
{
return $this->findOneBy(['state' => $state]);
return $this->findOneBy([
'state' => $state,
]);
}
}
4 changes: 2 additions & 2 deletions Classes/Service/GeoCoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

abstract class GeoCoder
{

final public const FETCH_URL = 'https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s';

protected FieldMap $fieldMap;
Expand Down Expand Up @@ -59,13 +58,14 @@ public function fetchCoordinatesForAddress($address): CodingResult
$urlWithApiKey = sprintf(self::FETCH_URL, urlencode((string) $address), urlencode($this->apiKey));
$geocode = json_decode((string) GeneralUtility::getUrl($urlWithApiKey), true, 512, JSON_THROW_ON_ERROR);
$status = ObjectAccess::getPropertyPath($geocode, 'status');

return match ($status) {
GeoCoderStatus::OK, GeoCoderStatus::ZERO_RESULTS => new CodingResult($geocode),
default => throw new Exception(
'An error occurred: ' . json_encode(
array_filter(
[$status, ObjectAccess::getPropertyPath($geocode, 'error_message')],
static fn($value): bool => (bool)$value
static fn ($value): bool => (bool) $value
),
JSON_THROW_ON_ERROR
)
Expand Down
4 changes: 3 additions & 1 deletion Classes/Service/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ public function import(string $recordTableName, int $storagePageId, array $tcaRe
$count = 0;
foreach ($tcaRecords as $tcaRecord) {
++$count;
$uid = array_key_exists('uid', $tcaRecord) ? $tcaRecord['uid'] : sprintf('NEW%s', substr(md5(self::class . $count), 0, 10));
$uid = array_key_exists('uid', $tcaRecord)
? $tcaRecord['uid']
: sprintf('NEW%s', substr(md5(self::class . $count), 0, 10));
$data[$recordTableName][$uid] = $tcaRecord;
$data[$recordTableName][$uid]['pid'] = $storagePageId;
$data[$recordTableName][$uid]['sys_language_uid'] = -1;
Expand Down
23 changes: 16 additions & 7 deletions Classes/ViewHelpers/Be/TableListViewHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Netlogix\Nxgooglelocations\ViewHelpers\Be;

use Psr\Http\Message\ServerRequestInterface;
Expand Down Expand Up @@ -135,21 +137,25 @@ protected function render(): string
$table = $request->getParsedBody()['table'] ?? $request->getQueryParams()['table'] ?? '';
$preventPointer = $tableName !== $table;

$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/recordlist.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/recordlist.js');
// Removed to disable the download button
// $this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/record-download-button.js');
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/action-dispatcher.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/action-dispatcher.js');
if ($enableControlPanels === true) {
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/multi-record-selection.js');
$this->getPageRenderer()->loadJavaScriptModule('@typo3/backend/context-menu.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/multi-record-selection.js');
$this->getPageRenderer()
->loadJavaScriptModule('@typo3/backend/context-menu.js');
}

$pageId = (int)($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);
$pageId = (int) ($request->getParsedBody()['id'] ?? $request->getQueryParams()['id'] ?? 0);

// Added to fix the issue with the table pointer
$pointer = $preventPointer
? 0
: (int)($request->getParsedBody()['pointer'] ?? $request->getQueryParams()['pointer'] ?? 0);
: (int) ($request->getParsedBody()['pointer'] ?? $request->getQueryParams()['pointer'] ?? 0);
$pageInfo = BackendUtility::readPageAccess(
$pageId,
$backendUser->getPagePermsClause(Permission::PAGE_SHOW)
Expand Down Expand Up @@ -183,10 +189,13 @@ protected function render(): string
$dbList->start($storagePid, $tableName, $pointer, $filter, $levels, $recordsPerPage);
// Column selector is disabled since fields are defined by the "fieldList" argument
$dbList->displayColumnSelector = false;
$dbList->setFields = [$tableName => $fieldList];
$dbList->setFields = [
$tableName => $fieldList,
];
$dbList->noControlPanels = !$enableControlPanels;
$dbList->sortField = $sortField;
$dbList->sortRev = $sortDescending;

return $dbList->generateList();
}

Expand Down
4 changes: 3 additions & 1 deletion Configuration/Icons.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?php

declare(strict_types=1);

use TYPO3\CMS\Core\Imaging\IconProvider\SvgIconProvider;

return [
'ext-nxgooglelocations-batch-type-default'=> [
'ext-nxgooglelocations-batch-type-default' => [
'provider' => SvgIconProvider::class,
'source' => 'EXT:nxgooglelocations/Resources/Public/Icons/tx_nxgooglelocations_domain_model_batch.svg',
],
Expand Down

0 comments on commit c781647

Please sign in to comment.