@@ -20,7 +20,7 @@ discard block |
||
| 20 | 20 | * @param bool $all |
| 21 | 21 | * @return array|string |
| 22 | 22 | */ |
| 23 | - public static function getByPath(string $path, bool $all = false): array|string |
|
| 23 | + public static function getByPath(string $path, bool $all = false): array | string |
|
| 24 | 24 | { |
| 25 | 25 | $ext = pathinfo($path, PATHINFO_EXTENSION); |
| 26 | 26 | return self::getByExtension($ext, $all); |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * @param bool $all |
| 32 | 32 | * @return array|string |
| 33 | 33 | */ |
| 34 | - public static function getByExtension(string $ext, bool $all = false): array|string |
|
| 34 | + public static function getByExtension(string $ext, bool $all = false): array | string |
|
| 35 | 35 | { |
| 36 | 36 | $mimeType = MimeTypes::getDefault(); |
| 37 | 37 | $types = $mimeType->getMimeTypes($ext); |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @param bool $all |
| 53 | 53 | * @return array|string |
| 54 | 54 | */ |
| 55 | - public static function getExtensionByMimeType(string $mime, bool $all = false): array|string |
|
| 55 | + public static function getExtensionByMimeType(string $mime, bool $all = false): array | string |
|
| 56 | 56 | { |
| 57 | 57 | $mimeType = MimeTypes::getDefault(); |
| 58 | 58 | $extensions = $mimeType->getExtensions($mime); |
@@ -43,53 +43,53 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | class RegistrationController extends AbstractController { |
| 45 | 45 | |
| 46 | - private SettingsManagerInterface $settingsManager; |
|
| 47 | - private ScopeContextInterface $scopeContext; |
|
| 48 | - private EmailVerifier $emailVerifier; |
|
| 49 | - private UserManagerInterface $userManager; |
|
| 50 | - private EntityManagerInterface $entityManager; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param SettingsManagerInterface $settingsManager |
|
| 54 | - * @param ScopeContextInterface $scopeContext |
|
| 55 | - * @param EmailVerifier $emailVerifier |
|
| 56 | - * @param UserManagerInterface $userManager |
|
| 57 | - * @param EntityManagerInterface $entityManager |
|
| 58 | - */ |
|
| 59 | - public function __construct( |
|
| 60 | - SettingsManagerInterface $settingsManager, |
|
| 61 | - ScopeContextInterface $scopeContext, |
|
| 62 | - EmailVerifier $emailVerifier, |
|
| 63 | - UserManagerInterface $userManager, |
|
| 64 | - EntityManagerInterface $entityManager |
|
| 65 | - ) { |
|
| 46 | + private SettingsManagerInterface $settingsManager; |
|
| 47 | + private ScopeContextInterface $scopeContext; |
|
| 48 | + private EmailVerifier $emailVerifier; |
|
| 49 | + private UserManagerInterface $userManager; |
|
| 50 | + private EntityManagerInterface $entityManager; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param SettingsManagerInterface $settingsManager |
|
| 54 | + * @param ScopeContextInterface $scopeContext |
|
| 55 | + * @param EmailVerifier $emailVerifier |
|
| 56 | + * @param UserManagerInterface $userManager |
|
| 57 | + * @param EntityManagerInterface $entityManager |
|
| 58 | + */ |
|
| 59 | + public function __construct( |
|
| 60 | + SettingsManagerInterface $settingsManager, |
|
| 61 | + ScopeContextInterface $scopeContext, |
|
| 62 | + EmailVerifier $emailVerifier, |
|
| 63 | + UserManagerInterface $userManager, |
|
| 64 | + EntityManagerInterface $entityManager |
|
| 65 | + ) { |
|
| 66 | 66 | $this->settingsManager = $settingsManager; |
| 67 | 67 | $this->scopeContext = $scopeContext; |
| 68 | 68 | $this->emailVerifier = $emailVerifier; |
| 69 | 69 | $this->userManager = $userManager; |
| 70 | 70 | $this->entityManager = $entityManager; |
| 71 | - } |
|
| 72 | - |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @FOSRoute( |
|
| 76 | - * "/api/{version}/users/register/", |
|
| 77 | - * methods={"POST"}, |
|
| 78 | - * options={"expose"=true}, |
|
| 79 | - * defaults={"version"="v2"}, |
|
| 80 | - * name="swp_api_core_register_user" |
|
| 81 | - * ) |
|
| 82 | - */ |
|
| 83 | - public function registerAction( |
|
| 84 | - Request $request, |
|
| 85 | - UserPasswordHasherInterface $userPasswordEncoder, |
|
| 86 | - UserManagerInterface $userManager, |
|
| 87 | - MailerInterface $mailer |
|
| 88 | - ) { |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @FOSRoute( |
|
| 76 | + * "/api/{version}/users/register/", |
|
| 77 | + * methods={"POST"}, |
|
| 78 | + * options={"expose"=true}, |
|
| 79 | + * defaults={"version"="v2"}, |
|
| 80 | + * name="swp_api_core_register_user" |
|
| 81 | + * ) |
|
| 82 | + */ |
|
| 83 | + public function registerAction( |
|
| 84 | + Request $request, |
|
| 85 | + UserPasswordHasherInterface $userPasswordEncoder, |
|
| 86 | + UserManagerInterface $userManager, |
|
| 87 | + MailerInterface $mailer |
|
| 88 | + ) { |
|
| 89 | 89 | try { |
| 90 | - $this->ensureThatRegistrationIsEnabled(); |
|
| 90 | + $this->ensureThatRegistrationIsEnabled(); |
|
| 91 | 91 | } catch (NotFoundHttpException $e) { |
| 92 | - return new SingleResourceResponse(null, new ResponseContext(404)); |
|
| 92 | + return new SingleResourceResponse(null, new ResponseContext(404)); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $user = $userManager->createUser(); |
@@ -98,69 +98,69 @@ discard block |
||
| 98 | 98 | $form->handleRequest($request); |
| 99 | 99 | |
| 100 | 100 | if ($form->isSubmitted() && $form->isValid()) { |
| 101 | - $user->addRole('ROLE_USER'); |
|
| 102 | - // encode the plain password |
|
| 103 | - $user->setPassword( |
|
| 104 | - $userPasswordEncoder->hashPassword( |
|
| 105 | - $user, |
|
| 106 | - $form->get('plainPassword')->getData() |
|
| 107 | - ) |
|
| 108 | - ); |
|
| 109 | - |
|
| 110 | - $entityManager = $this->entityManager; |
|
| 111 | - $entityManager->persist($user); |
|
| 112 | - $entityManager->flush(); |
|
| 113 | - |
|
| 114 | - $signatureComponents = $this->emailVerifier->getSignatureComponents('swp_user_verify_email', $user); |
|
| 115 | - $url = $signatureComponents->getSignedUrl(); |
|
| 116 | - |
|
| 117 | - $mailer->sendConfirmationEmail($user, $url); |
|
| 118 | - |
|
| 119 | - return new JsonResponse([ |
|
| 120 | - 'message' => sprintf( |
|
| 121 | - 'The user has been created successfully. |
|
| 101 | + $user->addRole('ROLE_USER'); |
|
| 102 | + // encode the plain password |
|
| 103 | + $user->setPassword( |
|
| 104 | + $userPasswordEncoder->hashPassword( |
|
| 105 | + $user, |
|
| 106 | + $form->get('plainPassword')->getData() |
|
| 107 | + ) |
|
| 108 | + ); |
|
| 109 | + |
|
| 110 | + $entityManager = $this->entityManager; |
|
| 111 | + $entityManager->persist($user); |
|
| 112 | + $entityManager->flush(); |
|
| 113 | + |
|
| 114 | + $signatureComponents = $this->emailVerifier->getSignatureComponents('swp_user_verify_email', $user); |
|
| 115 | + $url = $signatureComponents->getSignedUrl(); |
|
| 116 | + |
|
| 117 | + $mailer->sendConfirmationEmail($user, $url); |
|
| 118 | + |
|
| 119 | + return new JsonResponse([ |
|
| 120 | + 'message' => sprintf( |
|
| 121 | + 'The user has been created successfully. |
|
| 122 | 122 | An email has been sent to %s. It contains an activation link you must click to activate your account.', |
| 123 | - $user->getEmail() |
|
| 124 | - ), |
|
| 125 | - 'url' => $url, |
|
| 126 | - ]); |
|
| 123 | + $user->getEmail() |
|
| 124 | + ), |
|
| 125 | + 'url' => $url, |
|
| 126 | + ]); |
|
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @Route("/verify/email", name="swp_user_verify_email") |
|
| 134 | - */ |
|
| 135 | - public function verifyUserEmail( |
|
| 136 | - Request $request, |
|
| 137 | - GuardAuthenticatorHandler $guardHandler, |
|
| 138 | - LoginAuthenticator $authenticator |
|
| 139 | - ): Response { |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @Route("/verify/email", name="swp_user_verify_email") |
|
| 134 | + */ |
|
| 135 | + public function verifyUserEmail( |
|
| 136 | + Request $request, |
|
| 137 | + GuardAuthenticatorHandler $guardHandler, |
|
| 138 | + LoginAuthenticator $authenticator |
|
| 139 | + ): Response { |
|
| 140 | 140 | $id = (int)$request->get('id'); // retrieve the user id from the url |
| 141 | 141 | |
| 142 | 142 | if ($request->isXmlHttpRequest()) { |
| 143 | - return $this->verifyUserEmailFromPWA($id, $request); |
|
| 143 | + return $this->verifyUserEmailFromPWA($id, $request); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Verify the user id exists and is not null |
| 147 | 147 | if (null === $id) { |
| 148 | - return $this->redirectToRoute('homepage'); |
|
| 148 | + return $this->redirectToRoute('homepage'); |
|
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | $user = $this->userManager->find($id); |
| 152 | 152 | |
| 153 | 153 | // Ensure the user exists in persistence |
| 154 | 154 | if (null === $user) { |
| 155 | - return $this->redirectToRoute('homepage'); |
|
| 155 | + return $this->redirectToRoute('homepage'); |
|
| 156 | 156 | } |
| 157 | 157 | // validate email confirmation link, sets User::isVerified=true and persists |
| 158 | 158 | try { |
| 159 | - $this->emailVerifier->handleEmailConfirmation($request, $user); |
|
| 159 | + $this->emailVerifier->handleEmailConfirmation($request, $user); |
|
| 160 | 160 | } catch (VerifyEmailExceptionInterface $exception) { |
| 161 | - $this->addFlash('verify_email_error', $exception->getReason()); |
|
| 161 | + $this->addFlash('verify_email_error', $exception->getReason()); |
|
| 162 | 162 | |
| 163 | - return $this->redirectToRoute('homepage'); |
|
| 163 | + return $this->redirectToRoute('homepage'); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | 166 | |
@@ -174,63 +174,63 @@ discard block |
||
| 174 | 174 | $this->addFlash('success', 'The user has been created successfully.'); |
| 175 | 175 | |
| 176 | 176 | return $this->redirectToRoute('swp_user_registration_confirmed'); |
| 177 | - } |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Tell the user his account is now confirmed. |
|
| 181 | - */ |
|
| 182 | - public function confirmedAction(Request $request) { |
|
| 179 | + /** |
|
| 180 | + * Tell the user his account is now confirmed. |
|
| 181 | + */ |
|
| 182 | + public function confirmedAction(Request $request) { |
|
| 183 | 183 | $user = $this->getUser(); |
| 184 | 184 | if (!is_object($user) || !$user instanceof UserInterface) { |
| 185 | - $this->createAccessDeniedException('This user does not have access to this section.'); |
|
| 185 | + $this->createAccessDeniedException('This user does not have access to this section.'); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | return $this->render('@SWPUser/Registration/confirmed.html.twig', [ |
| 189 | 189 | 'user' => $user, |
| 190 | 190 | ]); |
| 191 | - } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - /** |
|
| 194 | - * @throws NotFoundHttpException |
|
| 195 | - */ |
|
| 196 | - private function ensureThatRegistrationIsEnabled() { |
|
| 193 | + /** |
|
| 194 | + * @throws NotFoundHttpException |
|
| 195 | + */ |
|
| 196 | + private function ensureThatRegistrationIsEnabled() { |
|
| 197 | 197 | $settingName = 'registration_enabled'; |
| 198 | 198 | $setting = $this->settingsManager->getOneSettingByName($settingName); |
| 199 | 199 | $registrationEnabled = $this->settingsManager |
| 200 | 200 | ->get($settingName, $setting['scope'], $this->scopeContext->getScopeOwner($setting['scope'])); |
| 201 | 201 | if (!$registrationEnabled) { |
| 202 | - throw new NotFoundHttpException('Registration is disabled.'); |
|
| 202 | + throw new NotFoundHttpException('Registration is disabled.'); |
|
| 203 | + } |
|
| 203 | 204 | } |
| 204 | - } |
|
| 205 | 205 | |
| 206 | - private function verifyUserEmailFromPWA(int $id, Request $request): JsonResponse { |
|
| 206 | + private function verifyUserEmailFromPWA(int $id, Request $request): JsonResponse { |
|
| 207 | 207 | // Verify the user id exists and is not null |
| 208 | 208 | if (null === $id) { |
| 209 | - return new JsonResponse( |
|
| 210 | - ['error' => 'User does not exist'] |
|
| 211 | - ); |
|
| 209 | + return new JsonResponse( |
|
| 210 | + ['error' => 'User does not exist'] |
|
| 211 | + ); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | $user = $this->userManager->find($id); |
| 215 | 215 | |
| 216 | 216 | // Ensure the user exists in persistence |
| 217 | 217 | if (null === $user) { |
| 218 | - return new JsonResponse( |
|
| 219 | - ['error' => 'User does not exist'] |
|
| 220 | - ); |
|
| 218 | + return new JsonResponse( |
|
| 219 | + ['error' => 'User does not exist'] |
|
| 220 | + ); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // validate email confirmation link, sets User::isVerified=true and persists |
| 224 | 224 | try { |
| 225 | - $this->emailVerifier->handleEmailConfirmation($request, $user); |
|
| 225 | + $this->emailVerifier->handleEmailConfirmation($request, $user); |
|
| 226 | 226 | } catch (VerifyEmailExceptionInterface $exception) { |
| 227 | - return new JsonResponse( |
|
| 228 | - ['error' => 'Registration confirmation invalid'] |
|
| 229 | - ); |
|
| 227 | + return new JsonResponse( |
|
| 228 | + ['error' => 'Registration confirmation invalid'] |
|
| 229 | + ); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | return new JsonResponse( |
| 233 | 233 | ['message' => 'The user has been created successfully.'] |
| 234 | 234 | ); |
| 235 | - } |
|
| 235 | + } |
|
| 236 | 236 | } |
@@ -137,7 +137,7 @@ |
||
| 137 | 137 | GuardAuthenticatorHandler $guardHandler, |
| 138 | 138 | LoginAuthenticator $authenticator |
| 139 | 139 | ): Response { |
| 140 | - $id = (int)$request->get('id'); // retrieve the user id from the url |
|
| 140 | + $id = (int) $request->get('id'); // retrieve the user id from the url |
|
| 141 | 141 | |
| 142 | 142 | if ($request->isXmlHttpRequest()) { |
| 143 | 143 | return $this->verifyUserEmailFromPWA($id, $request); |
@@ -54,7 +54,7 @@ |
||
| 54 | 54 | return $user; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - public function loadUserByIdentifier(string $identifier): SecurityUserInterface|UserInterface |
|
| 57 | + public function loadUserByIdentifier(string $identifier): SecurityUserInterface | UserInterface |
|
| 58 | 58 | { |
| 59 | 59 | $user = $this->findUser($identifier); |
| 60 | 60 | |
@@ -38,117 +38,117 @@ discard block |
||
| 38 | 38 | |
| 39 | 39 | class AuthController extends AbstractController |
| 40 | 40 | { |
| 41 | - protected FormFactoryInterface $formFactory; |
|
| 42 | - protected ApiKeyRepositoryInterface $apiKeyRepository; |
|
| 43 | - protected ApiKeyFactory $apiKeyFactory; |
|
| 44 | - protected LockFactory $lockFactory; |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param FormFactoryInterface $formFactory |
|
| 48 | - * @param ApiKeyRepositoryInterface $apiKeyRepository |
|
| 49 | - * @param ApiKeyFactory $apiKeyFactory |
|
| 50 | - * @param LockFactory $lockFactory |
|
| 51 | - */ |
|
| 52 | - public function __construct( |
|
| 53 | - FormFactoryInterface $formFactory, |
|
| 54 | - ApiKeyRepositoryInterface $apiKeyRepository, |
|
| 55 | - ApiKeyFactory$apiKeyFactory, |
|
| 56 | - LockFactory $lockFactory |
|
| 57 | - ) { |
|
| 41 | + protected FormFactoryInterface $formFactory; |
|
| 42 | + protected ApiKeyRepositoryInterface $apiKeyRepository; |
|
| 43 | + protected ApiKeyFactory $apiKeyFactory; |
|
| 44 | + protected LockFactory $lockFactory; |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param FormFactoryInterface $formFactory |
|
| 48 | + * @param ApiKeyRepositoryInterface $apiKeyRepository |
|
| 49 | + * @param ApiKeyFactory $apiKeyFactory |
|
| 50 | + * @param LockFactory $lockFactory |
|
| 51 | + */ |
|
| 52 | + public function __construct( |
|
| 53 | + FormFactoryInterface $formFactory, |
|
| 54 | + ApiKeyRepositoryInterface $apiKeyRepository, |
|
| 55 | + ApiKeyFactory$apiKeyFactory, |
|
| 56 | + LockFactory $lockFactory |
|
| 57 | + ) { |
|
| 58 | 58 | $this->formFactory = $formFactory; |
| 59 | 59 | $this->apiKeyRepository = $apiKeyRepository; |
| 60 | 60 | $this->apiKeyFactory = $apiKeyFactory; |
| 61 | 61 | $this->lockFactory = $lockFactory; |
| 62 | - } |
|
| 63 | - |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @Route( |
|
| 67 | - * "/api/{version}/auth/", |
|
| 68 | - * options={"expose"=true}, |
|
| 69 | - * defaults={"version"="v2"}, |
|
| 70 | - * methods={"POST"}, |
|
| 71 | - * name="swp_api_auth" |
|
| 72 | - * ) |
|
| 73 | - */ |
|
| 74 | - public function authenticateAction( |
|
| 75 | - Request $request, |
|
| 76 | - UserProviderInterface $userProvider, |
|
| 77 | - UserPasswordHasherInterface $userPasswordEncoder |
|
| 78 | - ) { |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @Route( |
|
| 67 | + * "/api/{version}/auth/", |
|
| 68 | + * options={"expose"=true}, |
|
| 69 | + * defaults={"version"="v2"}, |
|
| 70 | + * methods={"POST"}, |
|
| 71 | + * name="swp_api_auth" |
|
| 72 | + * ) |
|
| 73 | + */ |
|
| 74 | + public function authenticateAction( |
|
| 75 | + Request $request, |
|
| 76 | + UserProviderInterface $userProvider, |
|
| 77 | + UserPasswordHasherInterface $userPasswordEncoder |
|
| 78 | + ) { |
|
| 79 | 79 | $form = $this->formFactory->createNamed('', UserAuthenticationType::class, []); |
| 80 | 80 | $form->handleRequest($request); |
| 81 | 81 | if ($form->isSubmitted() && $form->isValid()) { |
| 82 | - $formData = $form->getData(); |
|
| 82 | + $formData = $form->getData(); |
|
| 83 | 83 | |
| 84 | - try { |
|
| 84 | + try { |
|
| 85 | 85 | $user = $userProvider->loadUserByIdentifier($formData['username']); |
| 86 | - } catch (AuthenticationException $e) { |
|
| 86 | + } catch (AuthenticationException $e) { |
|
| 87 | 87 | $user = null; |
| 88 | - } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - if ((null !== $user) && $userPasswordEncoder->isPasswordValid($user, $formData['password'])) { |
|
| 90 | + if ((null !== $user) && $userPasswordEncoder->isPasswordValid($user, $formData['password'])) { |
|
| 91 | 91 | return $this->returnApiTokenResponse($user); |
| 92 | - } |
|
| 92 | + } |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | return new SingleResourceResponse([ |
| 96 | 96 | 'status' => 401, |
| 97 | 97 | 'message' => 'Unauthorized', |
| 98 | 98 | ], new ResponseContext(401)); |
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * @Route( |
|
| 103 | - * "/api/{version}/auth/superdesk/", |
|
| 104 | - * options={"expose"=true}, |
|
| 105 | - * methods={"POST"}, |
|
| 106 | - * defaults={"version"="v2"}, |
|
| 107 | - * name="swp_api_auth_superdesk" |
|
| 108 | - * ) |
|
| 109 | - */ |
|
| 110 | - public function authenticateWithSuperdeskAction( |
|
| 111 | - Request $request, |
|
| 112 | - LoggerInterface $logger, |
|
| 113 | - array $superdeskServers, |
|
| 114 | - UserProviderInterface $userProvider, |
|
| 115 | - UserManagerInterface $userManager |
|
| 116 | - ) { |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * @Route( |
|
| 103 | + * "/api/{version}/auth/superdesk/", |
|
| 104 | + * options={"expose"=true}, |
|
| 105 | + * methods={"POST"}, |
|
| 106 | + * defaults={"version"="v2"}, |
|
| 107 | + * name="swp_api_auth_superdesk" |
|
| 108 | + * ) |
|
| 109 | + */ |
|
| 110 | + public function authenticateWithSuperdeskAction( |
|
| 111 | + Request $request, |
|
| 112 | + LoggerInterface $logger, |
|
| 113 | + array $superdeskServers, |
|
| 114 | + UserProviderInterface $userProvider, |
|
| 115 | + UserManagerInterface $userManager |
|
| 116 | + ) { |
|
| 117 | 117 | $form = $this->formFactory->createNamed('', SuperdeskCredentialAuthenticationType::class, []); |
| 118 | 118 | $form->handleRequest($request); |
| 119 | 119 | if ($form->isSubmitted() && $form->isValid()) { |
| 120 | - $formData = $form->getData(); |
|
| 121 | - $authorizedSuperdeskHosts = $superdeskServers; |
|
| 122 | - $superdeskUser = null; |
|
| 123 | - $client = new GuzzleHttp\Client(); |
|
| 120 | + $formData = $form->getData(); |
|
| 121 | + $authorizedSuperdeskHosts = $superdeskServers; |
|
| 122 | + $superdeskUser = null; |
|
| 123 | + $client = new GuzzleHttp\Client(); |
|
| 124 | 124 | |
| 125 | - foreach ($authorizedSuperdeskHosts as $baseUrl) { |
|
| 125 | + foreach ($authorizedSuperdeskHosts as $baseUrl) { |
|
| 126 | 126 | try { |
| 127 | - $apiRequest = new GuzzleHttp\Psr7\Request('GET', sprintf('%s/api/sessions/%s', $baseUrl, $formData['sessionId']), [ |
|
| 128 | - 'Authorization' => $formData['token'], |
|
| 129 | - ]); |
|
| 127 | + $apiRequest = new GuzzleHttp\Psr7\Request('GET', sprintf('%s/api/sessions/%s', $baseUrl, $formData['sessionId']), [ |
|
| 128 | + 'Authorization' => $formData['token'], |
|
| 129 | + ]); |
|
| 130 | 130 | |
| 131 | - $apiResponse = $client->send($apiRequest); |
|
| 132 | - if (200 !== $apiResponse->getStatusCode()) { |
|
| 131 | + $apiResponse = $client->send($apiRequest); |
|
| 132 | + if (200 !== $apiResponse->getStatusCode()) { |
|
| 133 | 133 | $logger->warning(sprintf('[%s] Unsuccessful response from Superdesk Server: %s', $apiResponse->getStatusCode(), $apiResponse->getBody()->getContents())); |
| 134 | 134 | |
| 135 | 135 | continue; |
| 136 | - } |
|
| 136 | + } |
|
| 137 | 137 | |
| 138 | - $content = json_decode($apiResponse->getBody()->getContents(), true); |
|
| 139 | - if (is_array($content) && array_key_exists('user', $content)) { |
|
| 138 | + $content = json_decode($apiResponse->getBody()->getContents(), true); |
|
| 139 | + if (is_array($content) && array_key_exists('user', $content)) { |
|
| 140 | 140 | $superdeskUser = $content['user']; |
| 141 | 141 | |
| 142 | 142 | break; |
| 143 | - } |
|
| 143 | + } |
|
| 144 | 144 | } catch (GuzzleHttp\Exception\ClientException $e) { |
| 145 | - $logger->warning(sprintf('Error when logging in Superdesk: %s', $e->getMessage())); |
|
| 145 | + $logger->warning(sprintf('Error when logging in Superdesk: %s', $e->getMessage())); |
|
| 146 | 146 | |
| 147 | - continue; |
|
| 147 | + continue; |
|
| 148 | + } |
|
| 148 | 149 | } |
| 149 | - } |
|
| 150 | 150 | |
| 151 | - if (null === $superdeskUser) { |
|
| 151 | + if (null === $superdeskUser) { |
|
| 152 | 152 | return new SingleResourceResponse([ |
| 153 | 153 | 'status' => 401, |
| 154 | 154 | 'message' => <<<'MESSAGE' |
@@ -156,18 +156,18 @@ discard block |
||
| 156 | 156 | Make sure that Publisher can talk to Superdesk instance. Set it's address in "SUPERDESK_SERVERS" environment variable. |
| 157 | 157 | MESSAGE, |
| 158 | 158 | ], new ResponseContext(401)); |
| 159 | - } |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | - $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
| 162 | - if (null === $publisherUser) { |
|
| 161 | + $publisherUser = $userProvider->findOneByEmail($superdeskUser['email']); |
|
| 162 | + if (null === $publisherUser) { |
|
| 163 | 163 | try { |
| 164 | - $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
| 164 | + $publisherUser = $userProvider->loadUserByUsername($superdeskUser['username']); |
|
| 165 | 165 | } catch (AuthenticationException $e) { |
| 166 | - $publisherUser = null; |
|
| 166 | + $publisherUser = null; |
|
| 167 | + } |
|
| 167 | 168 | } |
| 168 | - } |
|
| 169 | 169 | |
| 170 | - if (null === $publisherUser) { |
|
| 170 | + if (null === $publisherUser) { |
|
| 171 | 171 | /** @var UserInterface $publisherUser */ |
| 172 | 172 | $publisherUser = $userManager->createUser(); |
| 173 | 173 | $publisherUser->setUsername($superdeskUser['username']); |
@@ -177,20 +177,20 @@ discard block |
||
| 177 | 177 | $publisherUser->setLastName(\array_key_exists('last_name', $superdeskUser) ? $superdeskUser['last_name'] : ''); |
| 178 | 178 | $publisherUser->setPassword(password_hash(random_bytes(36), PASSWORD_BCRYPT)); |
| 179 | 179 | $userManager->updateUser($publisherUser); |
| 180 | - } |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - if (null !== $publisherUser) { |
|
| 182 | + if (null !== $publisherUser) { |
|
| 183 | 183 | return $this->returnApiTokenResponse($publisherUser, str_replace('Basic ', '', $formData['token'])); |
| 184 | - } |
|
| 184 | + } |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | return new SingleResourceResponse([ |
| 188 | 188 | 'status' => 401, |
| 189 | 189 | 'message' => 'Unauthorized', |
| 190 | 190 | ], new ResponseContext(401)); |
| 191 | - } |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface { |
|
| 193 | + private function returnApiTokenResponse(UserInterface $user, string $token = null): SingleResourceResponseInterface { |
|
| 194 | 194 | /** @var ApiKeyInterface $apiKey */ |
| 195 | 195 | $apiKey = $this->generateOrGetApiKey($user, $token); |
| 196 | 196 | |
@@ -201,36 +201,36 @@ discard block |
||
| 201 | 201 | ], |
| 202 | 202 | 'user' => $user, |
| 203 | 203 | ]); |
| 204 | - } |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface { |
|
| 206 | + private function generateOrGetApiKey(UserInterface $user, $token): ?ApiKeyInterface { |
|
| 207 | 207 | $apiKey = null; |
| 208 | 208 | if (null !== $token) { |
| 209 | - $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
| 209 | + $apiKey = $this->apiKeyRepository->getValidToken($token)->getQuery()->getOneOrNullResult(); |
|
| 210 | 210 | } else { |
| 211 | - $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
| 212 | - if (count($validKeys) > 0) { |
|
| 211 | + $validKeys = $this->apiKeyRepository->getValidTokenForUser($user)->getQuery()->getResult(); |
|
| 212 | + if (count($validKeys) > 0) { |
|
| 213 | 213 | $apiKey = reset($validKeys); |
| 214 | - } |
|
| 214 | + } |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | if (null === $apiKey) { |
| 218 | - $apiKey = $this->apiKeyFactory->create($user, $token); |
|
| 218 | + $apiKey = $this->apiKeyFactory->create($user, $token); |
|
| 219 | 219 | |
| 220 | - try { |
|
| 220 | + try { |
|
| 221 | 221 | $lock = $this->lockFactory->createLock(md5(json_encode(['type' => 'user_api_key', 'user' => $user->getId()])), 2); |
| 222 | 222 | if (!$lock->acquire()) { |
| 223 | - throw new RuntimeException('Other api key is created right now for this user'); |
|
| 223 | + throw new RuntimeException('Other api key is created right now for this user'); |
|
| 224 | 224 | } |
| 225 | 225 | $this->apiKeyRepository->add($apiKey); |
| 226 | 226 | $lock->release(); |
| 227 | - } catch (RuntimeException $e) { |
|
| 227 | + } catch (RuntimeException $e) { |
|
| 228 | 228 | sleep(2); |
| 229 | 229 | |
| 230 | 230 | return $this->generateOrGetApiKey($user, $token); |
| 231 | - } |
|
| 231 | + } |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | return $apiKey; |
| 235 | - } |
|
| 235 | + } |
|
| 236 | 236 | } |
@@ -126,9 +126,9 @@ |
||
| 126 | 126 | return $data; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - $callback = function ($matches) { |
|
| 129 | + $callback = function($matches) { |
|
| 130 | 130 | $matches[2] = trim(preg_replace('/\s\s+/', ' ', $matches[2])); |
| 131 | - return 's:' . mb_strlen($matches[2]) . ':"' . $matches[2] . '";'; |
|
| 131 | + return 's:'.mb_strlen($matches[2]).':"'.$matches[2].'";'; |
|
| 132 | 132 | }; |
| 133 | 133 | |
| 134 | 134 | $data = preg_replace_callback('!s:(\d+):"(.*?)";!s', $callback, $data); |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | if (is_array($extraItem)) { |
| 74 | 74 | $extra = ArticleExtraEmbedField::newFromValue($key, $extraItem); |
| 75 | 75 | } else { |
| 76 | - $extra = ArticleExtraTextField::newFromValue($key, (string)$extraItem); |
|
| 76 | + $extra = ArticleExtraTextField::newFromValue($key, (string) $extraItem); |
|
| 77 | 77 | } |
| 78 | 78 | $extra->setArticle($article); |
| 79 | 79 | $this->entityManager->persist($extra); |
@@ -113,9 +113,9 @@ discard block |
||
| 113 | 113 | return $unserialized; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | - $callback = function ($matches) { |
|
| 116 | + $callback = function($matches) { |
|
| 117 | 117 | $matches[2] = trim(preg_replace('/\s\s+/', ' ', $matches[2])); |
| 118 | - return 's:' . mb_strlen($matches[2]) . ':"' . $matches[2] . '";'; |
|
| 118 | + return 's:'.mb_strlen($matches[2]).':"'.$matches[2].'";'; |
|
| 119 | 119 | }; |
| 120 | 120 | |
| 121 | 121 | $fixedData = preg_replace_callback('!s:(\d+):"(.*?)";!s', $callback, $data); |
@@ -44,35 +44,35 @@ discard block |
||
| 44 | 44 | use FOS\RestBundle\Controller\Annotations\Route; |
| 45 | 45 | |
| 46 | 46 | class ContentListItemController extends AbstractController { |
| 47 | - private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 48 | - private EntityManagerInterface $entityManager; |
|
| 49 | - private ContentListServiceInterface $contentListService; |
|
| 50 | - private EventDispatcherInterface $eventDispatcher; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 54 | - * @param EntityManagerInterface $entityManager |
|
| 55 | - * @param ContentListServiceInterface $contentListService |
|
| 56 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 57 | - */ |
|
| 58 | - public function __construct(ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 59 | - EntityManagerInterface $entityManager, |
|
| 60 | - ContentListServiceInterface $contentListService, |
|
| 61 | - EventDispatcherInterface $eventDispatcher) { |
|
| 47 | + private ContentListItemRepositoryInterface $contentListItemRepository; |
|
| 48 | + private EntityManagerInterface $entityManager; |
|
| 49 | + private ContentListServiceInterface $contentListService; |
|
| 50 | + private EventDispatcherInterface $eventDispatcher; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * @param ContentListItemRepositoryInterface $contentListItemRepository |
|
| 54 | + * @param EntityManagerInterface $entityManager |
|
| 55 | + * @param ContentListServiceInterface $contentListService |
|
| 56 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 57 | + */ |
|
| 58 | + public function __construct(ContentListItemRepositoryInterface $contentListItemRepository, |
|
| 59 | + EntityManagerInterface $entityManager, |
|
| 60 | + ContentListServiceInterface $contentListService, |
|
| 61 | + EventDispatcherInterface $eventDispatcher) { |
|
| 62 | 62 | $this->contentListItemRepository = $contentListItemRepository; |
| 63 | 63 | $this->entityManager = $entityManager; |
| 64 | 64 | $this->contentListService = $contentListService; |
| 65 | 65 | $this->eventDispatcher = $eventDispatcher; |
| 66 | - } |
|
| 66 | + } |
|
| 67 | 67 | |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 71 | - */ |
|
| 72 | - public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 69 | + /** |
|
| 70 | + * @Route("/api/{version}/content/lists/{id}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_list_items", requirements={"id"="\d+"}) |
|
| 71 | + */ |
|
| 72 | + public function listAction(Request $request, int $id): ResourcesListResponseInterface { |
|
| 73 | 73 | $sort = $request->query->all('sorting'); |
| 74 | 74 | if (empty($sort)) { |
| 75 | - $sort = ['sticky' => 'desc']; |
|
| 75 | + $sort = ['sticky' => 'desc']; |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $items = $this->contentListItemRepository->getPaginatedByCriteria( |
@@ -105,20 +105,20 @@ discard block |
||
| 105 | 105 | ); |
| 106 | 106 | |
| 107 | 107 | return new ResourcesListResponse($items, $responseContext); |
| 108 | - } |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_show_lists_item", requirements={"id"="\d+"}) |
|
| 112 | - */ |
|
| 113 | - public function getAction($listId, $id) { |
|
| 110 | + /** |
|
| 111 | + * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_show_lists_item", requirements={"id"="\d+"}) |
|
| 112 | + */ |
|
| 113 | + public function getAction($listId, $id) { |
|
| 114 | 114 | return new SingleResourceResponse($this->findOr404($listId, $id)); |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_lists_item", requirements={"id"="\d+", "listId"="\d+"}) |
|
| 119 | - */ |
|
| 120 | - public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 121 | - $id): SingleResourceResponseInterface { |
|
| 117 | + /** |
|
| 118 | + * @Route("/api/{version}/content/lists/{listId}/items/{id}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_update_lists_item", requirements={"id"="\d+", "listId"="\d+"}) |
|
| 119 | + */ |
|
| 120 | + public function updateAction(Request $request, FormFactoryInterface $formFactory, $listId, |
|
| 121 | + $id): SingleResourceResponseInterface { |
|
| 122 | 122 | $contentListItem = $this->findOr404($listId, $id); |
| 123 | 123 | $form = $formFactory->createNamed( |
| 124 | 124 | '', |
@@ -130,59 +130,59 @@ discard block |
||
| 130 | 130 | $form->handleRequest($request); |
| 131 | 131 | |
| 132 | 132 | if ($form->isSubmitted() && $form->isValid()) { |
| 133 | - $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 133 | + $contentListItem->getContentList()->setUpdatedAt(new DateTime()); |
|
| 134 | 134 | |
| 135 | - if (null !== $contentListItem->getStickyPosition()) { |
|
| 135 | + if (null !== $contentListItem->getStickyPosition()) { |
|
| 136 | 136 | $contentListItem->setPosition($contentListItem->getStickyPosition()); |
| 137 | - } |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - $this->entityManager->flush(); |
|
| 139 | + $this->entityManager->flush(); |
|
| 140 | 140 | |
| 141 | - return new SingleResourceResponse($contentListItem); |
|
| 141 | + return new SingleResourceResponse($contentListItem); |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @Route("/api/{version}/content/lists/{listId}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_batch_update_lists_item", requirements={"listId"="\d+"}) |
|
| 149 | - */ |
|
| 150 | - public function batchUpdateAction( |
|
| 151 | - Request $request, |
|
| 152 | - FormFactoryInterface $formFactory, |
|
| 153 | - ContentListRepositoryInterface $contentListRepository, |
|
| 154 | - ArticleRepositoryInterface $articleRepository, |
|
| 155 | - EventDispatcherInterface $eventDispatcher, |
|
| 156 | - int $listId |
|
| 157 | - ): SingleResourceResponseInterface { |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @Route("/api/{version}/content/lists/{listId}/items/", options={"expose"=true}, defaults={"version"="v2"}, methods={"PATCH"}, name="swp_api_core_batch_update_lists_item", requirements={"listId"="\d+"}) |
|
| 149 | + */ |
|
| 150 | + public function batchUpdateAction( |
|
| 151 | + Request $request, |
|
| 152 | + FormFactoryInterface $formFactory, |
|
| 153 | + ContentListRepositoryInterface $contentListRepository, |
|
| 154 | + ArticleRepositoryInterface $articleRepository, |
|
| 155 | + EventDispatcherInterface $eventDispatcher, |
|
| 156 | + int $listId |
|
| 157 | + ): SingleResourceResponseInterface { |
|
| 158 | 158 | /** @var ContentListInterface $list */ |
| 159 | 159 | $list = $contentListRepository->findOneBy(['id' => $listId]); |
| 160 | 160 | if (null === $list) { |
| 161 | - throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 161 | + throw new NotFoundHttpException(sprintf('Content list with id "%s" was not found.', $list)); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | $form = $formFactory->createNamed('', ContentListItemsType::class, [], ['method' => $request->getMethod()]); |
| 165 | 165 | |
| 166 | 166 | $form->handleRequest($request); |
| 167 | 167 | if ($form->isSubmitted() && $form->isValid()) { |
| 168 | - $data = $form->getData(); |
|
| 169 | - $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 170 | - $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 171 | - $listUpdatedAt = $list->getUpdatedAt(); |
|
| 172 | - $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 173 | - if ($updatedAt < $listUpdatedAt) { |
|
| 168 | + $data = $form->getData(); |
|
| 169 | + $updatedAt = DateTime::createFromFormat(DateTime::RFC3339, $data['updatedAt'], new DateTimeZone('UTC')); |
|
| 170 | + $updatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 171 | + $listUpdatedAt = $list->getUpdatedAt(); |
|
| 172 | + $listUpdatedAt->setTimezone(new DateTimeZone('UTC')); |
|
| 173 | + if ($updatedAt < $listUpdatedAt) { |
|
| 174 | 174 | throw new ConflictHttpException('List was already updated'); |
| 175 | - } |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - $updatedArticles = []; |
|
| 178 | - /** @var ContentListAction $item */ |
|
| 179 | - foreach ($data['items'] as $item) { |
|
| 177 | + $updatedArticles = []; |
|
| 178 | + /** @var ContentListAction $item */ |
|
| 179 | + foreach ($data['items'] as $item) { |
|
| 180 | 180 | $position = $item->getPosition(); |
| 181 | 181 | $isSticky = $item->isSticky(); |
| 182 | 182 | $contentId = $item->getContentId(); |
| 183 | 183 | |
| 184 | 184 | switch ($item->getAction()) { |
| 185 | - case ContentListAction::ACTION_MOVE: |
|
| 185 | + case ContentListAction::ACTION_MOVE: |
|
| 186 | 186 | $updated = false; |
| 187 | 187 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 188 | 188 | |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 211 | 211 | |
| 212 | 212 | break; |
| 213 | - case ContentListAction::ACTION_ADD: |
|
| 213 | + case ContentListAction::ACTION_ADD: |
|
| 214 | 214 | $this->ensureThereIsNoItemOnPositionOrThrow409( |
| 215 | 215 | $listId, |
| 216 | 216 | $position, |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | $updatedArticles[$contentId] = $contentListItem->getContent(); |
| 225 | 225 | |
| 226 | 226 | break; |
| 227 | - case ContentListAction::ACTION_DELETE: |
|
| 227 | + case ContentListAction::ACTION_DELETE: |
|
| 228 | 228 | $contentListItem = $this->findByContentOr404($list, $contentId); |
| 229 | 229 | $this->entityManager->remove($contentListItem); |
| 230 | 230 | $list->setUpdatedAt(new DateTime('now')); |
@@ -233,25 +233,25 @@ discard block |
||
| 233 | 233 | |
| 234 | 234 | break; |
| 235 | 235 | } |
| 236 | - } |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | - $this->contentListService->repositionStickyItems($list); |
|
| 238 | + $this->contentListService->repositionStickyItems($list); |
|
| 239 | 239 | |
| 240 | - foreach ($updatedArticles as $updatedArticle) { |
|
| 240 | + foreach ($updatedArticles as $updatedArticle) { |
|
| 241 | 241 | $eventDispatcher->dispatch(new ArticleEvent( |
| 242 | 242 | $updatedArticle, |
| 243 | 243 | $updatedArticle->getPackage(), |
| 244 | 244 | ArticleEvents::POST_UPDATE |
| 245 | 245 | ), ArticleEvents::POST_UPDATE); |
| 246 | - } |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 248 | + return new SingleResourceResponse($list, new ResponseContext(201)); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 252 | - } |
|
| 252 | + } |
|
| 253 | 253 | |
| 254 | - private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 254 | + private function findByContentOr404($listId, $contentId): ContentListItemInterface { |
|
| 255 | 255 | /** @var ContentListItemInterface $listItem */ |
| 256 | 256 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 257 | 257 | 'contentList' => $listId, |
@@ -259,13 +259,13 @@ discard block |
||
| 259 | 259 | ]); |
| 260 | 260 | |
| 261 | 261 | if (null === $listItem) { |
| 262 | - throw new NotFoundHttpException(sprintf('Content list item with content_id "%s" was not found on that list. If You want to add new item - use action type "add".', $contentId)); |
|
| 262 | + throw new NotFoundHttpException(sprintf('Content list item with content_id "%s" was not found on that list. If You want to add new item - use action type "add".', $contentId)); |
|
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | return $listItem; |
| 266 | - } |
|
| 266 | + } |
|
| 267 | 267 | |
| 268 | - private function findOr404($listId, $id): ContentListItemInterface { |
|
| 268 | + private function findOr404($listId, $id): ContentListItemInterface { |
|
| 269 | 269 | /** @var ContentListItemInterface $listItem */ |
| 270 | 270 | $listItem = $this->contentListItemRepository->findOneBy([ |
| 271 | 271 | 'contentList' => $listId, |
@@ -273,29 +273,29 @@ discard block |
||
| 273 | 273 | ]); |
| 274 | 274 | |
| 275 | 275 | if (null === $listItem) { |
| 276 | - throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 276 | + throw new NotFoundHttpException(sprintf('Content list item with id "%s" was not found.', $id)); |
|
| 277 | 277 | } |
| 278 | 278 | |
| 279 | 279 | return $listItem; |
| 280 | - } |
|
| 280 | + } |
|
| 281 | 281 | |
| 282 | - private function ensureThereIsNoItemOnPositionOrThrow409( |
|
| 283 | - int $listId, |
|
| 284 | - int $position, |
|
| 285 | - bool $isSticky, |
|
| 286 | - string $action): void { |
|
| 287 | - $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
|
| 282 | + private function ensureThereIsNoItemOnPositionOrThrow409( |
|
| 283 | + int $listId, |
|
| 284 | + int $position, |
|
| 285 | + bool $isSticky, |
|
| 286 | + string $action): void { |
|
| 287 | + $existingContentListItem = $this->contentListService->isAnyItemPinnedOnPosition($listId, $position); |
|
| 288 | 288 | |
| 289 | - if (!$existingContentListItem && !$isSticky) { |
|
| 290 | - return; |
|
| 291 | - } |
|
| 289 | + if (!$existingContentListItem && !$isSticky) { |
|
| 290 | + return; |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - if ($existingContentListItem && $existingContentListItem->isSticky()) { |
|
| 293 | + if ($existingContentListItem && $existingContentListItem->isSticky()) { |
|
| 294 | 294 | throw new ConflictHttpException('There is already an item pinned on that position. Unpin it first.'); |
| 295 | - } |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - if ($action === ContentListAction::ACTION_MOVE && $isSticky) { |
|
| 298 | - throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); |
|
| 299 | - } |
|
| 300 | - } |
|
| 297 | + if ($action === ContentListAction::ACTION_MOVE && $isSticky) { |
|
| 298 | + throw new ConflictHttpException('Cannot move pinned item. Unpin it first.'); |
|
| 299 | + } |
|
| 300 | + } |
|
| 301 | 301 | } |
@@ -94,15 +94,15 @@ |
||
| 94 | 94 | |
| 95 | 95 | if ($seoMetadata->getMetaMedia()) { |
| 96 | 96 | $metaImage = $seoMetadata->getMetaMedia()->getImage(); |
| 97 | - $response['_links']['meta_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 97 | + $response['_links']['meta_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 98 | 98 | } |
| 99 | 99 | if ($seoMetadata->getOgMedia()) { |
| 100 | 100 | $metaImage = $seoMetadata->getOgMedia()->getImage(); |
| 101 | - $response['_links']['og_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 101 | + $response['_links']['og_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 102 | 102 | } |
| 103 | 103 | if ($seoMetadata->getTwitterMedia()) { |
| 104 | 104 | $metaImage = $seoMetadata->getTwitterMedia()->getImage(); |
| 105 | - $response['_links']['twitter_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 105 | + $response['_links']['twitter_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | return new SingleResourceResponse($response, new ResponseContext(201)); |
@@ -20,10 +20,10 @@ discard block |
||
| 20 | 20 | use FOS\RestBundle\Controller\Annotations\Route; |
| 21 | 21 | |
| 22 | 22 | class SeoMetadataController extends AbstractController { |
| 23 | - private FormFactoryInterface $formFactory; |
|
| 24 | - private FactoryInterface $seoMetadataFactory; |
|
| 25 | - private RepositoryInterface $seoMetadataRepository; |
|
| 26 | - private EventDispatcherInterface $eventDispatcher; |
|
| 23 | + private FormFactoryInterface $formFactory; |
|
| 24 | + private FactoryInterface $seoMetadataFactory; |
|
| 25 | + private RepositoryInterface $seoMetadataRepository; |
|
| 26 | + private EventDispatcherInterface $eventDispatcher; |
|
| 27 | 27 | protected MediaManagerInterface $mediaManager; |
| 28 | 28 | |
| 29 | 29 | /** |
@@ -33,29 +33,29 @@ discard block |
||
| 33 | 33 | * @param EventDispatcherInterface $eventDispatcher |
| 34 | 34 | * @param MediaManagerInterface $mediaManager |
| 35 | 35 | */ |
| 36 | - public function __construct( |
|
| 37 | - FormFactoryInterface $formFactory, |
|
| 38 | - FactoryInterface $seoMetadataFactory, |
|
| 39 | - RepositoryInterface $seoMetadataRepository, |
|
| 40 | - EventDispatcherInterface $eventDispatcher, |
|
| 41 | - MediaManagerInterface $mediaManager |
|
| 42 | - ) { |
|
| 36 | + public function __construct( |
|
| 37 | + FormFactoryInterface $formFactory, |
|
| 38 | + FactoryInterface $seoMetadataFactory, |
|
| 39 | + RepositoryInterface $seoMetadataRepository, |
|
| 40 | + EventDispatcherInterface $eventDispatcher, |
|
| 41 | + MediaManagerInterface $mediaManager |
|
| 42 | + ) { |
|
| 43 | 43 | $this->formFactory = $formFactory; |
| 44 | 44 | $this->seoMetadataFactory = $seoMetadataFactory; |
| 45 | 45 | $this->seoMetadataRepository = $seoMetadataRepository; |
| 46 | 46 | $this->eventDispatcher = $eventDispatcher; |
| 47 | - $this->mediaManager = $mediaManager; |
|
| 48 | - } |
|
| 47 | + $this->mediaManager = $mediaManager; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PUT"}, name="swp_api_core_seo_metadata_put") |
|
| 52 | - */ |
|
| 53 | - public function put(Request $request, string $packageGuid): SingleResourceResponse { |
|
| 50 | + /** |
|
| 51 | + * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"PUT"}, name="swp_api_core_seo_metadata_put") |
|
| 52 | + */ |
|
| 53 | + public function put(Request $request, string $packageGuid): SingleResourceResponse { |
|
| 54 | 54 | $this->eventDispatcher->dispatch(new GenericEvent(), MultiTenancyEvents::TENANTABLE_DISABLE); |
| 55 | 55 | |
| 56 | 56 | $seoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($packageGuid); |
| 57 | 57 | if (null === $seoMetadata) { |
| 58 | - $seoMetadata = $this->seoMetadataFactory->create(); |
|
| 58 | + $seoMetadata = $this->seoMetadataFactory->create(); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | $form = $this->formFactory->createNamed('', SeoMetadataType::class, $seoMetadata, ['method' => $request->getMethod()]); |
@@ -63,48 +63,48 @@ discard block |
||
| 63 | 63 | $form->handleRequest($request); |
| 64 | 64 | |
| 65 | 65 | if ($form->isSubmitted() && $form->isValid()) { |
| 66 | - $seoMetadata->setPackageGuid($packageGuid); |
|
| 67 | - $this->seoMetadataRepository->add($seoMetadata); |
|
| 66 | + $seoMetadata->setPackageGuid($packageGuid); |
|
| 67 | + $this->seoMetadataRepository->add($seoMetadata); |
|
| 68 | 68 | |
| 69 | - return new SingleResourceResponse($seoMetadata, new ResponseContext(200)); |
|
| 69 | + return new SingleResourceResponse($seoMetadata, new ResponseContext(200)); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | return new SingleResourceResponse($form, new ResponseContext(400)); |
| 73 | - } |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_seo_metadata_get") |
|
| 77 | - */ |
|
| 78 | - public function getAction(string $packageGuid): SingleResourceResponse { |
|
| 75 | + /** |
|
| 76 | + * @Route("/api/{version}/packages/seo/{packageGuid}", options={"expose"=true}, defaults={"version"="v2"}, methods={"GET"}, name="swp_api_core_seo_metadata_get") |
|
| 77 | + */ |
|
| 78 | + public function getAction(string $packageGuid): SingleResourceResponse { |
|
| 79 | 79 | $this->eventDispatcher->dispatch(new GenericEvent(), MultiTenancyEvents::TENANTABLE_DISABLE); |
| 80 | 80 | |
| 81 | - $seoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($packageGuid); |
|
| 81 | + $seoMetadata = $this->seoMetadataRepository->findOneByPackageGuid($packageGuid); |
|
| 82 | 82 | if (null === $seoMetadata) { |
| 83 | - throw new NotFoundHttpException('SEO metadata not found!'); |
|
| 83 | + throw new NotFoundHttpException('SEO metadata not found!'); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - $response = [ |
|
| 87 | - "meta_title" => $seoMetadata->getMetaTitle(), |
|
| 88 | - "meta_description" => $seoMetadata->getMetaDescription(), |
|
| 89 | - "og_title" => $seoMetadata->getOgTitle(), |
|
| 90 | - "og_description" => $seoMetadata->getOgDescription(), |
|
| 91 | - "twitter_title" => $seoMetadata->getTwitterTitle(), |
|
| 92 | - "twitter_description" => $seoMetadata->getTwitterDescription(), |
|
| 93 | - ]; |
|
| 94 | - |
|
| 95 | - if ($seoMetadata->getMetaMedia()) { |
|
| 96 | - $metaImage = $seoMetadata->getMetaMedia()->getImage(); |
|
| 97 | - $response['_links']['meta_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 98 | - } |
|
| 99 | - if ($seoMetadata->getOgMedia()) { |
|
| 100 | - $metaImage = $seoMetadata->getOgMedia()->getImage(); |
|
| 101 | - $response['_links']['og_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 102 | - } |
|
| 103 | - if ($seoMetadata->getTwitterMedia()) { |
|
| 104 | - $metaImage = $seoMetadata->getTwitterMedia()->getImage(); |
|
| 105 | - $response['_links']['twitter_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 106 | - } |
|
| 86 | + $response = [ |
|
| 87 | + "meta_title" => $seoMetadata->getMetaTitle(), |
|
| 88 | + "meta_description" => $seoMetadata->getMetaDescription(), |
|
| 89 | + "og_title" => $seoMetadata->getOgTitle(), |
|
| 90 | + "og_description" => $seoMetadata->getOgDescription(), |
|
| 91 | + "twitter_title" => $seoMetadata->getTwitterTitle(), |
|
| 92 | + "twitter_description" => $seoMetadata->getTwitterDescription(), |
|
| 93 | + ]; |
|
| 94 | + |
|
| 95 | + if ($seoMetadata->getMetaMedia()) { |
|
| 96 | + $metaImage = $seoMetadata->getMetaMedia()->getImage(); |
|
| 97 | + $response['_links']['meta_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 98 | + } |
|
| 99 | + if ($seoMetadata->getOgMedia()) { |
|
| 100 | + $metaImage = $seoMetadata->getOgMedia()->getImage(); |
|
| 101 | + $response['_links']['og_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 102 | + } |
|
| 103 | + if ($seoMetadata->getTwitterMedia()) { |
|
| 104 | + $metaImage = $seoMetadata->getTwitterMedia()->getImage(); |
|
| 105 | + $response['_links']['twitter_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 106 | + } |
|
| 107 | 107 | |
| 108 | 108 | return new SingleResourceResponse($response, new ResponseContext(200)); |
| 109 | - } |
|
| 109 | + } |
|
| 110 | 110 | } |
@@ -94,15 +94,15 @@ |
||
| 94 | 94 | |
| 95 | 95 | if ($seoMetadata->getMetaMedia()) { |
| 96 | 96 | $metaImage = $seoMetadata->getMetaMedia()->getImage(); |
| 97 | - $response['_links']['meta_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 97 | + $response['_links']['meta_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 98 | 98 | } |
| 99 | 99 | if ($seoMetadata->getOgMedia()) { |
| 100 | 100 | $metaImage = $seoMetadata->getOgMedia()->getImage(); |
| 101 | - $response['_links']['og_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 101 | + $response['_links']['og_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 102 | 102 | } |
| 103 | 103 | if ($seoMetadata->getTwitterMedia()) { |
| 104 | 104 | $metaImage = $seoMetadata->getTwitterMedia()->getImage(); |
| 105 | - $response['_links']['twitter_media_url'] = [ 'href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 105 | + $response['_links']['twitter_media_url'] = ['href' => $this->mediaManager->getMediaPublicUrl($metaImage)]; |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | return new SingleResourceResponse($response, new ResponseContext(200)); |