Skip to content

Commit b791941

Browse files
committed
Refactor Specs task for better code organization
- Extract platform, auth counts, and keys into separate methods - Add getPlatforms(), getAuthCounts(), and getKeys() methods - Extract SDK platform logic into getSDKPlatformsForRouteSecurity() - Add PHPDoc comments with proper type hints - Move constructor to top of class for better readability
1 parent 7d2a95c commit b791941

1 file changed

Lines changed: 74 additions & 39 deletions

File tree

src/Appwrite/Platform/Tasks/Specs.php

Lines changed: 74 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828

2929
class Specs extends Action
3030
{
31+
public function __construct()
32+
{
33+
$this
34+
->desc('Generate Appwrite API specifications')
35+
->param('version', 'latest', new Text(16), 'Spec version', true)
36+
->param('mode', 'normal', new WhiteList(['normal', 'mocks']), 'Spec Mode', true)
37+
->callback($this->action(...));
38+
}
39+
3140
public static function getName(): string
3241
{
3342
return 'specs';
@@ -52,40 +61,43 @@ protected function getFormatInstance(string $format, array $arguments)
5261
};
5362
}
5463

55-
public function __construct()
64+
/**
65+
* Platforms
66+
*
67+
* @return array{client: string, server: string, console: string}
68+
*/
69+
protected function getPlatforms(): array
5670
{
57-
$this
58-
->desc('Generate Appwrite API specifications')
59-
->param('version', 'latest', new Text(16), 'Spec version', true)
60-
->param('mode', 'normal', new WhiteList(['normal', 'mocks']), 'Spec Mode', true)
61-
->callback($this->action(...));
62-
}
63-
64-
public function action(string $version, string $mode): void
65-
{
66-
$appRoutes = App::getRoutes();
67-
$response = $this->getResponse();
68-
$mocks = ($mode === 'mocks');
69-
70-
// Mock dependencies
71-
App::setResource('request', fn () => $this->getRequest());
72-
App::setResource('response', fn () => $response);
73-
App::setResource('dbForPlatform', fn () => new Database(new MySQL(''), new Cache(new None())));
74-
App::setResource('dbForProject', fn () => new Database(new MySQL(''), new Cache(new None())));
75-
76-
$platforms = [
71+
return [
7772
'client' => APP_PLATFORM_CLIENT,
7873
'server' => APP_PLATFORM_SERVER,
7974
'console' => APP_PLATFORM_CONSOLE,
8075
];
76+
}
8177

82-
$authCounts = [
78+
/**
79+
* Number of authentication methods supported by each platform
80+
* client: 1 (Session or JWT), server: 2 (Key and JWT), console: 1 (Admin)
81+
*
82+
* @return array{client: int, console: int, server: int}
83+
*/
84+
protected function getAuthCounts(): array
85+
{
86+
return [
8387
'client' => 1,
8488
'server' => 2,
8589
'console' => 1,
8690
];
91+
}
8792

88-
$keys = [
93+
/**
94+
* Keys for each platform
95+
*
96+
* @return array{client: array, server: array, console: array}
97+
*/
98+
protected function getKeys(): array
99+
{
100+
return [
89101
APP_PLATFORM_CLIENT => [
90102
'Project' => [
91103
'type' => 'apiKey',
@@ -189,6 +201,44 @@ public function action(string $version, string $mode): void
189201
],
190202
],
191203
];
204+
}
205+
206+
protected function getSDKPlatformsForRouteSecurity(array $routeSecurity): array
207+
{
208+
$sdkPlatforms = [];
209+
foreach ($routeSecurity as $value) {
210+
switch ($value) {
211+
case AuthType::SESSION:
212+
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
213+
break;
214+
case AuthType::JWT:
215+
case AuthType::KEY:
216+
$sdkPlatforms[] = APP_PLATFORM_SERVER;
217+
break;
218+
case AuthType::ADMIN:
219+
$sdkPlatforms[] = APP_PLATFORM_CONSOLE;
220+
break;
221+
}
222+
}
223+
return $sdkPlatforms;
224+
}
225+
226+
public function action(string $version, string $mode): void
227+
{
228+
$appRoutes = App::getRoutes();
229+
/** @var AppwriteResponse $response */
230+
$response = $this->getResponse();
231+
$mocks = ($mode === 'mocks');
232+
233+
// Mock dependencies
234+
App::setResource('request', fn () => $this->getRequest());
235+
App::setResource('response', fn () => $response);
236+
App::setResource('dbForPlatform', fn () => new Database(new MySQL(''), new Cache(new None())));
237+
App::setResource('dbForProject', fn () => new Database(new MySQL(''), new Cache(new None())));
238+
239+
$platforms = $this->getPlatforms();
240+
$authCounts = $this->getAuthCounts();
241+
$keys = $this->getKeys();
192242

193243
foreach ($platforms as $platform) {
194244
$routes = [];
@@ -215,22 +265,7 @@ public function action(string $version, string $mode): void
215265
}
216266

217267
$routeSecurity = $sdk->getAuth();
218-
$sdkPlatforms = [];
219-
220-
foreach ($routeSecurity as $value) {
221-
switch ($value) {
222-
case AuthType::SESSION:
223-
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
224-
break;
225-
case AuthType::JWT:
226-
case AuthType::KEY:
227-
$sdkPlatforms[] = APP_PLATFORM_SERVER;
228-
break;
229-
case AuthType::ADMIN:
230-
$sdkPlatforms[] = APP_PLATFORM_CONSOLE;
231-
break;
232-
}
233-
}
268+
$sdkPlatforms = $this->getSDKPlatformsForRouteSecurity($routeSecurity);
234269

235270
if (empty($routeSecurity)) {
236271
$sdkPlatforms[] = APP_PLATFORM_SERVER;

0 commit comments

Comments
 (0)