Skip to content

Commit a953723

Browse files
committed
Add docblocks and improve API key management
Added detailed PHP docblocks to controllers, models, and services for better code documentation and maintainability. Improved API key management in CpController, including corrected German notice text. Enhanced Twig templates with comments and clarified API key UI logic. No functional changes, only documentation and minor UI improvements.
1 parent bf99f88 commit a953723

5 files changed

Lines changed: 143 additions & 36 deletions

File tree

src/controllers/CpController.php

Lines changed: 58 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,30 @@
1414
use yii\base\NotSupportedException;
1515
use yii\web\BadRequestHttpException;
1616
use yii\web\MethodNotAllowedHttpException;
17+
use yii\web\Response;
1718
use yii\web\ServerErrorHttpException;
1819

20+
/**
21+
* Class CpController
22+
*
23+
* This controller handles the Control Panel (CP) actions for the plugin.
24+
* It provides methods for managing plugin settings, such as generating or deleting API keys.
25+
*
26+
* @package digitaldiff\diffbase\controllers
27+
*/
1928
class CpController extends Controller
2029
{
21-
2230
/**
23-
* @throws NotSupportedException
24-
* @throws InvalidConfigException
25-
* @throws MissingComponentException
26-
* @throws ServerErrorHttpException
27-
* @throws StaleResourceException
28-
* @throws BusyResourceException
29-
* @throws ErrorException
30-
* @throws Exception
31+
* Renders the main settings page for the plugin.
32+
*
33+
* - Initializes the plugin and its settings.
34+
* - Automatically generates an API key if none exists.
35+
*
36+
* @throws MissingComponentException If a required component is missing.
37+
* @throws Exception If the plugin is not initialized.
38+
* @return Response The rendered settings page.
3139
*/
32-
public function actionIndex(): \yii\web\Response
40+
public function actionIndex(): Response
3341
{
3442
$plugin = Plugin::getInstance();
3543
if (!$plugin) {
@@ -54,6 +62,11 @@ public function actionIndex(): \yii\web\Response
5462
]);
5563
}
5664

65+
/**
66+
* Retrieves the plugin settings.
67+
*
68+
* @return Settings The plugin settings model.
69+
*/
5770
private function getSettings(): Settings
5871
{
5972
/** @var Settings $settings */
@@ -62,18 +75,24 @@ private function getSettings(): Settings
6275
}
6376

6477
/**
65-
* @throws NotSupportedException
66-
* @throws InvalidConfigException
67-
* @throws MissingComponentException
68-
* @throws MethodNotAllowedHttpException
69-
* @throws ServerErrorHttpException
70-
* @throws BadRequestHttpException
71-
* @throws StaleResourceException
72-
* @throws BusyResourceException
73-
* @throws ErrorException
74-
* @throws Exception
78+
* Generates a new API key and saves it to the plugin settings.
79+
*
80+
* - Requires a POST request.
81+
* - Displays a success notice upon completion.
82+
*
83+
* @throws NotSupportedException If the operation is not supported.
84+
* @throws InvalidConfigException If the configuration is invalid.
85+
* @throws MissingComponentException If a required component is missing.
86+
* @throws MethodNotAllowedHttpException If the request method is not allowed.
87+
* @throws ServerErrorHttpException If a server error occurs.
88+
* @throws BadRequestHttpException If the request is invalid.
89+
* @throws StaleResourceException If the resource is stale.
90+
* @throws BusyResourceException If the resource is busy.
91+
* @throws ErrorException If an error occurs during execution.
92+
* @throws Exception If an unexpected error occurs.
93+
* @return Response A redirect response to the posted URL.
7594
*/
76-
public function actionGenerateApiKey(): \yii\web\Response
95+
public function actionGenerateApiKey(): Response
7796
{
7897
$this->requirePostRequest();
7998

@@ -88,25 +107,36 @@ public function actionGenerateApiKey(): \yii\web\Response
88107
}
89108

90109
/**
91-
* @throws MissingComponentException
92-
* @throws MethodNotAllowedHttpException
93-
* @throws BadRequestHttpException
110+
* Deletes the existing API key from the plugin settings.
111+
*
112+
* - Requires a POST request.
113+
* - Displays a success notice upon completion.
114+
*
115+
* @throws MissingComponentException If a required component is missing.
116+
* @throws MethodNotAllowedHttpException If the request method is not allowed.
117+
* @throws BadRequestHttpException If the request is invalid.
118+
* @return Response A redirect response to the posted URL.
94119
*/
95-
public function actionDeleteApiKey(): \yii\web\Response
120+
public function actionDeleteApiKey(): Response
96121
{
97122
$this->requirePostRequest();
98123

99124
// Settings ber Plugin speichern
100125
Craft::$app->plugins->savePluginSettings(Plugin::getInstance(), ['apiKey' => null]);
101-
Craft::$app->getSession()->setNotice('API-Key wurde gelscht.');
126+
Craft::$app->getSession()->setNotice('API-Key wurde gelöscht.');
102127

103128
return $this->redirectToPostedUrl();
104129
}
105130

106131
/**
107-
* @throws \Exception
132+
* Renders the settings page for the plugin.
133+
*
134+
* - Initializes the plugin and its settings.
135+
*
136+
* @throws \Exception If the plugin is not initialized.
137+
* @return Response The rendered settings page.
108138
*/
109-
public function actionSettings(): \yii\web\Response
139+
public function actionSettings(): Response
110140
{
111141
$plugin = Plugin::getInstance();
112142
if (!$plugin) {

src/models/Settings.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,45 @@
22
namespace digitaldiff\diffbase\models;
33

44
use craft\base\Model;
5+
use Random\RandomException;
56

7+
/**
8+
* Class Settings
9+
*
10+
* This model represents the settings for the "diff. base plugin".
11+
* It is used to store and validate the plugin's configuration, such as the API key.
12+
*
13+
* @package digitaldiff\diffbase\models
14+
*/
615
class Settings extends Model
716
{
17+
/**
18+
* @var string|null $apiKey The API key used for authentication. Defaults to null.
19+
*/
820
public ?string $apiKey = null;
921

22+
/**
23+
* Returns the validation rules for the model's attributes.
24+
*
25+
* @return array The validation rules for the `apiKey` attribute.
26+
*/
1027
public function rules(): array
1128
{
1229
return [
13-
['apiKey', 'string'],
14-
['apiKey', 'default', 'value' => null],
30+
['apiKey', 'string'], // Ensures the API key is a string.
31+
['apiKey', 'default', 'value' => null], // Sets the default value of the API key to null.
1532
];
1633
}
1734

35+
/**
36+
* Generates a new random API key.
37+
*
38+
* This method uses `random_bytes` to generate a secure 32-byte random string,
39+
* which is then converted to a hexadecimal representation.
40+
*
41+
* @throws RandomException If an appropriate source of randomness cannot be found.
42+
* @return string The generated API key as a 64-character hexadecimal string.
43+
*/
1844
public function generateApiKey(): string
1945
{
2046
return bin2hex(random_bytes(32));

src/services/ApiService.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,35 @@
44
use craft\base\Component;
55
use digitaldiff\diffbase\Plugin;
66

7+
/**
8+
* Class ApiService
9+
*
10+
* This service handles API-related business logic for the plugin.
11+
* It provides methods for validating API keys and retrieving system data.
12+
*
13+
* @package digitaldiff\diffbase\services
14+
*/
715
class ApiService extends Component
816
{
17+
/**
18+
* Validates the provided API key against the stored plugin settings.
19+
*
20+
* @param string $key The API key to validate.
21+
* @return bool True if the API key matches the stored key, false otherwise.
22+
*/
923
public function validateApiKey(string $key): bool
1024
{
1125
$settings = Plugin::getInstance()->getSettings();
1226
return $settings->apiKey === $key;
1327
}
1428

29+
/**
30+
* Retrieves system data for the API, including plugin and API status.
31+
*
32+
* @return array An associative array containing:
33+
* - `api_enabled` (bool): Whether the API is enabled (based on the presence of an API key).
34+
* - `version` (string): The current version of the plugin.
35+
*/
1536
public function getSystemData(): array
1637
{
1738
$settings = Plugin::getInstance()->getSettings();

src/templates/index.twig

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
{#
2+
This Twig template extends the Craft CMS Control Panel layout and provides the interface for the "diff. base plugin" settings page.
3+
4+
Template Details:
5+
- Displays the current API key if available.
6+
- Allows users to copy the API key, generate a new one, or access the API endpoint.
7+
- Includes a JavaScript function for copying the API key to the clipboard.
8+
9+
Variables:
10+
- `settings.apiKey`: The current API key stored in the plugin settings.
11+
12+
Blocks:
13+
- `content`: Contains the main content of the settings page, including the API key management interface.
14+
15+
Notes:
16+
- Ensure the `settings.apiKey` variable is properly passed to the template.
17+
- The `copyApiKey` JavaScript function uses Craft's `Craft.cp.displayNotice` for user feedback.
18+
#}
119
{% extends "_layouts/cp" %}
220
{% set title = "diff. base plugin" %}
321
{% set crumbs = [
@@ -32,34 +50,35 @@
3250
{% if settings.apiKey %}
3351
Neuen API-Key generieren
3452
{% else %}
35-
API-Key generieren
3653
{% endif %}
54+
API-Key generieren
3755
</button>
3856
</form>
3957

4058
<a href="{{ siteUrl }}api/info?key={{ settings.apiKey }}" class="view-btn btn" target="_blank">
4159
{{ svg('@appicons/external-link.svg') }}
4260
API-Endpunkt aufrufen
4361
</a>
44-
45-
4662
</div>
4763
</div>
4864
</div>
49-
5065
{% endif %}
5166

5267
<hr>
5368
</div>
5469
</div>
5570

5671
<script>
72+
/**
73+
* Copies the API key to the clipboard and displays a success notice.
74+
*/
5775
function copyApiKey() {
5876
const field = document.getElementById('apiKeyField');
5977
field.select();
6078
document.execCommand('copy');
6179
80+
// Display a success message using Craft's control panel notice system
6281
Craft.cp.displayNotice('API-Key in Zwischenablage kopiert');
6382
}
6483
</script>
65-
{% endblock %}
84+
{% endblock %}

src/templates/settings.twig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
{% redirect "diffbase" %}
1+
{#
2+
This Twig template redirects the user to the main "diffbase" section of the Craft CMS Control Panel.
3+
4+
Usage:
5+
- This template is typically used as a placeholder or entry point for the plugin's settings page.
6+
- When accessed, it automatically redirects the user to the "diffbase" route.
7+
8+
Notes:
9+
- Ensure that the "diffbase" route is properly defined in your plugin's `src/controllers/CpController.php`.
10+
- This template does not render any content; it only performs a redirect.
11+
#}
12+
{% redirect "diffbase" %}

0 commit comments

Comments
 (0)