Skip to content

Commit 2dc1d82

Browse files
authored
Merge pull request #5 from TomHAnderson/feature/abstract-command
Moved all commands to abstract
2 parents bbe2708 + 07506d3 commit 2dc1d82

File tree

13 files changed

+216
-254
lines changed

13 files changed

+216
-254
lines changed

src/Console/Command/ActivateApiKey.php

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@
55
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
66

77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
8-
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
9-
use Illuminate\Console\Command;
10-
11-
use function implode;
128

139
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
1410
final class ActivateApiKey extends Command
1511
{
16-
private ApiKeyService $apiKeyService;
17-
1812
/**
1913
* The name and signature of the console command.
2014
*/
@@ -25,18 +19,6 @@ final class ActivateApiKey extends Command
2519
*/
2620
protected $description = 'Activate an ApiKey';
2721

28-
/**
29-
* Create a new command instance.
30-
*
31-
* @return void
32-
*/
33-
public function __construct(ApiKeyService $apiKeyService)
34-
{
35-
parent::__construct();
36-
37-
$this->apiKeyService = $apiKeyService;
38-
}
39-
4022
/**
4123
* Execute the console command.
4224
*/
@@ -50,30 +32,15 @@ public function handle(): mixed
5032
$apiKey = $apiKeyRepository->findOneBy(['name' => $name]);
5133

5234
if (! $apiKey) {
53-
$this->error('Invalid apiKey name');
35+
$this->error('Invalid apikey name');
5436

5537
return 1;
5638
}
5739

5840
$apiKeyRepository->updateActive($apiKey, true);
5941
$this->apiKeyService->getEntityManager()->flush();
6042

61-
$scopeNames = [];
62-
foreach ($apiKey->getScopes() as $s) {
63-
$scopeNames[] = $s->getName();
64-
}
65-
66-
$headers = ['name', 'key', 'status', 'scopes'];
67-
$rows = [
68-
[
69-
$apiKey->getName(),
70-
$apiKey->getApiKey(),
71-
$apiKey->getIsActive() ? 'active' : 'deactivated',
72-
implode(',', $scopeNames),
73-
],
74-
];
75-
76-
$this->table($headers, $rows);
43+
$this->printApiKeys([$apiKey]);
7744

7845
return 0;
7946
}

src/Console/Command/AddScope.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,10 @@
88
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
99
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateScopeForApiKey;
1010
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
11-
use Illuminate\Console\Command;
12-
13-
use function implode;
1411

1512
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
1613
final class AddScope extends Command
1714
{
18-
private ApiKeyService $apiKeyService;
19-
2015
/**
2116
* The name and signature of the console command.
2217
*/
@@ -27,18 +22,6 @@ final class AddScope extends Command
2722
*/
2823
protected $description = 'Add a Scope to an ApiKey';
2924

30-
/**
31-
* Create a new command instance.
32-
*
33-
* @return void
34-
*/
35-
public function __construct(ApiKeyService $apiKeyService)
36-
{
37-
parent::__construct();
38-
39-
$this->apiKeyService = $apiKeyService;
40-
}
41-
4225
/**
4326
* Execute the console command.
4427
*/
@@ -54,7 +37,7 @@ public function handle(): mixed
5437

5538
$apiKey = $apiKeyRepository->findOneBy(['name' => $apiKeyName]);
5639
if (! $apiKey) {
57-
$this->error('Cannot find ApiKey with name: ' . $apiKeyName);
40+
$this->error('Cannot find apikey with name: ' . $apiKeyName);
5841

5942
return 1;
6043
}
@@ -75,22 +58,7 @@ public function handle(): mixed
7558
return 1;
7659
}
7760

78-
$scopeNames = [];
79-
foreach ($apiKey->getScopes() as $s) {
80-
$scopeNames[] = $s->getName();
81-
}
82-
83-
$headers = ['name', 'key', 'status', 'scopes'];
84-
$rows = [
85-
[
86-
$apiKey->getName(),
87-
$apiKey->getApiKey(),
88-
$apiKey->getIsActive() ? 'active' : 'deactivated',
89-
implode(',', $scopeNames),
90-
],
91-
];
92-
93-
$this->table($headers, $rows);
61+
$this->printApiKeys([$apiKey]);
9462

9563
return 0;
9664
}

src/Console/Command/Command.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace ApiSkeletons\Laravel\Doctrine\ApiKey\Console\Command;
4+
5+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
6+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
7+
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
8+
use Illuminate\Console\Command as IlluminateCommand;
9+
10+
abstract class Command extends IlluminateCommand
11+
{
12+
protected ApiKeyService $apiKeyService;
13+
14+
/**
15+
* Create a new command instance.
16+
*
17+
* @return void
18+
*/
19+
public function __construct(ApiKeyService $apiKeyService)
20+
{
21+
parent::__construct();
22+
23+
$this->apiKeyService = $apiKeyService;
24+
}
25+
26+
/**
27+
* @param ApiKey[] $apiKeys
28+
*/
29+
protected function printApiKeys(array $apiKeys): void
30+
{
31+
$headers = ['name', 'api_key', 'status', 'scopes'];
32+
33+
$rows = [];
34+
foreach($apiKeys as $apiKey) {
35+
$scopeNames = [];
36+
foreach ($apiKey->getScopes() as $s) {
37+
$scopeNames[] = $s->getName();
38+
}
39+
40+
$rows[] = [
41+
$apiKey->getName(),
42+
$apiKey->getApiKey(),
43+
$apiKey->getIsActive() ? 'active' : 'deactivated',
44+
implode(',', $scopeNames),
45+
];
46+
}
47+
48+
$this->table($headers, $rows);
49+
}
50+
51+
/**
52+
* @param Scope[] $scopes
53+
*/
54+
protected function printScopes(array $scopes): void
55+
{
56+
$headers = ['name', 'apikey count'];
57+
58+
$rows = [];
59+
foreach ($scopes as $scope) {
60+
$rows[] = [
61+
$scope->getName(),
62+
count($scope->getApiKeys()),
63+
];
64+
}
65+
66+
$this->table($headers, $rows);
67+
}
68+
}

src/Console/Command/DeactivateApiKey.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66

77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
88
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
9-
use Illuminate\Console\Command;
109

1110
use function implode;
1211

1312
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
1413
final class DeactivateApiKey extends Command
1514
{
16-
private ApiKeyService $apiKeyService;
17-
1815
/**
1916
* The name and signature of the console command.
2017
*/
@@ -25,18 +22,6 @@ final class DeactivateApiKey extends Command
2522
*/
2623
protected $description = 'Deactivate an ApiKey';
2724

28-
/**
29-
* Create a new command instance.
30-
*
31-
* @return void
32-
*/
33-
public function __construct(ApiKeyService $apiKeyService)
34-
{
35-
parent::__construct();
36-
37-
$this->apiKeyService = $apiKeyService;
38-
}
39-
4025
/**
4126
* Execute the console command.
4227
*/
@@ -50,30 +35,15 @@ public function handle(): mixed
5035
$apiKey = $apiKeyRepository->findOneBy(['name' => $name]);
5136

5237
if (! $apiKey) {
53-
$this->error('Invalid apiKey name');
38+
$this->error('Invalid apikey name');
5439

5540
return 1;
5641
}
5742

5843
$apiKeyRepository->updateActive($apiKey, false);
5944
$this->apiKeyService->getEntityManager()->flush();
6045

61-
$scopeNames = [];
62-
foreach ($apiKey->getScopes() as $s) {
63-
$scopeNames[] = $s->getName();
64-
}
65-
66-
$headers = ['name', 'key', 'status', 'scopes'];
67-
$rows = [
68-
[
69-
$apiKey->getName(),
70-
$apiKey->getApiKey(),
71-
$apiKey->getIsActive() ? 'active' : 'deactivated',
72-
implode(',', $scopeNames),
73-
],
74-
];
75-
76-
$this->table($headers, $rows);
46+
$this->printApiKeys([$apiKey]);
7747

7848
return 0;
7949
}

src/Console/Command/DeleteScope.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\Scope;
88
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\ScopeHasApiKeys;
99
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
10-
use Illuminate\Console\Command;
1110

1211
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
1312
final class DeleteScope extends Command
1413
{
15-
private ApiKeyService $apiKeyService;
16-
1714
/**
1815
* The name and signature of the console command.
1916
*/
@@ -24,18 +21,6 @@ final class DeleteScope extends Command
2421
*/
2522
protected $description = 'Delete an ApiKey Scope (Delete a scope, not a relationship)';
2623

27-
/**
28-
* Create a new command instance.
29-
*
30-
* @return void
31-
*/
32-
public function __construct(ApiKeyService $apiKeyService)
33-
{
34-
parent::__construct();
35-
36-
$this->apiKeyService = $apiKeyService;
37-
}
38-
3924
/**
4025
* Execute the console command.
4126
*/

src/Console/Command/GenerateApiKey.php

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
use ApiSkeletons\Laravel\Doctrine\ApiKey\Entity\ApiKey;
88
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\DuplicateName;
99
use ApiSkeletons\Laravel\Doctrine\ApiKey\Exception\InvalidName;
10-
use ApiSkeletons\Laravel\Doctrine\ApiKey\Service\ApiKeyService;
11-
use Illuminate\Console\Command;
1210

1311
use function implode;
1412

1513
// phpcs:disable SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingAnyTypeHint
1614
final class GenerateApiKey extends Command
1715
{
18-
private ApiKeyService $apiKeyService;
19-
2016
/**
2117
* The name and signature of the console command.
2218
*/
@@ -25,19 +21,7 @@ final class GenerateApiKey extends Command
2521
/**
2622
* The console command description.
2723
*/
28-
protected $description = 'Create a new ApiKey';
29-
30-
/**
31-
* Create a new command instance.
32-
*
33-
* @return void
34-
*/
35-
public function __construct(ApiKeyService $apiKeyService)
36-
{
37-
parent::__construct();
38-
39-
$this->apiKeyService = $apiKeyService;
40-
}
24+
protected $description = 'Create a new apikey';
4125

4226
/**
4327
* Execute the console command.
@@ -63,22 +47,7 @@ public function handle(): mixed
6347

6448
$this->apiKeyService->getEntityManager()->flush();
6549

66-
$scopeNames = [];
67-
foreach ($apiKey->getScopes() as $s) {
68-
$scopeNames[] = $s->getName();
69-
}
70-
71-
$headers = ['name', 'key', 'status', 'scopes'];
72-
$rows = [
73-
[
74-
$apiKey->getName(),
75-
$apiKey->getApiKey(),
76-
$apiKey->getIsActive() ? 'active' : 'deactivated',
77-
implode(',', $scopeNames),
78-
],
79-
];
80-
81-
$this->table($headers, $rows);
50+
$this->printApiKeys([$apiKey]);
8251

8352
return 0;
8453
}

0 commit comments

Comments
 (0)