Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@
"require-dev": {
"phpunit/phpunit": "^9.6.22",
"rector/rector": "^2.0",
"phpstan/phpstan": "^2.1"
"phpstan/phpstan": "^2.1",
"symfony/yaml": "^7|^8",
"symfony/messenger": "^7.4",
"symfony/security-bundle": "^7.4",
"symfony/uid": "^7.4",
"symfony/string": "^7.4",
"doctrine/orm": "^3.6",
"doctrine/doctrine-bundle": "^2.18"
},
"scripts": {
"test": "vendor/bin/phpunit",
Expand Down
6 changes: 0 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
includes:
- phpstan-baseline.neon

parameters:
level: max
paths:
Expand All @@ -14,6 +11,3 @@ parameters:

ignoreErrors:
- identifier: missingType.iterableValue
- identifier: return.unusedType
- identifier: staticMethod.void
- identifier: staticMethod.alreadyNarrowedType
30 changes: 30 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Zabbix API Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>src/DependencyInjection</directory>
<directory>src/Resources</directory>
<directory>src/Enums</directory>
<directory>src/Actions/Dto</directory>
<directory>src/Provisioning/Dto</directory>
<directory>src/Provisioning/ValueObject</directory>
<directory>src/Message</directory>
</exclude>
</coverage>
</phpunit>
24 changes: 23 additions & 1 deletion src/ActionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public function __construct(

/**
* @param class-string<AbstractAction> $actionClass
* @param array<string, mixed> $input
*
* @throws ZabbixApiException
*/
Expand All @@ -32,6 +33,13 @@ public function call(string $actionClass, array $input): mixed
$method = $input['method'] ?? 'get';
$params = $input['params'] ?? $input;

if (!is_string($method)) {
throw new ZabbixApiException(
'Method must be a string',
-1,
);
}

$actionString = sprintf('%s.%s', $base, $method);

try {
Expand All @@ -45,6 +53,20 @@ public function call(string $actionClass, array $input): mixed
);
}

return $this->zabbixClient->call($action, $params);
if (!is_array($params)) {
throw new ZabbixApiException(
'Params must be an array',
-1,
);
}

$typedParams = [];
foreach ($params as $key => $value) {
if (is_string($key)) {
$typedParams[$key] = $value;
}
}

return $this->zabbixClient->call($action, $typedParams);
}
}
2 changes: 2 additions & 0 deletions src/ActionServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
interface ActionServiceInterface
{
/**
* @param array<string, mixed> $input
*
* @throws ZabbixApiException
*/
public function call(string $actionClass, array $input): mixed;
Expand Down
18 changes: 16 additions & 2 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use BytesCommerce\ZabbixApi\Actions\Dto\GetActionDto;
use BytesCommerce\ZabbixApi\Enums\OutputEnum;
use BytesCommerce\ZabbixApi\Enums\ZabbixAction;
use Webmozart\Assert\Assert;

final class Action extends AbstractAction
{
Expand All @@ -32,6 +33,7 @@ public function get(GetActionDto $dto): GetActionResponseDto
}

$result = $this->client->call(ZabbixAction::ACTION_GET, $params);
Assert::isArray($result);

return GetActionResponseDto::fromArray($result);
}
Expand All @@ -40,7 +42,10 @@ public function create(CreateActionDto $dto): CreateActionResponseDto
{
$actions = array_map($this->mapCreateAction(...), $dto->actions);

$result = $this->client->call(ZabbixAction::ACTION_CREATE, $actions);
$result = $this->client->call(ZabbixAction::ACTION_CREATE, ['actions' => $actions]);
Assert::isArray($result);
Assert::keyExists($result, 'actionids');
Assert::isArray($result['actionids']);

return new CreateActionResponseDto($result['actionids']);
}
Expand All @@ -49,7 +54,10 @@ public function update(UpdateActionDto $dto): UpdateActionResponseDto
{
$actions = array_map($this->mapUpdateAction(...), $dto->actions);

$result = $this->client->call(ZabbixAction::ACTION_UPDATE, $actions);
$result = $this->client->call(ZabbixAction::ACTION_UPDATE, ['actions' => $actions]);
Assert::isArray($result);
Assert::keyExists($result, 'actionids');
Assert::isArray($result['actionids']);

return new UpdateActionResponseDto($result['actionids']);
}
Expand All @@ -61,6 +69,9 @@ public function delete(DeleteActionDto $dto): DeleteActionResponseDto
return new DeleteActionResponseDto();
}

/**
* @return array<string, mixed>
*/
private function mapCreateAction(CreateSingleActionDto $action): array
{
$data = [
Expand Down Expand Up @@ -96,6 +107,9 @@ private function mapCreateAction(CreateSingleActionDto $action): array
return $data;
}

/**
* @return array<string, mixed>
*/
private function mapUpdateAction(UpdateSingleActionDto $action): array
{
$data = [
Expand Down
9 changes: 9 additions & 0 deletions src/Actions/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use BytesCommerce\ZabbixApi\Enums\OutputEnum;
use BytesCommerce\ZabbixApi\Enums\ZabbixAction;
use DateTimeInterface;
use Webmozart\Assert\Assert;

final class Alert extends AbstractAction
{
Expand All @@ -16,15 +17,23 @@ public static function getActionPrefix(): string
return 'alert';
}

/**
* @param array<string, mixed> $params
*/
public function get(array $params): GetAlertResponseDto
{
$processedParams = $this->processParams($params);

$result = $this->client->call(ZabbixAction::ALERT_GET, $processedParams);
Assert::isArray($result);

return GetAlertResponseDto::fromArray($result);
}

/**
* @param array<string, mixed> $params
* @return array<string, mixed>
*/
private function processParams(array $params): array
{
if (!isset($params['output'])) {
Expand Down
9 changes: 9 additions & 0 deletions src/Actions/AuditLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use BytesCommerce\ZabbixApi\Enums\OutputEnum;
use BytesCommerce\ZabbixApi\Enums\ZabbixAction;
use DateTimeInterface;
use Webmozart\Assert\Assert;

final class AuditLog extends AbstractAction
{
Expand All @@ -16,15 +17,23 @@ public static function getActionPrefix(): string
return 'auditlog';
}

/**
* @param array<string, mixed> $params
*/
public function get(array $params): GetAuditLogResponseDto
{
$processedParams = $this->processParams($params);

$result = $this->client->call(ZabbixAction::AUDITLOG_GET, $processedParams);
Assert::isArray($result);

return GetAuditLogResponseDto::fromArray($result);
}

/**
* @param array<string, mixed> $params
* @return array<string, mixed>
*/
private function processParams(array $params): array
{
if (!isset($params['output'])) {
Expand Down
35 changes: 31 additions & 4 deletions src/Actions/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use BytesCommerce\ZabbixApi\Actions\Dto\UpdateDashboardResponseDto;
use BytesCommerce\ZabbixApi\Enums\OutputEnum;
use BytesCommerce\ZabbixApi\Enums\ZabbixAction;
use Webmozart\Assert\Assert;

final class Dashboard extends AbstractAction
{
Expand All @@ -36,20 +37,38 @@ public function get(GetDashboardDto $dto): GetDashboardResponseDto

public function create(CreateDashboardDto $dto): CreateDashboardResponseDto
{
$params = array_map($this->mapCreateDashboard(...), $dto->dashboards);
$params = [];
foreach ($dto->dashboards as $dashboard) {
$params[] = $this->mapCreateDashboard($dashboard);
}

$result = $this->client->call(ZabbixAction::DASHBOARD_CREATE, $params);
Assert::isArray($result);
Assert::keyExists($result, 'dashboardids');
Assert::isList($result['dashboardids']);

/** @var list<string> $dashboardids */
$dashboardids = $result['dashboardids'];

return new CreateDashboardResponseDto($result['dashboardids']);
return new CreateDashboardResponseDto($dashboardids);
}

public function update(UpdateDashboardDto $dto): UpdateDashboardResponseDto
{
$params = array_map($this->mapUpdateDashboard(...), $dto->dashboards);
$params = [];
foreach ($dto->dashboards as $dashboard) {
$params[] = $this->mapUpdateDashboard($dashboard);
}

$result = $this->client->call(ZabbixAction::DASHBOARD_UPDATE, $params);
Assert::isArray($result);
Assert::keyExists($result, 'dashboardids');
Assert::isList($result['dashboardids']);

/** @var list<string> $dashboardids */
$dashboardids = $result['dashboardids'];

return new UpdateDashboardResponseDto($result['dashboardids']);
return new UpdateDashboardResponseDto($dashboardids);
}

public function delete(DeleteDashboardDto $dto): DeleteDashboardResponseDto
Expand All @@ -59,6 +78,10 @@ public function delete(DeleteDashboardDto $dto): DeleteDashboardResponseDto
return new DeleteDashboardResponseDto();
}

/**
* @param array<string, mixed> $dashboard
* @return array<string, mixed>
*/
private function mapCreateDashboard(array $dashboard): array
{
$data = [
Expand Down Expand Up @@ -88,6 +111,10 @@ private function mapCreateDashboard(array $dashboard): array
return $data;
}

/**
* @param array<string, mixed> $dashboard
* @return array<string, mixed>
*/
private function mapUpdateDashboard(array $dashboard): array
{
$data = [
Expand Down
24 changes: 18 additions & 6 deletions src/Actions/Dto/ActionDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use BytesCommerce\ZabbixApi\Enums\EventSourceEnum;
use BytesCommerce\ZabbixApi\Enums\StatusEnum;
use Webmozart\Assert\Assert;

final readonly class ActionDto
{
Expand All @@ -24,16 +25,27 @@ public function __construct(

public static function fromArray(array $data): self
{
Assert::string($data['actionid'] ?? null);
Assert::string($data['name'] ?? null);
Assert::integerish($data['eventsource'] ?? null);
Assert::string($data['esc_period'] ?? null);

$status = null;
if (isset($data['status'])) {
Assert::integerish($data['status']);
$status = StatusEnum::from((int) $data['status']);
}

return new self(
actionid: $data['actionid'],
name: $data['name'],
eventsource: EventSourceEnum::from($data['eventsource']),
eventsource: EventSourceEnum::from((int) $data['eventsource']),
esc_period: $data['esc_period'],
status: isset($data['status']) ? StatusEnum::from($data['status']) : null,
filter: $data['filter'] ?? null,
operations: $data['operations'] ?? null,
recovery_operations: $data['recovery_operations'] ?? null,
update_operations: $data['update_operations'] ?? null,
status: $status,
filter: isset($data['filter']) && is_array($data['filter']) ? $data['filter'] : null,
operations: isset($data['operations']) && is_array($data['operations']) ? $data['operations'] : null,
recovery_operations: isset($data['recovery_operations']) && is_array($data['recovery_operations']) ? $data['recovery_operations'] : null,
update_operations: isset($data['update_operations']) && is_array($data['update_operations']) ? $data['update_operations'] : null,
);
}

Expand Down
Loading