Skip to content

Commit

Permalink
style: optimize global namespace functions and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
aarongerig committed Feb 8, 2023
1 parent 7b66fbe commit 29b2a14
Show file tree
Hide file tree
Showing 26 changed files with 195 additions and 294 deletions.
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
],
"require": {
"php": ">=8.0",
"laminas/laminas-diagnostics": "^1.8",
"laminas/laminas-diagnostics": "^1.24",
"pimcore/pimcore": "^10.0"
},
"require-dev": {
"deployer/deployer": "^7.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-symfony": "^0.12",
"deployer/deployer": "^7.1",
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-symfony": "^1.2",
"roave/security-advisories": "dev-latest",
"symplify/easy-coding-standard": "^9.0"
"symplify/easy-coding-standard": "^11.2"
},
"autoload": {
"psr-4": {
Expand Down
9 changes: 1 addition & 8 deletions src/PimcoreMonitorBundle/Check/AppEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ class AppEnvironment extends AbstractCheck
{
protected const IDENTIFIER = 'system:app_environment';

protected bool $skip;
protected string $environment;

public function __construct(bool $skip, string $environment)
{
$this->skip = $skip;
$this->environment = $environment;
}
public function __construct(protected bool $skip, protected string $environment) {}

/**
* {@inheritDoc}
Expand Down
46 changes: 17 additions & 29 deletions src/PimcoreMonitorBundle/Check/DiskUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,12 @@ class DiskUsage extends AbstractCheck
{
protected const IDENTIFIER = 'device:disk_usage';

protected bool $skip;
protected int $warningThreshold;
protected int $criticalThreshold;
protected string $path;

public function __construct(bool $skip, int $warningThreshold, int $criticalThreshold, string $path)
{
$this->skip = $skip;
$this->warningThreshold = $warningThreshold;
$this->criticalThreshold = $criticalThreshold;
$this->path = $path;
}
public function __construct(
protected bool $skip,
protected int $warningThreshold,
protected int $criticalThreshold,
protected string $path
) {}

/**
* {@inheritDoc}
Expand All @@ -49,32 +43,26 @@ public function check(): ResultInterface
return new Skip('Check was skipped');
}

$df = disk_free_space($this->path);
$dt = disk_total_space($this->path);
$df = \disk_free_space($this->path);
$dt = \disk_total_space($this->path);
$du = $dt - $df;
$dp = ($du / $dt) * 100;

$data = [
'used' => \formatBytes($du),
'free' => \formatBytes($df),
'total' => \formatBytes($dt),
];

if ($dp >= $this->criticalThreshold) {
return new Failure(sprintf('Disk usage too high: %2d%%', $dp), [
'used' => formatBytes($du),
'free' => formatBytes($df),
'total' => formatBytes($dt),
]);
return new Failure(\sprintf('Disk usage too high: %2d%%', $dp), $data);
}

if ($dp >= $this->warningThreshold) {
return new Warning(sprintf('Disk usage high: %2d%%', $dp), [
'used' => formatBytes($du),
'free' => formatBytes($df),
'total' => formatBytes($dt),
]);
return new Warning(\sprintf('Disk usage high: %2d%%', $dp), $data);
}

return new Success(sprintf('Disk usage is %2d%%', $dp), [
'used' => formatBytes($du),
'free' => formatBytes($df),
'total' => formatBytes($dt),
]);
return new Success(\sprintf('Disk usage is %2d%%', $dp), $data);
}

/**
Expand Down
39 changes: 19 additions & 20 deletions src/PimcoreMonitorBundle/Check/DoctrineMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Doctrine\Migrations\Metadata\AvailableMigration;
use Doctrine\Migrations\Metadata\ExecutedMigration;
use Doctrine\Migrations\Version\Version;
use InvalidArgumentException;
use Laminas\Diagnostics\Result\Failure;
use Laminas\Diagnostics\Result\ResultInterface;
use Laminas\Diagnostics\Result\Skip;
Expand All @@ -32,8 +31,6 @@ class DoctrineMigrations extends AbstractCheck
{
protected const IDENTIFIER = 'system:doctrine_migrations';

protected bool $skip;

/**
* Type depends on the installed version of doctrine/migrations:
* for ^2.0 it is string[], for ^3.0 it is Version[]
Expand All @@ -50,10 +47,8 @@ class DoctrineMigrations extends AbstractCheck
*/
protected array $migratedVersions;

public function __construct(bool $skip, $input)
public function __construct(protected bool $skip, $input)
{
$this->skip = $skip;

// check for doctrine/migrations:^3.0
if ($input instanceof DependencyFactory) {
$this->availableVersions = $this->getAvailableVersionsFromDependencyFactory($input);
Expand All @@ -63,15 +58,15 @@ public function __construct(bool $skip, $input)

// check for doctrine/migrations:^2.0
if ($input instanceof Configuration &&
method_exists($input, 'getAvailableVersions') &&
method_exists($input, 'getMigratedVersions')
\method_exists($input, 'getAvailableVersions') &&
\method_exists($input, 'getMigratedVersions')
) {
$this->availableVersions = $input->getAvailableVersions();
$this->migratedVersions = $input->getMigratedVersions();
return;
}

throw new InvalidArgumentException(<<<'MESSAGE'
throw new \InvalidArgumentException(<<<'MESSAGE'
Invalid Argument for DoctrineMigration check.
If you are using doctrine/migrations ^3.0, pass Doctrine\Migrations\DependencyFactory as argument.
If you are using doctrine/migrations ^2.0, pass Doctrine\Migrations\Configuration\Configuration as argument.
Expand All @@ -88,25 +83,27 @@ public function check(): ResultInterface
return new Skip('Check was skipped');
}

$notMigratedVersions = array_diff($this->availableVersions, $this->migratedVersions);
$notMigratedVersions = \array_diff($this->availableVersions, $this->migratedVersions);

if (! empty($notMigratedVersions)) {
return new Failure(
'Not all migrations applied',
array_values(array_map('strval', $notMigratedVersions))
\array_values(\array_map('strval', $notMigratedVersions))
);
}

$notAvailableVersions = array_diff($this->migratedVersions, $this->availableVersions);
$notAvailableVersions = \array_diff($this->migratedVersions, $this->availableVersions);

if (! empty($notAvailableVersions)) {
return new Failure(
'Migrations applied which are not available',
array_values(array_map('strval', $notAvailableVersions))
\array_values(\array_map('strval', $notAvailableVersions))
);
}

return new Success(
'All migrations are correctly applied',
array_values(array_map('strval', $this->migratedVersions))
\array_values(\array_map('strval', $this->migratedVersions))
);
}

Expand All @@ -125,9 +122,10 @@ private function getAvailableVersionsFromDependencyFactory(DependencyFactory $de
{
$allMigrations = $dependencyFactory->getMigrationRepository()->getMigrations();

return array_map(static function (AvailableMigration $availableMigration) {
return $availableMigration->getVersion();
}, $allMigrations->getItems());
return \array_map(
static fn (AvailableMigration $availableMigration) => $availableMigration->getVersion(),
$allMigrations->getItems()
);
}

/**
Expand All @@ -137,8 +135,9 @@ private function getMigratedVersionsFromDependencyFactory(DependencyFactory $dep
{
$executedMigrations = $dependencyFactory->getMetadataStorage()->getExecutedMigrations();

return array_map(static function (ExecutedMigration $executedMigration) {
return $executedMigration->getVersion();
}, $executedMigrations->getItems());
return \array_map(
static fn (ExecutedMigration $executedMigration) => $executedMigration->getVersion(),
$executedMigrations->getItems()
);
}
}
60 changes: 32 additions & 28 deletions src/PimcoreMonitorBundle/Check/HostingSize.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

declare(strict_types=1);

/**
* Pimcore Monitor
*
* LICENSE
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2022 w-vision AG (https://www.w-vision.ch)
* @license https://github.com/w-vision/PimcoreMonitorBundle/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
*/

namespace Wvision\Bundle\PimcoreMonitorBundle\Check;

use Laminas\Diagnostics\Result\Failure;
Expand All @@ -12,18 +27,12 @@ class HostingSize extends AbstractCheck
{
protected const IDENTIFIER = 'device:hosting_size';

protected bool $skip;
protected int $warningThreshold;
protected int $criticalThreshold;
protected string $path;

public function __construct(bool $skip, int $warningThreshold, int $criticalThreshold, string $path)
{
$this->skip = $skip;
$this->warningThreshold = $warningThreshold;
$this->criticalThreshold = $criticalThreshold;
$this->path = $path;
}
public function __construct(
protected bool $skip,
protected int $warningThreshold,
protected int $criticalThreshold,
protected string $path
) {}

/**
* {@inheritDoc}
Expand All @@ -35,25 +44,20 @@ public function check(): ResultInterface
}

$size = $this->getDirectorySize();
$data = [
'path' => $this->path,
'size' => \formatBytes($size),
];

if ($size >= $this->criticalThreshold) {
return new Failure(sprintf('Hosting size is too high: %s', formatBytes($size)), [
'path' => $this->path,
'size' => formatBytes($size),
]);
return new Failure(\sprintf('Hosting size is too high: %s', \formatBytes($size)), $data);
}

if ($size >= $this->warningThreshold) {
return new Warning(sprintf('Hosting size is high: %s', formatBytes($size)), [
'path' => $this->path,
'size' => formatBytes($size),
]);
return new Warning(\sprintf('Hosting size is high: %s', \formatBytes($size)), $data);
}

return new Success(sprintf('Hosting size is %s', formatBytes($size)), [
'path' => $this->path,
'size' => formatBytes($size),
]);
return new Success(sprintf('Hosting size is %s', \formatBytes($size)), $data);
}

/**
Expand All @@ -69,10 +73,10 @@ public function getLabel(): string
*/
private function getDirectorySize(): int
{
$io = popen('/usr/bin/du -sk ' . $this->path, 'r');
$size = fgets($io, 4096);
$size = (int) substr($size, 0, strpos($size, "\t"));
pclose($io);
$io = \popen('/usr/bin/du -sk ' . $this->path, 'r');
$size = \fgets($io, 4096);
$size = (int) \substr($size, 0, \strpos($size, "\t"));
\pclose($io);

return $size * 1024;
}
Expand Down
32 changes: 20 additions & 12 deletions src/PimcoreMonitorBundle/Check/HttpsConnection.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
<?php

declare(strict_types=1);

/**
* Pimcore Monitor
*
* LICENSE
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2022 w-vision AG (https://www.w-vision.ch)
* @license https://github.com/w-vision/PimcoreMonitorBundle/blob/master/gpl-3.0.txt GNU General Public License version 3 (GPLv3)
*/

namespace Wvision\Bundle\PimcoreMonitorBundle\Check;

use Laminas\Diagnostics\Result\Failure;
Expand All @@ -12,14 +27,7 @@ class HttpsConnection extends AbstractCheck
{
protected const IDENTIFIER = 'system:https_connection';

protected bool $skip;
protected array $systemConfig;

public function __construct(bool $skip, array $systemConfig)
{
$this->skip = $skip;
$this->systemConfig = $systemConfig;
}
public function __construct(protected bool $skip, protected array $systemConfig) {}

/**
* {@inheritDoc}
Expand All @@ -37,15 +45,15 @@ public function check(): ResultInterface
}

// Create a stream context
$stream = stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
$url = sprintf('https://%s', $host);
$stream = \stream_context_create(['ssl' => ['capture_peer_cert' => true]]);
$url = \sprintf('https://%s', $host);

try {
// Bind the resource $url to $stream
$read = fopen($url, 'rb', false, $stream);
$read = \fopen($url, 'rb', false, $stream);

// Get the stream parameters
$params = stream_context_get_params($read);
$params = \stream_context_get_params($read);
} catch (\Exception) {
// Ignore exceptions thrown ...
}
Expand Down
Loading

0 comments on commit 29b2a14

Please sign in to comment.