33namespace AppBundle \Issues ;
44
55use AppBundle \Event \GitHubEvent ;
6- use AppBundle \Exception \GitHubAccessDeniedException ;
7- use AppBundle \Exception \GitHubBadRequestException ;
8- use AppBundle \Exception \GitHubExceptionInterface ;
9- use AppBundle \Exception \GitHubInvalidConfigurationException ;
10- use AppBundle \Exception \GitHubPreconditionFailedException ;
11- use AppBundle \Exception \GitHubRuntimeException ;
126use AppBundle \Repository \Provider \RepositoryProviderInterface ;
13- use Github \Api \Issue \Labels ;
147use Symfony \Component \DependencyInjection \ContainerInterface ;
158use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
169use Symfony \Component \HttpFoundation \Request ;
10+ use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
11+ use Symfony \Component \HttpKernel \Exception \PreconditionFailedHttpException ;
12+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
1713
1814/**
1915 * Handles GitHub webhook requests
@@ -36,33 +32,31 @@ public function __construct(EventDispatcherInterface $dispatcher, RepositoryProv
3632 /**
3733 * @param Request $request
3834 * @return array The response data
39- *
40- * @throws GitHubExceptionInterface When the request or the configuration are invalid
4135 */
4236 public function handle (Request $ request )
4337 {
4438 $ data = json_decode ($ request ->getContent (), true );
4539 if (null === $ data ) {
46- throw new GitHubBadRequestException ('Invalid JSON body! ' );
40+ throw new BadRequestHttpException ('Invalid JSON body! ' );
4741 }
4842
4943 $ repositoryFullName = isset ($ data ['repository ' ]['full_name ' ]) ? $ data ['repository ' ]['full_name ' ] : null ;
5044 if (empty ($ repositoryFullName )) {
51- throw new GitHubBadRequestException ('No repository name! ' );
45+ throw new BadRequestHttpException ('No repository name! ' );
5246 }
5347
5448 $ repository = $ this ->repositoryProvider ->getRepository ($ repositoryFullName );
5549
5650 if (!$ repository ) {
57- throw new GitHubPreconditionFailedException (sprintf ('Unsupported repository "%s". ' , $ repositoryFullName ));
51+ throw new PreconditionFailedHttpException (sprintf ('Unsupported repository "%s". ' , $ repositoryFullName ));
5852 }
5953
6054 if ($ repository ->getSecret ()) {
6155 if (!$ request ->headers ->has ('X-Hub-Signature ' )) {
62- throw new GitHubAccessDeniedException ('The request is not secured. ' );
56+ throw new AccessDeniedException ('The request is not secured. ' );
6357 }
6458 if (!$ this ->authenticate ($ request ->headers ->get ('X-Hub-Signature ' ), $ repository ->getSecret (), $ request ->getContent ())) {
65- throw new GitHubAccessDeniedException ('Invalid signature. ' );
59+ throw new AccessDeniedException ('Invalid signature. ' );
6660 }
6761 }
6862
@@ -76,22 +70,18 @@ public function handle(Request $request)
7670 try {
7771 $ this ->dispatcher ->dispatch ('github. ' .$ eventName , $ event );
7872 } catch (\Exception $ e ) {
79- throw new GitHubRuntimeException (sprintf ('Failed dispatching "%s" event for "%s" repository. ' , $ eventName , $ repository ), 0 , $ e );
73+ throw new \ RuntimeException (sprintf ('Failed dispatching "%s" event for "%s" repository. ' , $ eventName , $ repository ), 0 , $ e );
8074 }
8175
8276 $ responseData = $ event ->getResponseData ();
8377
84- if (empty ($ responseData )) {
85- throw new GitHubPreconditionFailedException (sprintf ('Unsupported event "%s" ' , $ eventName ));
86- }
87-
8878 return $ responseData ;
8979 }
9080
9181 private function authenticate ($ hash , $ key , $ data )
9282 {
9383 if (!extension_loaded ('hash ' )) {
94- throw new GitHubInvalidConfigurationException ('"hash" extension is needed to check request signature. ' );
84+ throw new \ RuntimeException ('"hash" extension is needed to check request signature. ' );
9585 }
9686
9787 return $ hash !== 'sha1= ' .hash_hmac ('sha1 ' , $ data , $ key );
0 commit comments