55use BenTools \WebPushBundle \Model \Subscription \UserSubscriptionManagerRegistry ;
66use Symfony \Component \HttpFoundation \Request ;
77use Symfony \Component \HttpFoundation \Response ;
8- use Symfony \Component \HttpKernel \Exception \AccessDeniedHttpException ;
98use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
109use Symfony \Component \HttpKernel \Exception \MethodNotAllowedHttpException ;
1110use Symfony \Component \Security \Core \User \UserInterface ;
@@ -16,13 +15,17 @@ final class RegisterSubscriptionAction
1615 * @var UserSubscriptionManagerRegistry
1716 */
1817 private $ registry ;
18+ private $ deleteAnonymousSubscriptionWithTheSameEndpoint ;
1919
2020 /**
2121 * RegisterSubscriptionAction constructor.
22+ * @param UserSubscriptionManagerRegistry $registry
23+ * @param $deleteAnonymousSubscriptionWithTheSameEndpoint
2224 */
23- public function __construct (UserSubscriptionManagerRegistry $ registry )
25+ public function __construct (UserSubscriptionManagerRegistry $ registry, $ deleteAnonymousSubscriptionWithTheSameEndpoint )
2426 {
2527 $ this ->registry = $ registry ;
28+ $ this ->deleteAnonymousSubscriptionWithTheSameEndpoint = $ deleteAnonymousSubscriptionWithTheSameEndpoint ;
2629 }
2730
2831 /**
@@ -31,7 +34,7 @@ public function __construct(UserSubscriptionManagerRegistry $registry)
3134 * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException
3235 * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException
3336 */
34- private function subscribe (UserInterface $ user , string $ subscriptionHash , array $ subscription , array $ options = [])
37+ private function subscribe (? UserInterface $ user , string $ subscriptionHash , array $ subscription , array $ options = [])
3538 {
3639 $ manager = $ this ->registry ->getManager ($ user );
3740 $ userSubscription = $ manager ->getUserSubscription ($ user , $ subscriptionHash )
@@ -43,7 +46,7 @@ private function subscribe(UserInterface $user, string $subscriptionHash, array
4346 * @throws BadRequestHttpException
4447 * @throws \RuntimeException
4548 */
46- private function unsubscribe (UserInterface $ user , string $ subscriptionHash )
49+ private function unsubscribe (? UserInterface $ user , string $ subscriptionHash )
4750 {
4851 $ manager = $ this ->registry ->getManager ($ user );
4952 $ subscription = $ manager ->getUserSubscription ($ user , $ subscriptionHash );
@@ -53,12 +56,8 @@ private function unsubscribe(UserInterface $user, string $subscriptionHash)
5356 $ manager ->delete ($ subscription );
5457 }
5558
56- public function __invoke (Request $ request , UserInterface $ user = null ): Response
59+ public function __invoke (Request $ request , ? UserInterface $ user = null ): Response
5760 {
58- if (null === $ user ) {
59- throw new AccessDeniedHttpException ('Not authenticated. ' );
60- }
61-
6261 if (!in_array ($ request ->getMethod (), ['POST ' , 'DELETE ' ])) {
6362 throw new MethodNotAllowedHttpException (['POST ' , 'DELETE ' ]);
6463 }
@@ -75,6 +74,14 @@ public function __invoke(Request $request, UserInterface $user = null): Response
7574 throw new BadRequestHttpException ('Invalid subscription object. ' );
7675 }
7776
77+ if (Request::METHOD_POST === $ request ->getMethod () && null !== $ user && true === $ this ->deleteAnonymousSubscriptionWithTheSameEndpoint ) {
78+ $ manager = $ this ->registry ->getManager (null );
79+ $ anonymousSubscriptionHash = $ manager ->hash ($ subscription ['endpoint ' ], null );
80+ $ anonymousSubscriptions = $ manager ->findByHash ($ anonymousSubscriptionHash );
81+ foreach ($ anonymousSubscriptions as $ anonymousSubscription ) {
82+ $ manager ->delete ($ anonymousSubscription );
83+ }
84+ }
7885 $ manager = $ this ->registry ->getManager ($ user );
7986 $ subscriptionHash = $ manager ->hash ($ subscription ['endpoint ' ], $ user );
8087
0 commit comments