Skip to content

Commit

Permalink
Fix CS
Browse files Browse the repository at this point in the history
  • Loading branch information
nmtho committed Dec 1, 2023
1 parent 3090c22 commit 7484f0d
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 90 deletions.
2 changes: 1 addition & 1 deletion src/thebigcrafter/omp/OhMyPMMP.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function onLoad() : void

public function onEnable() : void
{
UpdateChecker::init($this);
UpdateChecker::init($this);
$this->fetchData();
$this->saveDefaultConfig();

Expand Down
34 changes: 21 additions & 13 deletions src/thebigcrafter/omp/helpers/UpdateChecker.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
<?php

declare(strict_types=1);

namespace thebigcrafter\omp\helpers;


/*
* This file is part of oh-my-pmmp.
*
* (c) thebigcrafter <[email protected]>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\helpers;

use pocketmine\plugin\Plugin;
use pocketmine\Server;
use thebigcrafter\omp\OhMyPMMP;
use thebigcrafter\omp\tasks\CheckForUpdates;

use thebigcrafter\omp\tasks\CheckForUpdates;

class UpdateChecker
{
public static function init(Plugin $plugin): void
{
OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion()));
}
}
public static function init(Plugin $plugin) : void
{
OhMyPMMP::getInstance()->getServer()->getAsyncPool()->submitTask(new CheckForUpdates($plugin->getDescription()->getName(), $plugin->getDescription()->getVersion()));
}
}
117 changes: 65 additions & 52 deletions src/thebigcrafter/omp/tasks/CheckForUpdates.php
Original file line number Diff line number Diff line change
@@ -1,57 +1,70 @@
<?php

namespace thebigcrafter\omp\tasks;

<?php

/*
* This file is part of oh-my-pmmp.
*
* (c) thebigcrafter <[email protected]>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\tasks;

use pocketmine\scheduler\AsyncTask;
use pocketmine\utils\Internet;
use thebigcrafter\omp\Language;
use thebigcrafter\omp\OhMyPMMP;

use thebigcrafter\omp\OhMyPMMP;
use function json_decode;
use function version_compare;

final class CheckForUpdates extends AsyncTask
{
private string $highestVersion;
private string $artifactUrl;
public function __construct(private readonly string $name, private string $currentVersion)
{
$this->highestVersion = $currentVersion;
$this->artifactUrl = "";
}

public function onRun(): void
{
$res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name);

$releases = (array) json_decode($res->getBody(), true);

if ($releases !== null) {
/**
* @var array{'version': string, 'artifact_url': string} $release
*/
foreach ($releases as $release) {
if (version_compare($this->highestVersion, $release["version"], ">=")) {
continue;
}

$this->highestVersion = $release["version"];
$this->artifactUrl = $release["artifact_url"];
}
}

if ($this->highestVersion !== $this->currentVersion) {
$this->setResult(false);
}
}

public function onCompletion(): void
{
if (!$this->getResult()) {
$this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar";
OhMyPMMP::getInstance()->getLogger()->notice(
Language::translate(
"messages.new_update",
["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl]
)
);
}
}
}
private string $highestVersion;
private string $artifactUrl;
public function __construct(private readonly string $name, private string $currentVersion)
{
$this->highestVersion = $currentVersion;
$this->artifactUrl = "";
}

public function onRun() : void
{
$res = Internet::getURL("https://poggit.pmmp.io/releases.min.json?name=" . $this->name);

$releases = (array) json_decode($res->getBody(), true);

if ($releases !== null) {
/**
* @var array{'version': string, 'artifact_url': string} $release
*/
foreach ($releases as $release) {
if (version_compare($this->highestVersion, $release["version"], ">=")) {
continue;
}

$this->highestVersion = $release["version"];
$this->artifactUrl = $release["artifact_url"];
}
}

if ($this->highestVersion !== $this->currentVersion) {
$this->setResult(false);
}
}

public function onCompletion() : void
{
if (!$this->getResult()) {
$this->artifactUrl .= "/{$this->name}_{$this->highestVersion}.phar";
OhMyPMMP::getInstance()->getLogger()->notice(
Language::translate(
"messages.new_update",
["name" => $this->name, "version" => $this->highestVersion, "download_url" => $this->artifactUrl]
)
);
}
}
}
44 changes: 22 additions & 22 deletions src/thebigcrafter/omp/tasks/RemovePluginTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@

class RemovePluginTask extends Task
{
public function __construct(private readonly string $name, private readonly bool $wipeData)
{
}
public function execute(): bool
{
$name = $this->name;
$wipeData = $this->wipeData;
public function __construct(private readonly string $name, private readonly bool $wipeData)
{
}
public function execute() : bool
{
$name = $this->name;
$wipeData = $this->wipeData;

$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");
$pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name);
$pluginFilePath = Path::join(Utils::getPluginsFolder(), "$name.phar");
$pluginFolderPath = Path::join(Utils::getPluginsFolder(), $name);

if (Filesystem::exists($pluginFilePath)) {
Await::g2c(Filesystem::remove($pluginFilePath));
} elseif (Filesystem::exists($pluginFolderPath)) {
Await::g2c(Filesystem::remove($pluginFolderPath));
} else {
return false;
}
if ($wipeData) {
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name);
Await::g2c(Filesystem::remove($pluginDataFolder));
}
return true;
}
if (Filesystem::exists($pluginFilePath)) {
Await::g2c(Filesystem::remove($pluginFilePath));
} elseif (Filesystem::exists($pluginFolderPath)) {
Await::g2c(Filesystem::remove($pluginFolderPath));
} else {
return false;
}
if ($wipeData) {
$pluginDataFolder = Path::join(OhMyPMMP::getInstance()->getDataFolder(), "..", $name);
Await::g2c(Filesystem::remove($pluginDataFolder));
}
return true;
}
}
15 changes: 13 additions & 2 deletions src/thebigcrafter/omp/utils/Filesystem.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of oh-my-pmmp.
*
* (c) thebigcrafter <[email protected]>
*
* This source file is subject to the GPL-3.0 license that is bundled
* with this source code in the file LICENSE.
*/

declare(strict_types=1);

namespace thebigcrafter\omp\utils;
Expand All @@ -16,16 +25,18 @@ public static function remove(string $path): Generator
return yield from Await::promise(function (Closure $resolve, Closure $reject) use ($path) {
$fs = new \Symfony\Component\Filesystem\Filesystem();
try {
// @phpstan-ignore-next-line
$resolve($fs->remove($path));
} catch (IOException $e) {
$reject($e);
}
});
}

public static function exists(\Traversable|array|string $files): bool
// @phpstan-ignore-next-line
public static function exists(iterable|string $files): bool
{
$fs = new \Symfony\Component\Filesystem\Filesystem();
return $fs->exists($files);
}
}
}

0 comments on commit 7484f0d

Please sign in to comment.