Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ExtConf builder #13

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions Classes/Builder/ExtConfBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/plesk-widget.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\PleskWidget\Builder;

/*
* Builder to create ExtConf object
*/

use StefanFroemken\PleskWidget\Configuration\CredentialsConfiguration;
use StefanFroemken\PleskWidget\Configuration\DiskUsageTypeEnum;
use StefanFroemken\PleskWidget\Configuration\ExtConf;
use StefanFroemken\PleskWidget\Configuration\ViewConfiguration;
use TYPO3\CMS\Core\Utility\MathUtility;
use ValueError;

class ExtConfBuilder
{
private string $host = '';

private int $port = 8443;

private string $username = '';

private string $password = '';

private DiskUsageTypeEnum $diskUsageType = DiskUsageTypeEnum::PERCENT;

private string $domain = '';

public function setHost(string $host): self
{
$this->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);
}
}
23 changes: 23 additions & 0 deletions Classes/Builder/ExtConfBuilderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/plesk-widget.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\PleskWidget\Builder;

/*
* Builder to create ExtConf object
*/
readonly class ExtConfBuilderFactory
{
public function createBuilder(): ExtConfBuilder
{
return new ExtConfBuilder();
}
}
16 changes: 11 additions & 5 deletions Classes/Client/PleskClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
45 changes: 45 additions & 0 deletions Classes/Configuration/CredentialsConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the package stefanfroemken/plesk-widget.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

namespace StefanFroemken\PleskWidget\Configuration;

/*
* This class streamlines credential related settings from extension settings
*/
readonly class CredentialsConfiguration
{
public function __construct(
private string $host,
private int $port,
private string $username,
private string $password,
) {}

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;
}
}
50 changes: 8 additions & 42 deletions Classes/Configuration/ExtConf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
71 changes: 41 additions & 30 deletions Classes/Configuration/ExtConfFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,57 @@

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;
use TYPO3\CMS\Core\Utility\MathUtility;
use ValueError;

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
{
$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) {
Expand All @@ -73,4 +70,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();
}
}
Loading
Loading