* wip * wip * wip * wip * fix * wip * wip * fix * fix * cs fix * #116 - fix * #116 - changed home-office icon * Apply suggestions from code review Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com> * #116 - cr fix * #116 - cs fix * #116 - cs fix * Apply suggestions from code review Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com> * #5 - bump codestyle Co-authored-by: EwelinaLasowy <ewelina.lasowy@blumilk.pl> Co-authored-by: Krzysztof Rewak <krzysztof.rewak@gmail.com> Co-authored-by: Ewelina Lasowy <56546832+EwelinaLasowy@users.noreply.github.com>
		
			
				
	
	
		
			57 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
declare(strict_types=1);
 | 
						|
 | 
						|
namespace Toby\Infrastructure\Slack;
 | 
						|
 | 
						|
use Exception;
 | 
						|
use Illuminate\Http\Request as IlluminateRequest;
 | 
						|
use Illuminate\Http\Response as IlluminateResponse;
 | 
						|
use Illuminate\Support\Collection;
 | 
						|
use Illuminate\Validation\ValidationException;
 | 
						|
use Spatie\SlashCommand\Attachment;
 | 
						|
use Spatie\SlashCommand\Controller as SlackController;
 | 
						|
use Spatie\SlashCommand\Exceptions\InvalidRequest;
 | 
						|
use Spatie\SlashCommand\Exceptions\RequestCouldNotBeHandled;
 | 
						|
use Spatie\SlashCommand\Exceptions\SlackSlashCommandException;
 | 
						|
use Spatie\SlashCommand\Response;
 | 
						|
 | 
						|
class Controller extends SlackController
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * @throws InvalidRequest|RequestCouldNotBeHandled
 | 
						|
     */
 | 
						|
    public function getResponse(IlluminateRequest $request): IlluminateResponse
 | 
						|
    {
 | 
						|
        $this->verifyWithSigning($request);
 | 
						|
 | 
						|
        $handler = $this->determineHandler();
 | 
						|
 | 
						|
        try {
 | 
						|
            $response = $handler->handle($this->request);
 | 
						|
        } catch (SlackSlashCommandException $exception) {
 | 
						|
            $response = $exception->getResponse($this->request);
 | 
						|
        } catch (ValidationException $exception) {
 | 
						|
            $response = $this->prepareValidationResponse($exception);
 | 
						|
        } catch (Exception $exception) {
 | 
						|
            $response = $this->convertToResponse($exception);
 | 
						|
        }
 | 
						|
 | 
						|
        return $response->getIlluminateResponse();
 | 
						|
    }
 | 
						|
 | 
						|
    protected function prepareValidationResponse(ValidationException $exception): Response
 | 
						|
    {
 | 
						|
        $errors = (new Collection($exception->errors()))
 | 
						|
            ->map(
 | 
						|
                fn(array $message): Attachment => Attachment::create()
 | 
						|
                    ->setColor("danger")
 | 
						|
                    ->setText($message[0]),
 | 
						|
            );
 | 
						|
 | 
						|
        return Response::create($this->request)
 | 
						|
            ->withText(":x: Polecenie `/{$this->request->command} {$this->request->text}` jest niepoprawne:")
 | 
						|
            ->withAttachments($errors->all());
 | 
						|
    }
 | 
						|
}
 |