Skip to content

Commit

Permalink
✅ Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
marcreichel committed Mar 10, 2024
1 parent 8ae8301 commit 3dd216c
Show file tree
Hide file tree
Showing 7 changed files with 313 additions and 32 deletions.
30 changes: 12 additions & 18 deletions src/Console/CreateWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public function handle(): int
$this->error('Model "' . $model . '" does not exist.');
$closestModel = $this->getClosestModel($model);
if (!$closestModel) {
return 1;
return self::FAILURE;
}
if (!$this->confirm('Did you mean <comment>' . $closestModel . '</comment>?')) {
return 1;
return self::FAILURE;
}
$fullQualifiedName = $namespace . $closestModel;
}
Expand All @@ -65,7 +65,7 @@ public function handle(): int
if (!in_array($method, $methods, true)) {
$this->error((new InvalidWebhookMethodException())->getMessage());

return 1;
return self::FAILURE;
}

$mappedMethod = match ($method) {
Expand All @@ -79,38 +79,32 @@ public function handle(): int
} catch (Exception $e) {
$this->error($e->getMessage());

return 1;
return self::FAILURE;
}

$this->info('Webhook created successfully!');

return 0;
return self::SUCCESS;
}

private function getModels(): array
{
$pattern = '/\/(?:Model|Search|Webhook|Image)\.php$/';
$glob = glob(__DIR__ . '/../Models/*.php');

if (!$glob) {
return [];
}
$glob = glob(__DIR__ . '/../Models/*.php') ?? [];

Check failure on line 92 in src/Console/CreateWebhook.php

View workflow job for this annotation

GitHub Actions / phpstan

Expression on left side of ?? is not nullable.

$pattern = '/\/(?:Model|Search|Webhook|Image)\.php$/';
$grep = preg_grep($pattern, $glob, PREG_GREP_INVERT);

Check failure on line 95 in src/Console/CreateWebhook.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $array of function preg_grep expects array, array<int, string>|false given.

return collect($grep ?: [])
->map(fn ($path) => basename($path, '.php'))
->toArray();
}

private function getClosestModel(string $model): string
private function getClosestModel(string $model): ?string
{
return collect($this->getModels())->map(function ($m) use ($model) {
return [
'model' => $m,
'levenshtein' => levenshtein($m, $model),
];
})
return collect($this->getModels())->map(fn ($m) => [
'model' => $m,
'levenshtein' => levenshtein($m, $model),
])
->filter(fn ($m) => $m['levenshtein'] <= 5)
->sortBy(fn ($m) => $m['levenshtein'])
->map(fn ($m) => $m['model'])
Expand Down
12 changes: 6 additions & 6 deletions src/Console/DeleteWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function handle(): int
return $this->deleteAll();
}

return 0;
return self::FAILURE;
}

private function deleteOne(int $id): int
Expand All @@ -41,18 +41,18 @@ private function deleteOne(int $id): int
if (!$webhook instanceof Webhook) {
$this->error('Webhook not found.');

return 1;
return self::FAILURE;
}

if (!$webhook->delete()) {
$this->error('Webhook could not be deleted.');

return 1;
return self::FAILURE;
}

$this->info('Webhook deleted.');

return 0;
return self::SUCCESS;
}

private function deleteAll(): int
Expand All @@ -62,7 +62,7 @@ private function deleteAll(): int
if (!$webhooks->count()) {
$this->info('You do not have any registered webhooks.');

return 1;
return self::SUCCESS;
}

$this->comment('Deleting all your registered webhooks ...');
Expand All @@ -75,6 +75,6 @@ private function deleteAll(): int

$this->info('All Webhooks deleted.');

return 0;
return self::SUCCESS;
}
}
4 changes: 2 additions & 2 deletions src/Console/ListWebhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function handle(): int
if (!$webhooks->count()) {
$this->warn('You do not have any registered webhooks.');

return 1;
return self::FAILURE;
}
$this->table(
['ID', 'URL', 'Model', 'Method', 'Retries', 'Active'],
Expand All @@ -33,6 +33,6 @@ public function handle(): int
})->sortBy('id')->toArray(),
);

return 0;
return self::SUCCESS;
}
}
10 changes: 5 additions & 5 deletions src/Console/ReactivateWebhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function handle(): int
if (!$webhook) {
$this->error('Webhook not found.');

return 1;
return self::FAILURE;
}

if ($webhook->active) {
$this->info('Webhook does not need to be reactivated.');

return 0;
return self::SUCCESS;
}

$model = $webhook->getModel();
Expand All @@ -47,7 +47,7 @@ public function handle(): int
if (!class_exists($fullQualifiedName)) {
$this->error('Model not found.');

return 1;
return self::FAILURE;
}

/** @var Model $class */
Expand All @@ -58,11 +58,11 @@ public function handle(): int
} catch (AuthenticationException | WebhookSecretMissingException $e) {
$this->error($e->getMessage());

return 1;
return self::FAILURE;
}

$this->info('Webhook reactivated.');

return 0;
return self::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ public static function createWebhook(Method | string $method, array $parameters
])->throw()->json();

if (!is_array($response)) {
throw new Exception('An error occured while trying to create the webhook.');
throw new Exception('An error occurred while trying to create the webhook.');
}

return new Webhook(...$response);
Expand Down
1 change: 1 addition & 0 deletions src/Models/Webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static function find(int $id): ?self

public function delete(): mixed
{
ray($this->id);
if (!$this->id) {
return false;
}
Expand Down
Loading

0 comments on commit 3dd216c

Please sign in to comment.