Skip to content

Commit 5acbee6

Browse files
Add module statistics to Statistics class
Enhanced the Statistics class to handle module statistics, including adding the ModuleStatisticsInterface and implementing getModules and getModuleStatistics methods. Updated related tests and bumped the version in composer.json to 0.9.0.
1 parent 6f2c790 commit 5acbee6

File tree

9 files changed

+269
-57
lines changed

9 files changed

+269
-57
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pavlusha/unit-php-sdk",
3-
"version": "0.8.0",
3+
"version": "0.9.0",
44
"type": "sdk",
55
"description": "This project allows developers to interact with the Nginx Unit web server through PHP classes",
66
"license": "Apache-2.0",
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Contracts;
4+
5+
interface ModuleStatisticsInterface
6+
{
7+
/**
8+
* Language module version. If multiple versions are loaded, the list contains multiple items.
9+
*
10+
* @return string
11+
*/
12+
public function getVersion(): string;
13+
14+
/**
15+
* Path to the language module file
16+
*
17+
* @return string
18+
*/
19+
public function getLibPath(): string;
20+
}

src/Contracts/StatisticsInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,25 @@
33
namespace UnitPhpSdk\Contracts;
44

55
use UnitPhpSdk\Abstract\AbstractApplication;
6+
use UnitPhpSdk\Statistics\ModuleStatistics;
67

78
interface StatisticsInterface extends Arrayable, Jsonable
89
{
10+
/**
11+
* Get modules
12+
*
13+
* @return array
14+
*/
15+
public function getModules(): array;
16+
17+
/**
18+
* Get module statistics
19+
*
20+
* @param string $module
21+
* @return ModuleStatistics
22+
*/
23+
public function getModuleStatistics(string $module): ModuleStatisticsInterface;
24+
925
/**
1026
* Get connections
1127
*

src/Statistics/ModuleStatistics.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace UnitPhpSdk\Statistics;
4+
5+
use UnitPhpSdk\Contracts\Arrayable;
6+
use UnitPhpSdk\Contracts\ModuleStatisticsInterface;
7+
8+
class ModuleStatistics implements ModuleStatisticsInterface, Arrayable
9+
{
10+
/**
11+
* @var string Language module version. If multiple versions are loaded, the list contains multiple items.
12+
*/
13+
private string $version;
14+
15+
/**
16+
* @var string Path to the language module file
17+
*/
18+
private string $libPath;
19+
20+
public function __construct(array $data)
21+
{
22+
$this->parseFromArray($data);
23+
}
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function getVersion(): string
29+
{
30+
return $this->version;
31+
}
32+
33+
/**
34+
* @inheritDoc
35+
*/
36+
public function getLibPath(): string
37+
{
38+
return $this->libPath;
39+
}
40+
41+
public function parseFromArray(array $data)
42+
{
43+
if (!isset($data['version'])) {
44+
throw new \InvalidArgumentException('Version is required');
45+
}
46+
47+
if (!isset($data['lib'])) {
48+
throw new \InvalidArgumentException('Lib is required');
49+
}
50+
51+
$this->version = $data['version'];
52+
$this->libPath = $data['lib'];
53+
}
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
#[\Override] public function toArray(): array
59+
{
60+
return [
61+
'version' => $this->getVersion(),
62+
'lib' => $this->getLibPath()
63+
];
64+
}
65+
}

src/Statistics/Statistics.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use UnitPhpSdk\Contracts\{ApplicationStatisticsInterface,
1010
Arrayable,
1111
ConnectionsStatisticsInterface,
12+
ModuleStatisticsInterface,
1213
RequestsStatisticsInterface,
13-
StatisticsInterface
14-
};
14+
StatisticsInterface};
1515
use UnitPhpSdk\Exceptions\UnitParseException;
1616

1717
/**
@@ -22,6 +22,13 @@
2222
*/
2323
final readonly class Statistics implements StatisticsInterface
2424
{
25+
/**
26+
* Modules statistics
27+
*
28+
* @var array
29+
*/
30+
private array $modules;
31+
2532
/**
2633
* Connections statistics
2734
*
@@ -48,7 +55,7 @@
4855
*/
4956
public function __construct(array $data)
5057
{
51-
// $this->unitInformation = new UnitStatistics($data['unit']);
58+
$this->modules = array_map(fn ($item) => new ModuleStatistics($item), $data['modules']);
5259
$this->connections = new ConnectionsStatistics($data['connections']);
5360
$this->requests = new RequestsStatistics($data['requests']);
5461
$this->applications = array_map(fn ($item) => new ApplicationStatistics($item), $data['applications']);
@@ -94,12 +101,33 @@ public function getApplicationStatistics(AbstractApplication|string $application
94101
return $this->applications[$application->getName()];
95102
}
96103

104+
/**
105+
* @return array
106+
*/
107+
public function getModules(): array
108+
{
109+
return $this->modules;
110+
}
111+
112+
/**
113+
* @inheritDoc
114+
*/
115+
public function getModuleStatistics(string $module): ModuleStatisticsInterface
116+
{
117+
if (!array_key_exists($module, $this->modules)) {
118+
throw new InvalidArgumentException('Module with name ' . $module . ' not found');
119+
}
120+
121+
return $this->modules[$module];
122+
}
123+
97124
/**
98125
* @inheritDoc
99126
*/
100127
#[Override] public function toArray(): array
101128
{
102129
return [
130+
'modules' => array_map(fn ($item) => $item->toArray(), $this->modules),
103131
'connections' => $this->connections->toArray(),
104132
'requests' => $this->requests->toArray(),
105133
'applications' => array_map(fn ($item) => $item->toArray(), $this->applications),

src/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function parse_listener_pass(string $string): array
3030

3131
return $data;
3232
} else {
33-
throw new ParseError('Error when try to parse listenerPass');
33+
throw new ParseError('Invalid format. The string must start with applications, routes, or upstreams, followed by up to three optional segments separated by slashes, where each segment can contain letters, digits, underscores, or hyphens.');
3434
}
3535
}
3636
}

tests/Unit/Helpers/ListenerParseTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
foreach ($invalidStrings as $string) {
4949
$this->expectException(ParseError::class);
50-
$this->expectExceptionMessage('Error when try to parse listenerPass');
50+
$this->expectExceptionMessage('Invalid format. The string must start with applications, routes, or upstreams, followed by up to three optional segments separated by slashes, where each segment can contain letters, digits, underscores, or hyphens.');
5151

5252
parse_listener_pass($string);
5353
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Tests\Unit\Statistics;
4+
5+
use InvalidArgumentException;
6+
use UnitPhpSdk\Statistics\ModuleStatistics;
7+
8+
it('creates ModuleStatistics instance correctly', function () {
9+
$data = [
10+
'version' => '1.0.0',
11+
'lib' => '/path/to/libfile.so'
12+
];
13+
14+
$moduleStats = new ModuleStatistics($data);
15+
16+
expect($moduleStats->getVersion())->toBe('1.0.0')
17+
->and($moduleStats->getLibPath())->toBe('/path/to/libfile.so')
18+
->and($moduleStats->toArray())->toBe([
19+
'version' => '1.0.0',
20+
'lib' => '/path/to/libfile.so'
21+
]);
22+
});
23+
24+
it(
25+
/**
26+
* @throws InvalidArgumentException
27+
*/
28+
'throws InvalidArgumentException when version is missing',
29+
function () {
30+
$data = [
31+
'lib' => '/path/to/libfile.so'
32+
];
33+
34+
$this->expectException(InvalidArgumentException::class);
35+
$this->expectExceptionMessage('Version is required');
36+
37+
new ModuleStatistics($data);
38+
}
39+
);
40+
41+
it(
42+
/**
43+
* @throws InvalidArgumentException
44+
*/
45+
'throws InvalidArgumentException when lib is missing',
46+
function () {
47+
$data = [
48+
'version' => '1.0.0',
49+
];
50+
51+
$this->expectException(InvalidArgumentException::class);
52+
$this->expectExceptionMessage('Lib is required');
53+
54+
new ModuleStatistics($data);
55+
}
56+
);

0 commit comments

Comments
 (0)