22
33namespace ApiSkeletons \Laravel \Doctrine \ApiKey \Console \Command ;
44
5+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Entity \Scope ;
6+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Exception \DuplicateName ;
7+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Exception \InvalidName ;
8+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Exception \ScopeHasApiKeys ;
9+ use ApiSkeletons \Laravel \Doctrine \ApiKey \Service \ApiKeyService ;
510use Illuminate \Console \Command ;
611
712final class DeleteScope extends Command
813{
14+ private ApiKeyService $ apiKeyService ;
15+
916 /**
1017 * The name and signature of the console command.
1118 *
1219 * @var string
1320 */
14- protected $ signature = 'apikey:scope:delete {scopeName } ' ;
21+ protected $ signature = 'apikey:scope:delete {name } ' ;
1522
1623 /**
1724 * The console command description.
1825 *
1926 * @var string
2027 */
21- protected $ description = 'Delete a scope for ApiKeys ' ;
28+ protected $ description = 'Delete an ApiKey Scope (Delete a scope, not a relationship) ' ;
2229
2330 /**
2431 * Create a new command instance.
2532 *
2633 * @return void
2734 */
28- public function __construct ()
35+ public function __construct (ApiKeyService $ apiKeyService )
2936 {
3037 parent ::__construct ();
38+
39+ $ this ->apiKeyService = $ apiKeyService ;
3140 }
3241
3342 /**
@@ -37,6 +46,32 @@ public function __construct()
3746 */
3847 public function handle ()
3948 {
40- // $drip->send(User::find($this->argument('user')));
49+ $ name = $ this ->argument ('name ' );
50+
51+ $ scopeRepository = $ this ->apiKeyService ->getEntityManager ()
52+ ->getRepository (Scope::class);
53+
54+ $ scope = $ scopeRepository ->findOneBy ([
55+ 'name ' => $ name ,
56+ ]);
57+
58+ if (! $ scope ) {
59+ $ this ->error ('Cannot find scope with name: ' . $ name );
60+
61+ return 1 ;
62+ }
63+
64+ try {
65+ $ scopeRepository ->delete ($ scope );
66+ $ this ->apiKeyService ->getEntityManager ()->flush ();
67+ } catch (ScopeHasApiKeys $ e ) {
68+ $ this ->error ($ e ->getMessage ());
69+
70+ return 1 ;
71+ }
72+
73+ $ this ->info ('Scope has been deleted ' );
74+
75+ return 0 ;
4176 }
4277}
0 commit comments