Skip to content

Commit

Permalink
Merge pull request #5389 from nextcloud/enh/noid/constructor
Browse files Browse the repository at this point in the history
Inprove constructor
  • Loading branch information
szaimen authored Oct 7, 2024
2 parents a5e8c82 + 496ec9b commit a54cfed
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 193 deletions.
8 changes: 4 additions & 4 deletions php/src/Auth/AuthManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use AIO\Data\DataConst;
use \DateTime;

class AuthManager {
readonly class AuthManager {
private const string SESSION_KEY = 'aio_authenticated';
private ConfigurationManager $configurationManager;

public function __construct(ConfigurationManager $configurationManager) {
$this->configurationManager = $configurationManager;
public function __construct(
private ConfigurationManager $configurationManager
) {
}

public function CheckCredentials(string $password) : bool {
Expand Down
109 changes: 30 additions & 79 deletions php/src/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,38 @@
use AIO\Docker\DockerActionManager;
use AIO\ContainerDefinitionFetcher;

class Container {
private string $identifier;
private string $displayName;
private string $containerName;
private string $restartPolicy;
private int $maxShutdownTime;
private ContainerPorts $ports;
private string $internalPorts;
private ContainerVolumes $volumes;
private ContainerEnvironmentVariables $containerEnvironmentVariables;
/** @var string[] */
private array $dependsOn;
/** @var string[] */
private array $secrets;
/** @var string[] */
private array $devices;
/** @var string[] */
private array $capAdd;
private int $shmSize;
private bool $apparmorUnconfined;
/** @var string[] */
private array $backupVolumes;
private array $nextcloudExecCommands;
private bool $readOnlyRootFs;
private array $tmpfs;
private bool $init;
private string $imageTag;
private AioVariables $aioVariables;
private string $documentation;
private DockerActionManager $dockerActionManager;

readonly class Container {
public function __construct(
string $identifier,
string $displayName,
string $containerName,
string $restartPolicy,
int $maxShutdownTime,
ContainerPorts $ports,
string $internalPorts,
ContainerVolumes $volumes,
ContainerEnvironmentVariables $containerEnvironmentVariables,
array $dependsOn,
array $secrets,
array $devices,
array $capAdd,
int $shmSize,
bool $apparmorUnconfined,
array $backupVolumes,
array $nextcloudExecCommands,
bool $readOnlyRootFs,
array $tmpfs,
bool $init,
string $imageTag,
AioVariables $aioVariables,
string $documentation,
DockerActionManager $dockerActionManager
private string $identifier,
private string $displayName,
private string $containerName,
private string $restartPolicy,
private int $maxShutdownTime,
private ContainerPorts $ports,
private string $internalPorts,
private ContainerVolumes $volumes,
private ContainerEnvironmentVariables $containerEnvironmentVariables,
/** @var string[] */
private array $dependsOn,
/** @var string[] */
private array $secrets,
/** @var string[] */
private array $devices,
/** @var string[] */
private array $capAdd,
private int $shmSize,
private bool $apparmorUnconfined,
/** @var string[] */
private array $backupVolumes,
private array $nextcloudExecCommands,
private bool $readOnlyRootFs,
private array $tmpfs,
private bool $init,
private string $imageTag,
private AioVariables $aioVariables,
private string $documentation,
private DockerActionManager $dockerActionManager
) {
$this->identifier = $identifier;
$this->displayName = $displayName;
$this->containerName = $containerName;
$this->restartPolicy = $restartPolicy;
$this->maxShutdownTime = $maxShutdownTime;
$this->ports = $ports;
$this->internalPorts = $internalPorts;
$this->volumes = $volumes;
$this->containerEnvironmentVariables = $containerEnvironmentVariables;
$this->dependsOn = $dependsOn;
$this->secrets = $secrets;
$this->devices = $devices;
$this->capAdd = $capAdd;
$this->shmSize = $shmSize;
$this->apparmorUnconfined = $apparmorUnconfined;
$this->backupVolumes = $backupVolumes;
$this->nextcloudExecCommands = $nextcloudExecCommands;
$this->readOnlyRootFs = $readOnlyRootFs;
$this->tmpfs = $tmpfs;
$this->init = $init;
$this->imageTag = $imageTag;
$this->aioVariables = $aioVariables;
$this->documentation = $documentation;
$this->dockerActionManager = $dockerActionManager;
}

public function GetIdentifier() : string {
Expand Down
13 changes: 3 additions & 10 deletions php/src/Container/ContainerPort.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@
namespace AIO\Container;

class ContainerPort {
public string $port;
public string $ipBinding;
public string $protocol;

public function __construct(
string $port,
string $ipBinding,
string $protocol
public string $port,
public string $ipBinding,
public string $protocol
) {
$this->port = $port;
$this->ipBinding = $ipBinding;
$this->protocol = $protocol;
}
}
13 changes: 3 additions & 10 deletions php/src/Container/ContainerVolume.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@
namespace AIO\Container;

class ContainerVolume {
public string $name;
public string $mountPoint;
public bool $isWritable;

public function __construct(
string $name,
string $mountPoint,
bool $isWritable
public string $name,
public string $mountPoint,
public bool $isWritable
) {
$this->name = $name;
$this->mountPoint = $mountPoint;
$this->isWritable = $isWritable;
}
}
19 changes: 6 additions & 13 deletions php/src/ContainerDefinitionFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@
use AIO\Data\DataConst;
use AIO\Docker\DockerActionManager;

class ContainerDefinitionFetcher
{
private ConfigurationManager $configurationManager;
private \DI\Container $container;

readonly class ContainerDefinitionFetcher {
public function __construct(
ConfigurationManager $configurationManager,
\DI\Container $container
)
{
$this->configurationManager = $configurationManager;
$this->container = $container;
private ConfigurationManager $configurationManager,
private \DI\Container $container
) {
}

public function GetContainerById(string $id): Container
Expand Down Expand Up @@ -103,7 +96,7 @@ private function GetDefinition(): array

$ports = new ContainerPorts();
if (isset($entry['ports'])) {
foreach ($entry['ports'] as $value) {
foreach ($entry['ports'] as $value) {
$ports->AddPort(
new ContainerPort(
$value['port_number'],
Expand Down Expand Up @@ -212,7 +205,7 @@ private function GetDefinition(): array
$dependsOn[] = $value;
}
}

$variables = new ContainerEnvironmentVariables();
if (isset($entry['environment'])) {
foreach ($entry['environment'] as $value) {
Expand Down
8 changes: 2 additions & 6 deletions php/src/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class ConfigurationController
{
private ConfigurationManager $configurationManager;

readonly class ConfigurationController {
public function __construct(
ConfigurationManager $configurationManager
private ConfigurationManager $configurationManager
) {
$this->configurationManager = $configurationManager;
}

public function SetConfig(Request $request, Response $response, array $args) : Response {
Expand Down
17 changes: 5 additions & 12 deletions php/src/Controller/DockerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@
use Psr\Http\Message\ServerRequestInterface as Request;
use AIO\Data\ConfigurationManager;

class DockerController
{
private DockerActionManager $dockerActionManager;
private ContainerDefinitionFetcher $containerDefinitionFetcher;
readonly class DockerController {
private const string TOP_CONTAINER = 'nextcloud-aio-apache';
private ConfigurationManager $configurationManager;

public function __construct(
DockerActionManager $dockerActionManager,
ContainerDefinitionFetcher $containerDefinitionFetcher,
ConfigurationManager $configurationManager
private DockerActionManager $dockerActionManager,
private ContainerDefinitionFetcher $containerDefinitionFetcher,
private ConfigurationManager $configurationManager
) {
$this->dockerActionManager = $dockerActionManager;
$this->containerDefinitionFetcher = $containerDefinitionFetcher;
$this->configurationManager = $configurationManager;
}

private function PerformRecursiveContainerStart(string $id, bool $pullImage = true) : void {
Expand All @@ -48,7 +41,7 @@ private function PerformRecursiveContainerStart(string $id, bool $pullImage = tr
}
}

// Check if docker hub is reachable in order to make sure that we do not try to pull an image if it is down
// Check if docker hub is reachable in order to make sure that we do not try to pull an image if it is down
// and try to mitigate issues that are arising due to that
if ($pullImage) {
if (!$this->dockerActionManager->isDockerHubReachable($container)) {
Expand Down
13 changes: 5 additions & 8 deletions php/src/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;

class LoginController
{
private AuthManager $authManager;
private DockerActionManager $dockerActionManager;

public function __construct(AuthManager $authManager, DockerActionManager $dockerActionManager) {
$this->authManager = $authManager;
$this->dockerActionManager = $dockerActionManager;
readonly class LoginController {
public function __construct(
private AuthManager $authManager,
private DockerActionManager $dockerActionManager,
) {
}

public function TryLogin(Request $request, Response $response, array $args) : Response {
Expand Down
13 changes: 4 additions & 9 deletions php/src/Data/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,11 @@

use AIO\Auth\PasswordGenerator;

class Setup
{
private PasswordGenerator $passwordGenerator;
private ConfigurationManager $configurationManager;

readonly class Setup {
public function __construct(
PasswordGenerator $passwordGenerator,
ConfigurationManager $configurationManager) {
$this->passwordGenerator = $passwordGenerator;
$this->configurationManager = $configurationManager;
private PasswordGenerator $passwordGenerator,
private ConfigurationManager $configurationManager,
) {
}

public function Setup() : string {
Expand Down
27 changes: 7 additions & 20 deletions php/src/Docker/DockerActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,21 @@
use AIO\Container\State\StoppedState;
use AIO\Container\State\VersionEqualState;
use AIO\Data\ConfigurationManager;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use AIO\ContainerDefinitionFetcher;
use http\Env\Response;

class DockerActionManager
{
readonly class DockerActionManager {
private const string API_VERSION = 'v1.41';
private \GuzzleHttp\Client $guzzleClient;
private ConfigurationManager $configurationManager;
private ContainerDefinitionFetcher $containerDefinitionFetcher;
private DockerHubManager $dockerHubManager;
private Client $guzzleClient;

public function __construct(
ConfigurationManager $configurationManager,
ContainerDefinitionFetcher $containerDefinitionFetcher,
DockerHubManager $dockerHubManager
private ConfigurationManager $configurationManager,
private ContainerDefinitionFetcher $containerDefinitionFetcher,
private DockerHubManager $dockerHubManager
) {
$this->configurationManager = $configurationManager;
$this->containerDefinitionFetcher = $containerDefinitionFetcher;
$this->dockerHubManager = $dockerHubManager;
$this->guzzleClient = new \GuzzleHttp\Client(
[
'curl' => [
CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock',

],
]
);
$this->guzzleClient = new Client(['curl' => [CURLOPT_UNIX_SOCKET_PATH => '/var/run/docker.sock']]);
}

private function BuildApiUrl(string $url) : string {
Expand Down
9 changes: 4 additions & 5 deletions php/src/Docker/DockerHubManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
use AIO\Data\ConfigurationManager;
use GuzzleHttp\Client;

class DockerHubManager
{
readonly class DockerHubManager {
private Client $guzzleClient;

public function __construct()
{
public function __construct(
) {
$this->guzzleClient = new Client();
}

Expand Down Expand Up @@ -59,4 +58,4 @@ public function GetLatestDigestOfTag(string $name, string $tag) : ?string {
return null;
}
}
}
}
10 changes: 4 additions & 6 deletions php/src/Middleware/AuthMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

class AuthMiddleware
{
private AuthManager $authManager;

public function __construct(AuthManager $authManager) {
$this->authManager = $authManager;
readonly class AuthMiddleware {
public function __construct(
private AuthManager $authManager
) {
}

public function __invoke(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Expand Down
Loading

0 comments on commit a54cfed

Please sign in to comment.