Skip to content

Commit

Permalink
PHPStan Level Up. Store modules as MultiSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuapease committed May 20, 2024
1 parent 5c86035 commit 1d90e78
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ includes:
- vendor/craftcms/phpstan/phpstan.neon

parameters:
level: 4
level: 9
paths:
- src
3 changes: 3 additions & 0 deletions src/PhoneHome.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class PhoneHome extends Plugin
public string $schemaVersion = '1.0.0';
public bool $hasCpSettings = true;

/**
* @phpstan-ignore-next-line
*/
public static function config(): array
{
return [
Expand Down
9 changes: 4 additions & 5 deletions src/endpoints/NotionEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class NotionEndpoint implements EndpointInterface
'class' => MultiSelectDb::class,
],
self::PROPERTY_MODULES => [
'class' => RichTextDb::class,
'class' => MultiSelectDb::class,
],
self::PROPERTY_DATE_UPDATED => [
'class' => DateDb::class,
Expand Down Expand Up @@ -157,9 +157,8 @@ public function send(SitePayload $payload): void
$page = $page ?? Page::create($parent);

// Update properties

$plugins = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->id)->values()->all();
$pluginVersions = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->versionedId)->values()->all();
$plugins = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->id)->all();
$pluginVersions = $payload->plugins->map(fn(SitePayloadPlugin $plugin) => $plugin->versionedId)->all();

$page = $page->addProperty(self::PROPERTY_NAME, Title::fromString($payload->siteName))
->addProperty(self::PROPERTY_URL, Url::create($payload->siteUrl))
Expand All @@ -170,7 +169,7 @@ public function send(SitePayload $payload): void
->addProperty(self::PROPERTY_DB_VERSION, Select::fromName($payload->dbVersion))
->addProperty(self::PROPERTY_PLUGINS, MultiSelect::fromNames(...$plugins))
->addProperty(self::PROPERTY_PLUGIN_VERSIONS, MultiSelect::fromNames(...$pluginVersions))
->addProperty(self::PROPERTY_MODULES, RichTextProperty::fromString($payload->modules))
->addProperty(self::PROPERTY_MODULES, MultiSelect::fromNames(...$payload->modules->all()))
->addProperty(self::PROPERTY_DATE_UPDATED, Date::create(new DateTimeImmutable('now', new DateTimeZone('UTC'))));

if ($isCreate) {
Expand Down
32 changes: 23 additions & 9 deletions src/models/SitePayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,40 @@ public function __construct(
public readonly string $siteUrl,
public readonly string $siteName,
public readonly string $environment,
/** @var string $craftEdition - Solo, Team, Pro, etc */
public readonly string $craftEdition,
/** @var string The version number */
public readonly string $craftVersion,
public readonly string $phpVersion,
public readonly string $dbVersion,
/** @var Collection<SitePayloadPlugin> $plugins */
/** @var Collection<int,SitePayloadPlugin> $plugins */
public readonly Collection $plugins,
public readonly string $modules
/** @var Collection<int,string> $modules */
public readonly Collection $modules
)
{
}

public static function fromSite(Site $site): self
{
$siteUrl = $site->getBaseUrl();
$environment = Craft::$app->env;

if (!$siteUrl || !$environment) {
throw new \Exception('$siteUrl or $environment not found');
}

return new self(
siteUrl: $site->getBaseUrl(),
siteUrl: $siteUrl,
siteName: $site->name,
environment: Craft::$app->env,
craftVersion: App::editionName(Craft::$app->getEdition()),
environment: $environment,
craftEdition: App::editionName(Craft::$app->getEdition()),
craftVersion: App::normalizeVersion(Craft::$app->getVersion()),
phpVersion: App::phpVersion(),
dbVersion: self::_dbDriver(),
plugins: Collection::make(Craft::$app->plugins->getAllPlugins())
->map(SitePayloadPlugin::fromPluginInterface(...)),
->map(SitePayloadPlugin::fromPluginInterface(...))
->values(),
modules: self::_modules()
);
}
Expand All @@ -62,9 +75,9 @@ private static function _dbDriver(): string
/**
* Returns the list of modules
*
* @return string
* @return Collection<int,string>
*/
private static function _modules(): string
private static function _modules(): Collection
{
$modules = [];

Expand All @@ -82,7 +95,8 @@ private static function _modules(): string
}
}

return implode(PHP_EOL, $modules);
// ->values() forces a 0 indexed array
return Collection::make($modules)->values();
}

}
5 changes: 3 additions & 2 deletions src/services/PhoneHomeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Craft;
use craft\helpers\Queue;
use craft\web\Request;
use Illuminate\Support\Collection;
use viget\phonehome\endpoints\EndpointInterface;
use viget\phonehome\jobs\SendPayloadJob;
Expand All @@ -29,13 +30,13 @@ public function tryQueuePhoneHome(): void
Craft::$app->getIsInstalled() === false
|| Craft::$app->getRequest()->getIsConsoleRequest()
|| !Craft::$app->getRequest()->getIsCpRequest() // Only run on CP request
|| $request->getIsAjax()
|| $request instanceof Request && $request->getIsAjax()
) {
return;
}

// Only run when the cache is empty (once per day at most)
if (Craft::$app->getCache()->get(self::CACHE_KEY) !== false) {
if (Craft::$app->getCache()?->get(self::CACHE_KEY) !== false) {
return;
}

Expand Down

0 comments on commit 1d90e78

Please sign in to comment.