wip
This commit is contained in:
		
							
								
								
									
										49
									
								
								app/Domain/Slack/Handlers/GiveKeysTo.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								app/Domain/Slack/Handlers/GiveKeysTo.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| <?php | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace Toby\Domain\Slack\Handlers; | ||||
|  | ||||
| use Illuminate\Support\Str; | ||||
| use Spatie\SlashCommand\Request; | ||||
| use Spatie\SlashCommand\Response; | ||||
| use Spatie\SlashCommand\Handlers\SignatureHandler; | ||||
| use Toby\Eloquent\Models\Key; | ||||
| use Toby\Eloquent\Models\User; | ||||
|  | ||||
| class GiveKeysTo extends SignatureHandler | ||||
| { | ||||
|     protected $signature = "toby klucze:dla {użytkownik}"; | ||||
|  | ||||
|     protected $description = "Daj klucze wskazanemu użytkownikowi"; | ||||
|  | ||||
|     public function handle(Request $request): Response | ||||
|     { | ||||
|         $to = $this->getArgument('użytkownik'); | ||||
|  | ||||
|         $id = Str::between($to, "@", "|"); | ||||
|  | ||||
|         $authUser = $this->findUserBySlackId($request->userId); | ||||
|         $user = $this->findUserBySlackId($id); | ||||
|  | ||||
|         /** @var Key $key */ | ||||
|         $key = $authUser->keys()->first(); | ||||
|  | ||||
|         $key->user()->associate($user); | ||||
|  | ||||
|         $key->save(); | ||||
|  | ||||
|         return $this->respondToSlack("<@{$authUser->profile->slack_id}> daje klucz nr {$key->id} użytkownikowi <@{$user->profile->slack_id}>") | ||||
|             ->displayResponseToEveryoneOnChannel(); | ||||
|     } | ||||
|  | ||||
|     protected function findUserBySlackId(string $slackId): User | ||||
|     { | ||||
|         /** @var User $user */ | ||||
|         $user = User::query() | ||||
|             ->whereRelation("profile", "slack_id", $slackId) | ||||
|             ->first(); | ||||
|  | ||||
|         return $user; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user