Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code cleanup #27

Merged
merged 25 commits into from Dec 4, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
da179c3
big cleanup, improved consistency and test coverage; this breaks BC
SamMousa Mar 23, 2022
e77e8ab
fix: psr3 compliance and cleaned up the logger using the trait from t…
SamMousa Mar 23, 2022
b925c56
rewrote logger to not use service locator pattern and added tests for it
SamMousa Mar 23, 2022
4fb882b
implement interfaces and wrappers for signers and encrypters
SamMousa Mar 24, 2022
47ce95e
fix #28
SamMousa Mar 24, 2022
090a226
fix CS using easy coding standard, remove logging getters and setters…
SamMousa Mar 24, 2022
3c53053
Update src/DkimSignerWrapper.php
SamMousa Mar 24, 2022
5599b51
Update src/SMimeEncrypterWrapper.php
SamMousa Mar 24, 2022
50b7aae
Update src/SymfonyMessageSignerInterface.php
SamMousa Mar 24, 2022
d423914
Update src/SymfonyMessageWrapperInterface.php
SamMousa Mar 24, 2022
923a1b6
Update src/SymfonyMessageEncrypterInterface.php
SamMousa Mar 24, 2022
f6a284e
Update src/SMimeSignerWrapper.php
SamMousa Mar 24, 2022
ffa9fb6
fix: pr feedback
SamMousa Mar 24, 2022
64bd4b0
chore: getTransport is now private
SamMousa Apr 12, 2022
1a8bf25
chore: optimize imports
SamMousa Apr 12, 2022
b6ccfc2
add static analysis and apply fixes
SamMousa Apr 12, 2022
254535a
chore: fix cs
SamMousa Aug 16, 2022
9563579
chore: add test for serialization #35
SamMousa Aug 16, 2022
5a5b6e9
chore: add changelog
SamMousa Aug 16, 2022
8a7bb5d
fix: dont silence exceptions #37
SamMousa Aug 16, 2022
74dc189
fix: remove unused Logger
SamMousa Aug 16, 2022
ed9d662
fix: mailer couldn't be instantiated via yii createobject
SamMousa Aug 17, 2022
5b174e4
feat: allow transportfactory to be unset in mailer
SamMousa Aug 17, 2022
0c13d06
chore: fix cs add captainhook for automatic checks
SamMousa Aug 17, 2022
307f857
Merge remote-tracking branch 'origin/master' into code-cleanup
SamMousa Sep 29, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -30,7 +30,8 @@
"symfony/mailer": ">=5.4.0"
},
"require-dev": {
"phpunit/phpunit": "9.5.10"
"phpunit/phpunit": "9.5.10",
"symplify/easy-coding-standard": "^10.1"
},
"repositories": [
{
Expand Down
28 changes: 28 additions & 0 deletions ecs.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

use PhpCsFixer\Fixer\ArrayNotation\ArraySyntaxFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PATHS, [
__DIR__ . '/src',
__DIR__ . '/tests',
]);

$services = $containerConfigurator->services();
$services->set(ArraySyntaxFixer::class)
->call('configure', [[
'syntax' => 'short',
]]);

// run and fix, one by one
$containerConfigurator->import(SetList::SPACES);
$containerConfigurator->import(SetList::ARRAY);
$containerConfigurator->import(SetList::DOCBLOCK);
$containerConfigurator->import(SetList::PSR_12);
};
8 changes: 7 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -5,10 +5,16 @@
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Yii2-swiftmailer Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
26 changes: 26 additions & 0 deletions src/DkimSignerWrapper.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace yii\symfonymailer;

use Symfony\Component\Mime\Crypto\DkimSigner;
use Symfony\Component\Mime\Message;

/**
* @codeCoverageIgnore This class is a trivial proxy that requires no testing
*/
class DkimSignerWrapper implements SymfonyMessageSignerInterface
SamMousa marked this conversation as resolved.
Show resolved Hide resolved
{
private DkimSigner $dkimSigner;

public function __construct(DkimSigner $dkimSigner)
{
$this->dkimSigner = $dkimSigner;
}

public function sign(Message $message, array $options = []): Message
{
return $this->dkimSigner->sign($message, $options);
}
}
155 changes: 26 additions & 129 deletions src/Logger.php
Expand Up @@ -7,124 +7,37 @@

namespace yii\symfonymailer;

use Yii;
use Psr\Log\InvalidArgumentException;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Psr\Log\LogLevel;
use Yii;
use yii\log\Logger as YiiLogger;

final class Logger implements LoggerInterface
{
/**
* System is unusable.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function emergency($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_ERROR, __METHOD__);
}
use LoggerTrait;

/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function alert($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_ERROR, __METHOD__);
}
private YiiLogger $logger;

/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function critical($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_ERROR, __METHOD__);
}
private array $map;

/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function error($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_ERROR, __METHOD__);
}
private string $category;

/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function warning($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_WARNING, __METHOD__);
}

/**
* Normal but significant events.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function notice($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_WARNING, __METHOD__);
}

/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function info($message, array $context = []): void
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_INFO, __METHOD__);
}

/**
* Detailed debug information.
*
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*/
public function debug($message, array $context = []): void
public function __construct(YiiLogger $logger, array $map = [
LogLevel::ERROR => YiiLogger::LEVEL_ERROR,
LogLevel::CRITICAL => YiiLogger::LEVEL_ERROR,
LogLevel::ALERT => YiiLogger::LEVEL_ERROR,
LogLevel::EMERGENCY => YiiLogger::LEVEL_ERROR,
LogLevel::NOTICE => YiiLogger::LEVEL_WARNING,
LogLevel::WARNING => YiiLogger::LEVEL_WARNING,
LogLevel::DEBUG => YiiLogger::LEVEL_INFO,
LogLevel::INFO => YiiLogger::LEVEL_INFO,
], string $category = 'PSR Logging Adapter')
SamMousa marked this conversation as resolved.
Show resolved Hide resolved
{
Yii::getLogger()->log($message, \yii\log\Logger::LEVEL_INFO, __METHOD__);
$this->logger = $logger;
$this->map = $map;
$this->category = $category;
}

/**
Expand All @@ -134,30 +47,14 @@ public function debug($message, array $context = []): void
* @param string|\Stringable $message
* @param mixed[] $context
*
* @return void
*
* @throws \Psr\Log\InvalidArgumentException
*/
public function log($level, $message, array $context = []): void
{
switch ($level) {
case 'error':
case 'critical':
case 'alert':
case 'emergency':
$level = \yii\log\Logger::LEVEL_ERROR;
break;
case 'notice':
case 'warning':
$level = \yii\log\Logger::LEVEL_WARNING;
break;
case 'debug':
case 'info':
$level = \yii\log\Logger::LEVEL_INFO;
break;
default:
$level = \yii\log\Logger::LEVEL_INFO;
if (! isset($this->map[$level])) {
SamMousa marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidArgumentException("Unknown logging level $level");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new InvalidArgumentException("Unknown logging level $level");
throw new InvalidArgumentException("Unknown logging level $level.");

}
Yii::getLogger()->log($message, $level, __METHOD__);

$this->logger->log($message, $this->map[$level], $this->category);
}
}