Skip to content

Commit

Permalink
Add swiftmailer bc-layer and update pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler committed Apr 9, 2024
1 parent d538845 commit d0bc406
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ jobs:
if: ${{ matrix.php-version == '7.2' }}
run: composer remove php-cs-fixer/shim --dev --no-interaction

- name: Install additional lowest dependencies
if: ${{ matrix.dependency-versions == 'lowest' }}
run: |
composer require symfony/swiftmailer-bundle --no-interaction --no-update
- name: Install composer dependencies
uses: ramsey/composer-install@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private function addPermissions(RoleInterface $role, string $system, string $web
}
}

if (0 === \strpos($securityContext, 'sulu.webspaces.')
if (\str_starts_with($securityContext, 'sulu.webspaces.')
&& $webspaceSecurityContext !== $securityContext
) {
// Do not add permissions for other webspaces
Expand Down
14 changes: 5 additions & 9 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,11 @@ protected function encodePassword(User $user, string $plainPassword): string
$hasher = $this->container->get('?security.password_hasher');

return $hasher->hashPassword($user, $plainPassword);
} else {
/** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('?security.password_encoder');

return $encoder->encodePassword($user, $plainPassword);
}
/** @var \Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('?security.password_encoder');

return $encoder->encodePassword($user, $plainPassword);
}

/**
Expand Down Expand Up @@ -147,9 +146,6 @@ private function getTemplateAttributes(array $custom = []): array
return $this->getTemplateAttributeResolver()->resolve($custom);
}

/**
* @return User
*/
public function getUser(): ?User
{
$user = parent::getUser();
Expand Down Expand Up @@ -190,7 +186,7 @@ private function addAddress(User $user): void
/**
* @param mixed[] $parameters
*/
public function render(string $view, array $parameters = [], Response $response = null): Response
public function render(string $view, array $parameters = [], ?Response $response = null): Response
{
return parent::render(
$view,
Expand Down
1 change: 1 addition & 0 deletions Controller/BlacklistItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* Provides admin-api for blacklist-items.
*
* @NamePrefix("sulu_community.")
*
* @RouteResource("blacklist-item")
*/
class BlacklistItemController extends AbstractRestController implements ClassResourceInterface
Expand Down
2 changes: 1 addition & 1 deletion Controller/ConfirmationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function indexAction(Request $request, string $token): Response
$redirectTo = $communityManager->getConfigTypeProperty(self::TYPE, Configuration::REDIRECT_TO);

if ($redirectTo) {
if (0 === \strpos($redirectTo, '/')) {
if (\str_starts_with($redirectTo, '/')) {

Check failure on line 52 in Controller/ConfirmationController.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 (highest, Lint true)

Parameter #1 $haystack of function str_starts_with expects string, array<string, array<array|bool|string|null>|string> given.
$url = \str_replace('{localization}', $request->getLocale(), $redirectTo);
} else {
$url = $this->getRouter()->generate($redirectTo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
* },
* delete_user: bool,
* }
*
* @phpstan-type Config array{
* from: string|string[],
* to: string|string[],
Expand Down Expand Up @@ -84,7 +83,7 @@ public function process(ContainerBuilder $container): void
$references[$webspaceKey] = new Reference($id);
$container->setDefinition($id, $definition);

if (false !== \strpos($webspaceKey, '-')) {
if (\str_contains($webspaceKey, '-')) {
$container->setAlias(
\sprintf('sulu_community.%s.community_manager', $webspaceKey),
\sprintf('sulu_community.%s.community_manager', Normalizer::normalize($webspaceKey))
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/SuluCommunityExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public function prepend(ContainerBuilder $container): void
'orm' => [
'dql' => [
'string_functions' => [
'regexp' => RegExp::class,
'regexp' => Regexp::class,
],
],
],
Expand Down
4 changes: 0 additions & 4 deletions Entity/BlacklistItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ class BlacklistItem
*/
private $type;

/**
* @param string $pattern
* @param string $type
*/
public function __construct(?string $pattern = null, ?string $type = null)
{
$this->type = $type;
Expand Down
2 changes: 1 addition & 1 deletion EventListener/LastLoginListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static function getSubscribedEvents()
*/
public function onRequest(RequestEvent $event): void
{
if (!$event->isMasterRequest()) {
if (!$event->isMainRequest()) {
return;
}

Expand Down
2 changes: 0 additions & 2 deletions Mail/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Mail
* user_template: string|null,
* admin_template: string|null,
* } $config
*
* @return Mail
*/
public static function create($from, $to, array $config): self
{
Expand Down
43 changes: 28 additions & 15 deletions Mail/MailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Sulu\Bundle\CommunityBundle\Mail;

use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
Expand All @@ -25,7 +26,7 @@
class MailFactory implements MailFactoryInterface
{
/**
* @var MailerInterface
* @var MailerInterface|\Swift_Mailer
*/
protected $mailer;

Expand All @@ -39,7 +40,7 @@ class MailFactory implements MailFactoryInterface
*/
protected $translator;

public function __construct(MailerInterface $mailer, Environment $twig, TranslatorInterface $translator)
public function __construct($mailer, Environment $twig, TranslatorInterface $translator)

Check failure on line 43 in Mail/MailFactory.php

View workflow job for this annotation

GitHub Actions / PHP 8.1 (highest, Lint true)

Method Sulu\Bundle\CommunityBundle\Mail\MailFactory::__construct() has parameter $mailer with no type specified.
{
$this->mailer = $mailer;
$this->twig = $twig;
Expand Down Expand Up @@ -72,7 +73,6 @@ public function sendEmails(Mail $mail, User $user, array $parameters = []): void
}
}


/**
* Create and send email.
*
Expand All @@ -84,30 +84,43 @@ protected function sendEmail($from, $to, string $subject, string $template, arra
{
$body = $this->twig->render($template, $data);

$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);
if ($this->mailer instanceof \Swift_Mailer) {
$email = $this->mailer->createMessage()
->setSubject($this->translator->trans($subject))
->setFrom($from)
->setTo($to)
->setBody($body, 'text/html');

$this->mailer->send($email);
} else {
if (!$this->getAddress($from) || !$this->getAddress($to)) {
return;
}

$email = (new Email())
->subject($this->translator->trans($subject))
->from($this->getAddress($from))
->to($this->getAddress($to))
->html($body);
}

$this->mailer->send($email);
}

/**
* Convert string/array email address to an Address object
* Convert string/array email address to an Address object.
*
* @param $address
* @return Address
* @param mixed $address
*/
protected function getAddress($address): ?Address
{
$name = '';

if (is_array($address)) {
if(empty($address)) {
if (\is_array($address)) {
if (empty($address)) {
return null;
} else if (!isset($address['email'])) {
$email = $address[array_keys($address)[0]];
} elseif (!isset($address['email'])) {
$email = $address[\array_keys($address)[0]];
} else {
$email = $address['email'];
$name = $address['name'] ?? '';
Expand Down
3 changes: 0 additions & 3 deletions Manager/CommunityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* },
* delete_user: bool,
* }
*
* @phpstan-type Config array{
* from: string|string[],
* to: string|string[],
Expand Down Expand Up @@ -137,8 +136,6 @@ public function sendEmails(string $type, User $user): void;

/**
* Save profile for given user.
*
* @return User
*/
public function saveProfile(User $user): ?User;
}
2 changes: 1 addition & 1 deletion Tests/Application/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\HttpKernel\Kernel;

return static function(PhpFileLoader $loader, ContainerBuilder $container) {
return static function (PhpFileLoader $loader, ContainerBuilder $container) {
$context = $container->getParameter('sulu.context');

$loader->import('context_' . $context . '.yml');
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/Controller/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public function testRegistrationBlacklistedBlocked(): void
$this->assertNull($this->findUser());
}

public function testRegistrationBlacklistedRequested(): RawMessage
public function testRegistrationBlacklistedRequested(): ?RawMessage
{
$this->createBlacklistItem($this->getEntityManager(), '*@sulu.io', BlacklistItem::TYPE_REQUEST);

Expand Down
14 changes: 9 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
"type": "sulu-bundle",
"license": "MIT",
"require": {
"php": "^8.0 || ^8.1",
"php": "^7.2 || ^8.0",
"beberlei/doctrineextensions": "^1.0",
"doctrine/doctrine-bundle": "^1.10 || ^2.0",
"doctrine/orm": "^2.5.3",
"doctrine/persistence": "^1.3 || ^2.0",
"doctrine/persistence": "^1.3 || ^2.0 || ^3.0",
"doctrine/phpcr-bundle": "^2 || ^3.0",
"jms/serializer-bundle": "^3.3 || ^4.0",
"massive/build-bundle": "^0.3 || ^0.4 || ^0.5",
"sulu/sulu": "^2.5.0 || ^2.6@dev",
"sulu/sulu": "^2.4.0 || ^2.6@dev",
"symfony/config": "^5.4 || ^6.0",
"symfony/console": "^5.4 || ^6.0",
"symfony/dependency-injection": "^5.4 || ^6.0",
"symfony/event-dispatcher": "^5.4 || ^6.0",
"symfony/form": "^5.4 || ^6.0",
"symfony/mailer": "^5.4 || ^6.0",
"symfony/framework-bundle": "^5.4 || ^6.0",
"symfony/http-foundation": "^5.4 || ^6.0",
"symfony/http-kernel": "^5.4 || ^6.0",
"symfony/intl": "^5.4 || ^6.0",
"symfony/mailer": "^5.4 || ^6.0",
"symfony/routing": "^5.4 || ^6.0",
"symfony/security-bundle": "^5.4 || ^6.0"
},
Expand Down Expand Up @@ -112,6 +113,9 @@
]
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"php-http/discovery": true
}
}
}
64 changes: 62 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
parameters:
ignoreErrors:
-
message: "#^Else branch is unreachable because previous condition is always true\\.$#"
count: 1
path: Command/InitCommand.php

-
message: "#^Call to method encodePassword\\(\\) on an unknown class Symfony\\\\Component\\\\Security\\\\Core\\\\Encoder\\\\UserPasswordEncoderInterface\\.$#"
count: 1
path: Controller/AbstractController.php

-
message: "#^Method Sulu\\\\Bundle\\\\CommunityBundle\\\\Controller\\\\AbstractController\\:\\:getSubscribedServices\\(\\) should return array\\<int\\|string, string\\> but returns array\\<string\\|Symfony\\\\Contracts\\\\Service\\\\Attribute\\\\SubscribedService\\>\\.$#"
count: 1
path: Controller/AbstractController.php

-
message: "#^PHPDoc tag @var for variable \\$encoder contains unknown class Symfony\\\\Component\\\\Security\\\\Core\\\\Encoder\\\\UserPasswordEncoderInterface\\.$#"
count: 1
path: Controller/AbstractController.php

-
message: "#^Strict comparison using \\=\\=\\= between true and array\\{from\\: array\\<string\\>\\|string, to\\: array\\<string\\>\\|string, webspace_key\\: string, role\\: string, firewall\\: string, maintenance\\: array\\{enabled\\: bool, template\\: string\\}, login\\: array\\{enabled\\: bool, template\\: string, service\\: string\\|null, embed_template\\: string, type\\: string, options\\: array, activate_user\\: bool, auto_login\\: bool, \\.\\.\\.\\}, registration\\: array\\{enabled\\: bool, template\\: string, service\\: string\\|null, embed_template\\: string, type\\: string, options\\: array, activate_user\\: bool, auto_login\\: bool, \\.\\.\\.\\}, \\.\\.\\.\\} will always evaluate to false\\.$#"
count: 1
Expand Down Expand Up @@ -250,6 +270,31 @@ parameters:
count: 1
path: EventListener/MailListener.php

-
message: "#^Call to method createMessage\\(\\) on an unknown class Swift_Mailer\\.$#"
count: 1
path: Mail/MailFactory.php

-
message: "#^Call to method send\\(\\) on an unknown class Swift_Mailer\\.$#"
count: 2
path: Mail/MailFactory.php

-
message: "#^Class Swift_Mailer not found\\.$#"
count: 1
path: Mail/MailFactory.php

-
message: "#^Parameter \\$mailer of method Sulu\\\\Bundle\\\\CommunityBundle\\\\Mail\\\\MailFactory\\:\\:__construct\\(\\) has invalid type Swift_Mailer\\.$#"
count: 1
path: Mail/MailFactory.php

-
message: "#^Property Sulu\\\\Bundle\\\\CommunityBundle\\\\Mail\\\\MailFactory\\:\\:\\$mailer has unknown class Swift_Mailer as its type\\.$#"
count: 1
path: Mail/MailFactory.php

-
message: "#^Method Sulu\\\\Bundle\\\\CommunityBundle\\\\Manager\\\\CommunityManager\\:\\:getConfigProperty\\(\\) should return array\\{from\\: array\\<string\\>\\|string, to\\: array\\<string\\>\\|string, webspace_key\\: string, role\\: string, firewall\\: string, maintenance\\: array\\{enabled\\: bool, template\\: string\\}, login\\: array\\{enabled\\: bool, template\\: string, service\\: string\\|null, embed_template\\: string, type\\: string, options\\: array, activate_user\\: bool, auto_login\\: bool, \\.\\.\\.\\}, registration\\: array\\{enabled\\: bool, template\\: string, service\\: string\\|null, embed_template\\: string, type\\: string, options\\: array, activate_user\\: bool, auto_login\\: bool, \\.\\.\\.\\}, \\.\\.\\.\\} but returns array\\<array\\|bool\\|string\\|null\\>\\|string\\.$#"
count: 1
Expand All @@ -275,6 +320,11 @@ parameters:
count: 1
path: Manager/CommunityManager.php

-
message: "#^Parameter \\#2 \\$firewallName of class Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\UsernamePasswordToken constructor expects string, array\\<string, array\\<array\\|bool\\|string\\|null\\>\\|string\\> given\\.$#"
count: 1
path: Manager/CommunityManager.php

-
message: "#^Parameter \\#2 \\$to of static method Sulu\\\\Bundle\\\\CommunityBundle\\\\Mail\\\\Mail\\:\\:create\\(\\) expects array\\<string\\>\\|string, array\\<string, array\\<array\\|bool\\|string\\|null\\>\\|string\\> given\\.$#"
count: 1
Expand All @@ -291,7 +341,17 @@ parameters:
path: Manager/CommunityManager.php

-
message: "#^Parameter \\#3 \\$roles of class Symfony\\\\Component\\\\Security\\\\Core\\\\Authentication\\\\Token\\\\UsernamePasswordToken constructor expects array\\<string\\>, array\\<string, array\\<array\\|bool\\|string\\|null\\>\\|string\\> given\\.$#"
message: "#^If condition is always true\\.$#"
count: 1
path: Manager/CommunityManager.php
path: Tests/Application/config/config.php

-
message: "#^Cannot call method getHtmlBody\\(\\) on Symfony\\\\Component\\\\Mime\\\\RawMessage\\|null\\.$#"
count: 3
path: Tests/Functional/Controller/RegistrationTest.php

-
message: "#^Cannot call method getTo\\(\\) on Symfony\\\\Component\\\\Mime\\\\RawMessage\\|null\\.$#"
count: 3
path: Tests/Functional/Controller/RegistrationTest.php

0 comments on commit d0bc406

Please sign in to comment.