From f9787fc5af4d2ca419f6106b2f689a4586f740b5 Mon Sep 17 00:00:00 2001 From: Stefan Froemken Date: Sun, 12 Jan 2025 22:53:27 +0100 Subject: [PATCH 1/2] Add builder pattern for ExtConf --- Classes/Builder/ExtConfBuilder.php | 109 ++++++++++++++++++ Classes/Builder/ExtConfBuilderFactory.php | 23 ++++ .../CredentialsConfiguration.php | 45 ++++++++ Classes/Configuration/ExtConf.php | 50 ++------ Classes/Configuration/ExtConfFactory.php | 69 ++++++----- Classes/Configuration/ViewConfiguration.php | 33 ++++++ Configuration/Services.yaml | 2 +- .../Configuration/ExtConfFactoryTest.php | 56 ++++----- .../Functional/Configuration/ExtConfTest.php | 53 ++++----- ext_conf_template.txt | 4 +- 10 files changed, 312 insertions(+), 132 deletions(-) create mode 100644 Classes/Builder/ExtConfBuilder.php create mode 100644 Classes/Builder/ExtConfBuilderFactory.php create mode 100644 Classes/Configuration/CredentialsConfiguration.php create mode 100644 Classes/Configuration/ViewConfiguration.php diff --git a/Classes/Builder/ExtConfBuilder.php b/Classes/Builder/ExtConfBuilder.php new file mode 100644 index 0000000..87de777 --- /dev/null +++ b/Classes/Builder/ExtConfBuilder.php @@ -0,0 +1,109 @@ +host = $host; + + return $this; + } + + public function setPort(string $port): self + { + if (MathUtility::canBeInterpretedAsInteger($port)) { + $this->port = (int)$port; + } + + return $this; + } + + public function setUsername(string $username): self + { + $this->username = $username; + + return $this; + } + + public function setPassword(string $password): self + { + $this->password = $password; + + return $this; + } + + public function setDiskUsageType(string $diskUsageType): self + { + try { + $this->diskUsageType = DiskUsageTypeEnum::from($diskUsageType); + } catch (ValueError) { + // Do nothing, keep default value + } + + return $this; + } + + public function setDomain(string $domain): self + { + $this->domain = $domain; + + return $this; + } + + public function buildExtConf(): ExtConf + { + return new ExtConf( + $this->buildCredentialsConfiguration(), + $this->buildViewConfiguration(), + ); + } + + private function buildCredentialsConfiguration(): CredentialsConfiguration + { + return new CredentialsConfiguration( + $this->host, + $this->port, + $this->username, + $this->password, + ); + } + + private function buildViewConfiguration(): ViewConfiguration + { + return new ViewConfiguration($this->diskUsageType, $this->domain); + } +} diff --git a/Classes/Builder/ExtConfBuilderFactory.php b/Classes/Builder/ExtConfBuilderFactory.php new file mode 100644 index 0000000..fdee2ca --- /dev/null +++ b/Classes/Builder/ExtConfBuilderFactory.php @@ -0,0 +1,23 @@ +host; + } + + public function getPort(): int + { + return $this->port; + } + + public function getUsername(): string + { + return $this->username; + } + + public function getPassword(): string + { + return $this->password; + } +} diff --git a/Classes/Configuration/ExtConf.php b/Classes/Configuration/ExtConf.php index 32bd9fd..caba113 100644 --- a/Classes/Configuration/ExtConf.php +++ b/Classes/Configuration/ExtConf.php @@ -16,52 +16,18 @@ */ readonly class ExtConf { - private string $host; + public function __construct( + private CredentialsConfiguration $credentialsConfiguration, + private ViewConfiguration $viewConfiguration + ) {} - private int $port; - - private string $username; - - private string $password; - - private DiskUsageTypeEnum $diskUsageType; - - private string $domain; - - public function __construct(array $extensionSettings) - { - foreach ($extensionSettings as $property => $value) { - $this->{$property} = $value; - } - } - - public function getHost(): string - { - return $this->host; - } - - public function getPort(): int - { - return $this->port; - } - - public function getUsername(): string - { - return $this->username; - } - - public function getPassword(): string - { - return $this->password; - } - - public function getDiskUsageType(): string + public function getCredentialsConfiguration(): CredentialsConfiguration { - return $this->diskUsageType->value; + return $this->credentialsConfiguration; } - public function getDomain(): string + public function getViewConfiguration(): ViewConfiguration { - return $this->domain; + return $this->viewConfiguration; } } diff --git a/Classes/Configuration/ExtConfFactory.php b/Classes/Configuration/ExtConfFactory.php index 9e712ba..5a3af26 100644 --- a/Classes/Configuration/ExtConfFactory.php +++ b/Classes/Configuration/ExtConfFactory.php @@ -11,6 +11,8 @@ namespace StefanFroemken\PleskWidget\Configuration; +use StefanFroemken\PleskWidget\Builder\ExtConfBuilder; +use StefanFroemken\PleskWidget\Builder\ExtConfBuilderFactory; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; @@ -19,20 +21,37 @@ readonly class ExtConfFactory { + /** + * Stored values of ext_conf_template.txt are always string. + * So, keep all values as string here, too. + * They will be cast within their setter methods. + */ private const DEFAULT_SETTINGS = [ 'host' => '', - 'port' => 8443, + 'port' => '8443', 'username' => '', 'password' => '', 'diskUsageType' => '%', 'domain' => '', ]; - public function __construct(private ExtensionConfiguration $extensionConfiguration) {} + public function __construct( + private ExtConfBuilderFactory $extConfBuilderFactory, + private ExtensionConfiguration $extensionConfiguration + ) {} - public function create(): ExtConf + public function createExtConf(): ExtConf { - return new ExtConf($this->getExtensionSettings()); + $extensionSettings = $this->getExtensionSettings(); + + return $this->createExtConfBuilder() + ->setHost($extensionSettings['host']) + ->setPort($extensionSettings['port']) + ->setUsername($extensionSettings['username']) + ->setPassword($extensionSettings['password']) + ->setDiskUsageType($extensionSettings['diskUsageType']) + ->setDomain($extensionSettings['domain']) + ->buildExtConf(); } private function getExtensionSettings(): array @@ -40,31 +59,11 @@ private function getExtensionSettings(): array $extensionSettings = self::DEFAULT_SETTINGS; try { - $extensionSettings = (array)$this->extensionConfiguration->get('plesk_widget'); - - // Remove whitespaces - $extensionSettings = array_map('trim', $extensionSettings); - - // remove empty values - $extensionSettings = array_filter($extensionSettings); + $extensionSettings = $this->sanitizeExtensionSettings( + (array)$this->extensionConfiguration->get('plesk_widget') + ); - $extensionSettings = array_merge(self::DEFAULT_SETTINGS, $extensionSettings); - - // Special handling for integer value "port" - if (MathUtility::canBeInterpretedAsInteger($extensionSettings['port'])) { - $extensionSettings['port'] = (int)$extensionSettings['port']; - } else { - $extensionSettings['port'] = self::DEFAULT_SETTINGS['port']; - } - - // Migrate diskUsageType to ENUM - try { - $extensionSettings['diskUsageType'] = DiskUsageTypeEnum::from( - (string)$extensionSettings['diskUsageType'] - ); - } catch (ValueError) { - $extensionSettings['diskUsageType'] = DiskUsageTypeEnum::from(self::DEFAULT_SETTINGS['diskUsageType']); - } + return array_merge(self::DEFAULT_SETTINGS, $extensionSettings); } catch (ExtensionConfigurationExtensionNotConfiguredException) { // Do nothing. Keep the default values } catch (ExtensionConfigurationPathDoesNotExistException) { @@ -73,4 +72,18 @@ private function getExtensionSettings(): array return $extensionSettings; } + + private function sanitizeExtensionSettings(array $extensionSettings): array + { + // Remove whitespaces + $extensionSettings = array_map('trim', $extensionSettings); + + // remove empty values + return array_filter($extensionSettings); + } + + private function createExtConfBuilder(): ExtConfBuilder + { + return $this->extConfBuilderFactory->createBuilder(); + } } diff --git a/Classes/Configuration/ViewConfiguration.php b/Classes/Configuration/ViewConfiguration.php new file mode 100644 index 0000000..6162a0d --- /dev/null +++ b/Classes/Configuration/ViewConfiguration.php @@ -0,0 +1,33 @@ +diskUsageType->value; + } + + public function getDomain(): string + { + return $this->domain; + } +} diff --git a/Configuration/Services.yaml b/Configuration/Services.yaml index 3804464..0dd763c 100644 --- a/Configuration/Services.yaml +++ b/Configuration/Services.yaml @@ -46,7 +46,7 @@ services: height: 'medium' StefanFroemken\PleskWidget\Configuration\ExtConf: - factory: ['@StefanFroemken\PleskWidget\Configuration\ExtConfFactory', 'create'] + factory: ['@StefanFroemken\PleskWidget\Configuration\ExtConfFactory', 'createExtConf'] StefanFroemken\PleskWidget\Service\PleskSiteService: arguments: diff --git a/Tests/Functional/Configuration/ExtConfFactoryTest.php b/Tests/Functional/Configuration/ExtConfFactoryTest.php index b428e5e..3e45d2e 100644 --- a/Tests/Functional/Configuration/ExtConfFactoryTest.php +++ b/Tests/Functional/Configuration/ExtConfFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\MockObject\MockObject; +use StefanFroemken\PleskWidget\Builder\ExtConfBuilderFactory; use StefanFroemken\PleskWidget\Configuration\ExtConfFactory; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; @@ -39,6 +40,7 @@ protected function setUp(): void $this->extensionConfigurationMock = $this->createMock(ExtensionConfiguration::class); $this->subject = new ExtConfFactory( + new ExtConfBuilderFactory(), $this->extensionConfigurationMock ); } @@ -57,7 +59,7 @@ public function getHostWillInitiallyReturnEmptyValue(): void { self::assertSame( '', - $this->subject->create()->getHost() + $this->subject->createExtConf()->getCredentialsConfiguration()->getHost() ); } @@ -74,7 +76,7 @@ public function getHostWillReturnHost(): void self::assertSame( 'plesk.example.com', - $this->subject->create()->getHost() + $this->subject->createExtConf()->getCredentialsConfiguration()->getHost() ); } @@ -83,7 +85,7 @@ public function getPortWillInitiallyReturnDefaultPort(): void { self::assertSame( 8443, - $this->subject->create()->getPort() + $this->subject->createExtConf()->getCredentialsConfiguration()->getPort() ); } @@ -110,7 +112,7 @@ public function getPortWillReturnPort(mixed $port, int $expectedPort): void self::assertSame( $expectedPort, - $this->subject->create()->getPort() + $this->subject->createExtConf()->getCredentialsConfiguration()->getPort() ); } @@ -119,7 +121,7 @@ public function getUsernameWillInitiallyReturnEmptyValue(): void { self::assertSame( '', - $this->subject->create()->getUsername() + $this->subject->createExtConf()->getCredentialsConfiguration()->getUsername() ); } @@ -136,7 +138,7 @@ public function getUsernameWillReturnUsername(): void self::assertSame( 'mustermann', - $this->subject->create()->getUsername() + $this->subject->createExtConf()->getCredentialsConfiguration()->getUsername() ); } @@ -145,7 +147,7 @@ public function getPasswordWillInitiallyReturnEmptyValue(): void { self::assertSame( '', - $this->subject->create()->getPassword() + $this->subject->createExtConf()->getCredentialsConfiguration()->getPassword() ); } @@ -162,7 +164,7 @@ public function getPasswordWillReturnPassword(): void self::assertSame( 'very-cryptic', - $this->subject->create()->getPassword() + $this->subject->createExtConf()->getCredentialsConfiguration()->getPassword() ); } @@ -171,7 +173,7 @@ public function getDiskUsageTypeWillInitiallyReturnDefaultValue(): void { self::assertSame( '%', - $this->subject->create()->getDiskUsageType() + $this->subject->createExtConf()->getViewConfiguration()->getDiskUsageType() ); } @@ -188,7 +190,7 @@ public function getDiskUsageTypeWillReturnDiskUsageType(): void self::assertSame( 'MB', - $this->subject->create()->getDiskUsageType() + $this->subject->createExtConf()->getViewConfiguration()->getDiskUsageType() ); } @@ -205,7 +207,7 @@ public function getDiskUsageWithInvalidValueWillReturnDefaultValue(): void self::assertSame( '%', - $this->subject->create()->getDiskUsageType() + $this->subject->createExtConf()->getViewConfiguration()->getDiskUsageType() ); } @@ -214,7 +216,7 @@ public function getDomainWillInitiallyReturnEmptyValue(): void { self::assertSame( '', - $this->subject->create()->getDomain() + $this->subject->createExtConf()->getViewConfiguration()->getDomain() ); } @@ -231,7 +233,7 @@ public function getDomainWillReturnDomain(): void self::assertSame( '134.example.com', - $this->subject->create()->getDomain() + $this->subject->createExtConf()->getViewConfiguration()->getDomain() ); } @@ -251,7 +253,7 @@ public function trimValuesBeforeAssigningThem(): void 'domain' => ' 134.example.com ', ]); - $extConf = $this->subject->create(); + $extConf = $this->subject->createExtConf(); self::assertSame( [ @@ -263,12 +265,12 @@ public function trimValuesBeforeAssigningThem(): void 'domain' => '134.example.com', ], [ - 'host' => $extConf->getHost(), - 'port' => $extConf->getPort(), - 'username' => $extConf->getUsername(), - 'password' => $extConf->getPassword(), - 'diskUsageType' => $extConf->getDiskUsageType(), - 'domain' => $extConf->getDomain(), + 'host' => $extConf->getCredentialsConfiguration()->getHost(), + 'port' => $extConf->getCredentialsConfiguration()->getPort(), + 'username' => $extConf->getCredentialsConfiguration()->getUsername(), + 'password' => $extConf->getCredentialsConfiguration()->getPassword(), + 'diskUsageType' => $extConf->getViewConfiguration()->getDiskUsageType(), + 'domain' => $extConf->getViewConfiguration()->getDomain(), ] ); } @@ -285,7 +287,7 @@ public function emptyValuesWillBeFilledWithDefaultValues(): void 'diskUsageType' => '', ]); - $extConf = $this->subject->create(); + $extConf = $this->subject->createExtConf(); self::assertSame( [ @@ -297,12 +299,12 @@ public function emptyValuesWillBeFilledWithDefaultValues(): void 'domain' => '', ], [ - 'host' => $extConf->getHost(), - 'port' => $extConf->getPort(), - 'username' => $extConf->getUsername(), - 'password' => $extConf->getPassword(), - 'diskUsageType' => $extConf->getDiskUsageType(), - 'domain' => $extConf->getDomain(), + 'host' => $extConf->getCredentialsConfiguration()->getHost(), + 'port' => $extConf->getCredentialsConfiguration()->getPort(), + 'username' => $extConf->getCredentialsConfiguration()->getUsername(), + 'password' => $extConf->getCredentialsConfiguration()->getPassword(), + 'diskUsageType' => $extConf->getViewConfiguration()->getDiskUsageType(), + 'domain' => $extConf->getViewConfiguration()->getDomain(), ] ); } diff --git a/Tests/Functional/Configuration/ExtConfTest.php b/Tests/Functional/Configuration/ExtConfTest.php index b04c673..bd5a55d 100644 --- a/Tests/Functional/Configuration/ExtConfTest.php +++ b/Tests/Functional/Configuration/ExtConfTest.php @@ -12,8 +12,10 @@ namespace StefanFroemken\PleskWidget\Tests\Functional\Configuration; use PHPUnit\Framework\Attributes\Test; +use StefanFroemken\PleskWidget\Configuration\CredentialsConfiguration; use StefanFroemken\PleskWidget\Configuration\DiskUsageTypeEnum; use StefanFroemken\PleskWidget\Configuration\ExtConf; +use StefanFroemken\PleskWidget\Configuration\ViewConfiguration; use TypeError; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; @@ -33,24 +35,26 @@ protected function setUp(): void { parent::setUp(); - $extensionSetting = [ - 'host' => 'plesk.example.com', - 'port' => 1234, - 'username' => 'mustermann', - 'password' => 'very-cryptic', - 'diskUsageType' => DiskUsageTypeEnum::from('MB'), - 'domain' => '134.example.com', - ]; - - $this->subject = new ExtConf($extensionSetting); + $this->subject = new ExtConf( + new CredentialsConfiguration( + 'plesk.example.com', + 1234, + 'mustermann', + 'very-cryptic', + ), + new ViewConfiguration( + DiskUsageTypeEnum::from('MB'), + '134.example.com', + ) + ); } protected function tearDown(): void { unset( - $this->extensionConfigurationMock, $this->subject, ); + parent::tearDown(); } @@ -59,7 +63,7 @@ public function getHostWillReturnHost(): void { self::assertSame( 'plesk.example.com', - $this->subject->getHost() + $this->subject->getCredentialsConfiguration()->getHost() ); } @@ -68,7 +72,7 @@ public function getPortWillReturnPortAsInt(): void { self::assertSame( 1234, - $this->subject->getPort() + $this->subject->getCredentialsConfiguration()->getPort() ); } @@ -77,7 +81,7 @@ public function getUsernameWillReturnUsername(): void { self::assertSame( 'mustermann', - $this->subject->getUsername() + $this->subject->getCredentialsConfiguration()->getUsername() ); } @@ -86,7 +90,7 @@ public function getPasswordWillReturnPassword(): void { self::assertSame( 'very-cryptic', - $this->subject->getPassword() + $this->subject->getCredentialsConfiguration()->getPassword() ); } @@ -95,22 +99,7 @@ public function getDiskUsageTypeWillReturnDiskUsageType(): void { self::assertSame( 'MB', - $this->subject->getDiskUsageType() - ); - } - - #[Test] - public function getDiskUsageWithInvalidValueWillThrowTypeError(): void - { - self::expectException(TypeError::class); - - $subject = new ExtConf([ - 'diskUsageType' => 'INVALID', - ]); - - self::assertSame( - 'WillNotBeTested', - $subject->getDiskUsageType() + $this->subject->getViewConfiguration()->getDiskUsageType() ); } @@ -119,7 +108,7 @@ public function getDomainWillReturnDomain(): void { self::assertSame( '134.example.com', - $this->subject->getDomain() + $this->subject->getViewConfiguration()->getDomain() ); } } diff --git a/ext_conf_template.txt b/ext_conf_template.txt index 05676b5..7797980 100644 --- a/ext_conf_template.txt +++ b/ext_conf_template.txt @@ -6,7 +6,7 @@ port = 8443 username = # cat=plesk; type=string; label=Plesk password password = -# cat=basic; type=options[Percent (%)=%,MegaByte (MB)=MB,GigaByte (GB)=GB]; label=Disk usage type: Show disk usage in percent or in Mega- or GigaByte? +# cat=view; type=options[Percent (%)=%,MegaByte (MB)=MB,GigaByte (GB)=GB]; label=Disk usage type: Show disk usage in percent or in Mega- or GigaByte? diskUsageType = % -# cat=basic; type=string; label=Domain: Enter domain name to show info about. Without www and without scheme (https://) +# cat=view; type=string; label=Domain: Enter domain name to show info about. Without www and without scheme (https://) domain = From 3bc35ecefa19edab1e11f821c00d560f924448e0 Mon Sep 17 00:00:00 2001 From: Stefan Froemken Date: Sun, 12 Jan 2025 22:58:34 +0100 Subject: [PATCH 2/2] Use ExtConf from ExtConfBuilder --- Classes/Client/PleskClientFactory.php | 16 +++++++++++----- Classes/Configuration/ExtConfFactory.php | 2 -- Classes/DataProvider/ServerDataProvider.php | 5 ++++- Classes/DataProvider/WebspaceDataProvider.php | 8 +++++--- Classes/Widget/PhpWidget.php | 9 +++++---- Classes/Widget/ServerWidget.php | 4 ++-- Classes/Widget/WebspaceWidget.php | 2 +- Tests/Functional/Configuration/ExtConfTest.php | 1 - 8 files changed, 28 insertions(+), 19 deletions(-) diff --git a/Classes/Client/PleskClientFactory.php b/Classes/Client/PleskClientFactory.php index 96da204..0505246 100644 --- a/Classes/Client/PleskClientFactory.php +++ b/Classes/Client/PleskClientFactory.php @@ -34,25 +34,31 @@ public function create(): Client ); } - $pleskClient = new Client($this->extConf->getHost(), $this->extConf->getPort()); - $pleskClient->setCredentials($this->extConf->getUsername(), $this->extConf->getPassword()); + $pleskClient = new Client( + $this->extConf->getCredentialsConfiguration()->getHost(), + $this->extConf->getCredentialsConfiguration()->getPort() + ); + $pleskClient->setCredentials( + $this->extConf->getCredentialsConfiguration()->getUsername(), + $this->extConf->getCredentialsConfiguration()->getPassword() + ); return $pleskClient; } private function validateExtConf(): bool { - if ($this->extConf->getHost() === '') { + if ($this->extConf->getCredentialsConfiguration()->getHost() === '') { $this->logger->error('Plesk host in extension settings can not be empty'); return false; } - if ($this->extConf->getUsername() === '') { + if ($this->extConf->getCredentialsConfiguration()->getUsername() === '') { $this->logger->error('Plesk user in extension settings can not be empty'); return false; } - if ($this->extConf->getPassword() === '') { + if ($this->extConf->getCredentialsConfiguration()->getPassword() === '') { $this->logger->error('Plesk password in extension settings can not be empty'); return false; } diff --git a/Classes/Configuration/ExtConfFactory.php b/Classes/Configuration/ExtConfFactory.php index 5a3af26..bda0980 100644 --- a/Classes/Configuration/ExtConfFactory.php +++ b/Classes/Configuration/ExtConfFactory.php @@ -16,8 +16,6 @@ use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; use TYPO3\CMS\Core\Configuration\ExtensionConfiguration; -use TYPO3\CMS\Core\Utility\MathUtility; -use ValueError; readonly class ExtConfFactory { diff --git a/Classes/DataProvider/ServerDataProvider.php b/Classes/DataProvider/ServerDataProvider.php index e1b908c..38b02dc 100644 --- a/Classes/DataProvider/ServerDataProvider.php +++ b/Classes/DataProvider/ServerDataProvider.php @@ -36,7 +36,10 @@ public function getLoginLink(Client $pleskClient, string $externalIpAddress): st $pleskClient->getProtocol() ?: 'https', $pleskClient->getHost(), $pleskClient->getPort(), - $pleskClient->server()->createSession($this->extConf->getUsername(), $externalIpAddress), + $pleskClient->server()->createSession( + $this->extConf->getCredentialsConfiguration()->getUsername(), + $externalIpAddress + ), '/smb/web/view' ); } diff --git a/Classes/DataProvider/WebspaceDataProvider.php b/Classes/DataProvider/WebspaceDataProvider.php index ce42961..86a8957 100644 --- a/Classes/DataProvider/WebspaceDataProvider.php +++ b/Classes/DataProvider/WebspaceDataProvider.php @@ -78,11 +78,13 @@ private function getWebSpaceStatus(Client $pleskClient): array private function calcDiskUsage(int $part, int $total = 0): float { - if ($this->extConf->getDiskUsageType() === '%' && $total !== 0) { + $diskUsageType = $this->extConf->getViewConfiguration()->getDiskUsageType(); + + if ($diskUsageType === '%' && $total !== 0) { $value = round(100 / $total * $part, 4); - } elseif ($this->extConf->getDiskUsageType() === 'MB') { + } elseif ($diskUsageType === 'MB') { $value = round($part / 1024 / 1024, 4); - } elseif ($this->extConf->getDiskUsageType() === 'GB') { + } elseif ($diskUsageType === 'GB') { $value = round($part / 1024 / 1024 / 1024, 4); } else { $value = (float)$part; diff --git a/Classes/Widget/PhpWidget.php b/Classes/Widget/PhpWidget.php index f2a7da2..9c8c094 100644 --- a/Classes/Widget/PhpWidget.php +++ b/Classes/Widget/PhpWidget.php @@ -48,14 +48,15 @@ public function renderWidgetContent(): string try { $pleskClient = $this->pleskClientFactory->create(); + $domain = $this->extConf->getViewConfiguration()->getDomain(); - if ($this->extConf->getDomain() === '') { + if ($domain === '') { $variables['error'] = 'You have to select a domain name in extension settings of EXT:plesk-widget.'; - } elseif ($site = $this->pleskSiteService->getSiteByName($this->extConf->getDomain(), $pleskClient)) { - $variables['domain'] = $this->extConf->getDomain(); + } elseif ($site = $this->pleskSiteService->getSiteByName($domain, $pleskClient)) { + $variables['domain'] = $domain; $variables['hosting'] = $site->getHosting(); } else { - $variables['error'] = 'Plesk API can not retrieve domain information for ' . $this->extConf->getDomain(); + $variables['error'] = 'Plesk API can not retrieve domain information for ' . $domain; } } catch (ExtensionSettingException $extensionSettingException) { $variables['error'] = $extensionSettingException->getMessage(); diff --git a/Classes/Widget/ServerWidget.php b/Classes/Widget/ServerWidget.php index 66337a8..e454667 100644 --- a/Classes/Widget/ServerWidget.php +++ b/Classes/Widget/ServerWidget.php @@ -60,8 +60,8 @@ public function renderWidgetContent(): string $variables['button'] = $this->getButtonProvider($pleskClient, $externalIpAddress); if ( - $this->extConf->getDomain() - && ($site = $this->pleskSiteService->getSiteByName($this->extConf->getDomain(), $pleskClient)) + ($domain = $this->extConf->getViewConfiguration()->getDomain()) + && ($site = $this->pleskSiteService->getSiteByName($domain, $pleskClient)) && $ipAddresses = $site->getHosting()->getIpAddresses() ) { $variables['ipAddresses'] = $ipAddresses; diff --git a/Classes/Widget/WebspaceWidget.php b/Classes/Widget/WebspaceWidget.php index 1c47fe8..da90134 100644 --- a/Classes/Widget/WebspaceWidget.php +++ b/Classes/Widget/WebspaceWidget.php @@ -63,7 +63,7 @@ public function getEventData(): array ], 'title' => [ 'display' => true, - 'text' => 'Usage in ' . $this->extConf->getDiskUsageType(), + 'text' => 'Usage in ' . $this->extConf->getViewConfiguration()->getDiskUsageType(), ], 'tooltip' => [ 'enabled' => true, diff --git a/Tests/Functional/Configuration/ExtConfTest.php b/Tests/Functional/Configuration/ExtConfTest.php index bd5a55d..c4505e9 100644 --- a/Tests/Functional/Configuration/ExtConfTest.php +++ b/Tests/Functional/Configuration/ExtConfTest.php @@ -16,7 +16,6 @@ use StefanFroemken\PleskWidget\Configuration\DiskUsageTypeEnum; use StefanFroemken\PleskWidget\Configuration\ExtConf; use StefanFroemken\PleskWidget\Configuration\ViewConfiguration; -use TypeError; use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; /**