Skip to content

Commit

Permalink
Merge pull request #8 from bedita/restore-cloudfront-adapter
Browse files Browse the repository at this point in the history
Restore correct behavior of `AwsS3CloudFrontAdapter`, which was never invalidating anything
  • Loading branch information
fquffio authored Aug 31, 2022
2 parents fc507cb + 9315ece commit 1081c3c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 117 deletions.
56 changes: 35 additions & 21 deletions src/Filesystem/Adapter/AwsS3CloudFrontAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ class AwsS3CloudFrontAdapter extends AwsS3V3Adapter
*
* @var \Aws\CloudFront\CloudFrontClient|null
*/
protected $cloudfrontClient = null;
protected ?CloudFrontClient $cloudfrontClient = null;

/**
* CloudFront distribution ID.
*
* @var string|null
*/
protected ?string $distributionId = null;

/**
* CloudFront distribution path prefix.
*
* @var string|null
*/
protected ?string $cloudfrontPathPrefix = null;

/**
* Adapter constructor.
Expand All @@ -44,14 +58,16 @@ class AwsS3CloudFrontAdapter extends AwsS3V3Adapter
* @param bool $streamReads Whether reads should be streamed.
* @param \Aws\CloudFront\CloudFrontClient|null $cloudfrontClient CloudFront client instance, or `null`.
*/
public function __construct(S3ClientInterface $client, $bucket, $prefix = '', array $options = [], $streamReads = true, ?CloudFrontClient $cloudfrontClient = null)
public function __construct(S3ClientInterface $client, string $bucket, string $prefix = '', array $options = [], $streamReads = true, ?CloudFrontClient $cloudfrontClient = null)
{
parent::__construct($client, $bucket, $prefix, null, null, $options, $streamReads);

if (!empty($options['distributionId']) && $cloudfrontClient === null) {
throw new DomainException('When `distributionId` is set, a CloudFront client instance is required');
}
$this->cloudfrontClient = $cloudfrontClient;
$this->distributionId = $options['distributionId'] ?? null;
$this->cloudfrontPathPrefix = $options['cloudFrontPathPrefix'] ?? null;
}

/**
Expand All @@ -69,12 +85,10 @@ public function getCloudFrontClient(): ?CloudFrontClient
*
* @return string|null
*/
// TODO: `options` attribute is now private in base class,
// and this method doesn't work anymore - see if there's a different way to obtain this ID.
// public function getDistributionId(): ?string
// {
// return $this->options['distributionId'] ?? null;
// }
public function getDistributionId(): ?string
{
return $this->distributionId;
}

/**
* Check whether CloudFront configuration is set.
Expand All @@ -89,12 +103,12 @@ public function hasCloudFrontConfig(): bool
/**
* @inheritDoc
*/
public function copy(string $path, string $newpath, Config $config): void
public function copy(string $source, string $destination, Config $config): void
{
$existed = $this->hasCloudFrontConfig() && $this->fileExists($newpath);
parent::copy($path, $newpath, $config);
$existed = $this->hasCloudFrontConfig() && $this->fileExists($destination);
parent::copy($source, $destination, $config);
if ($existed) {
$this->createCloudFrontInvalidation($newpath);
$this->createCloudFrontInvalidation($destination);
}
}

Expand All @@ -113,19 +127,19 @@ public function delete(string $path): void
/**
* @inheritDoc
*/
public function deleteDirectory(string $dirname): void
public function deleteDirectory(string $path): void
{
parent::deleteDirectory($dirname);
$this->createCloudFrontInvalidation(rtrim($dirname, '/') . '/*');
parent::deleteDirectory($path);
$this->createCloudFrontInvalidation(rtrim($path, '/') . '/*');
}

/**
* @inheritDoc
*/
public function write(string $path, string $body, Config $config): void
public function write(string $path, string $contents, Config $config): void
{
$existed = $this->hasCloudFrontConfig() && $this->fileExists($path);
parent::write($path, $body, $config);
parent::write($path, $contents, $config);
if ($existed) {
$this->createCloudFrontInvalidation($path);
}
Expand All @@ -140,11 +154,11 @@ public function write(string $path, string $body, Config $config): void
protected function applyCloudFrontPathPrefix(string $path): string
{
$path = '/' . ltrim($path, '/');
if (empty($this->options['cloudFrontPathPrefix'])) {
if (empty($this->cloudfrontPathPrefix)) {
return $path;
}

return '/' . trim($this->options['cloudFrontPathPrefix'], '/') . $path;
return '/' . trim($this->cloudfrontPathPrefix, '/') . $path;
}

/**
Expand All @@ -155,13 +169,13 @@ protected function applyCloudFrontPathPrefix(string $path): string
*/
protected function createCloudFrontInvalidation(string $path): void
{
if ($this->cloudfrontClient === null || empty($this->options['distributionId'])) {
if ($this->cloudfrontClient === null || empty($this->distributionId)) {
return;
}

try {
$this->cloudfrontClient->createInvalidation([
'DistributionId' => $this->options['distributionId'],
'DistributionId' => $this->distributionId,
'InvalidationBatch' => [
'CallerReference' => uniqid($path),
'Paths' => [
Expand Down
4 changes: 2 additions & 2 deletions src/Filesystem/Adapter/S3Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ class S3Adapter extends FilesystemAdapter
*
* @var \Aws\S3\S3Client|null
*/
protected $client;
protected ?S3Client $client;

/**
* AWS CloudFront client.
*
* @var \Aws\CloudFront\CloudFrontClient|null
*/
protected $cloudFrontClient;
protected ?CloudFrontClient $cloudFrontClient;

/**
* @inheritDoc
Expand Down
12 changes: 6 additions & 6 deletions src/Mailer/Transport/SesTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SesTransport extends AbstractTransport
*
* @var \Aws\Ses\SesClient|null
*/
protected $client;
protected ?SesClient $client;

/**
* @inheritDoc
Expand Down Expand Up @@ -78,19 +78,19 @@ protected function getClient(): SesClient
/**
* @inheritDoc
*/
public function send(Message $email): array
public function send(Message $message): array
{
$headerList = ['from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'];
$headers = $email->getHeadersString($headerList, static::EOL);
$headers = $message->getHeadersString($headerList, static::EOL);

$message = $email->getBodyString(static::EOL);
$body = $message->getBodyString(static::EOL);

$this->getClient()->sendRawEmail([
'RawMessage' => [
'Data' => $headers . static::EOL . static::EOL . $message,
'Data' => $headers . static::EOL . static::EOL . $body,
],
]);

return compact('headers', 'message');
return ['headers' => $headers, 'message' => $body];
}
}
21 changes: 8 additions & 13 deletions src/Mailer/Transport/SnsTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SnsTransport extends AbstractTransport
*
* @var \Aws\Sns\SnsClient|null
*/
protected $client;
protected ?SnsClient $client;

/**
* @inheritDoc
Expand All @@ -70,21 +70,16 @@ protected function getClient(): SnsClient
}

/**
* Send mail
*
* @param \Cake\Mailer\Message $email Email message.
* @return array
* @inheritDoc
*/
public function send(Message $email): array
public function send(Message $message): array
{
$from = $email->getFrom();
$to = $email->getTo();
$from = $message->getFrom();
$to = $message->getTo();

$phoneNumber = reset($to);
$senderId = trim(reset($from));
/** @var string $message */
$message = $email->getBodyText();
$message = trim($message);
$body = trim($message->getBodyText());
$smsType = $this->getConfig('smsType');

$attributes = [];
Expand All @@ -102,11 +97,11 @@ public function send(Message $email): array
}

$this->getClient()->publish([
'Message' => $message,
'Message' => $body,
'PhoneNumber' => $phoneNumber,
'MessageAttributes' => $attributes,
]);

return compact('message') + ['headers' => ''];
return ['headers' => '', 'message' => $body];
}
}
4 changes: 2 additions & 2 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use BEdita\Core\Filesystem\FilesystemRegistry;
use Cake\Core\BasePlugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Mailer\Email;
use Cake\Mailer\Mailer;

/**
* Plugin class.
Expand All @@ -36,7 +36,7 @@ public function bootstrap(PluginApplicationInterface $app): void
parent::bootstrap($app);

// Register SES (email) and SNS (SMS) transports.
Email::setDsnClassMap([
Mailer::setDsnClassMap([
'ses' => SesTransport::class,
'sns' => SnsTransport::class,
]);
Expand Down
9 changes: 5 additions & 4 deletions tests/TestCase/Authenticator/AlbAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Lcobucci\JWT\Encoding\JoseEncoder;
use Lcobucci\JWT\Signer\Ecdsa\MultibyteStringConverter;
use Lcobucci\JWT\Signer\Ecdsa\Sha256;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Signer\None;
use Lcobucci\JWT\Token\Builder;
Expand All @@ -54,28 +55,28 @@ class AlbAuthenticatorTest extends TestCase
*
* @var string
*/
protected $keyId;
protected string $keyId;

/**
* Private key material.
*
* @var \Lcobucci\JWT\Signer\Key
*/
protected $privateKey;
protected Key $privateKey;

/**
* Requests history.
*
* @var array<int, array{request: \GuzzleHttp\Psr7\Request, response: \GuzzleHttp\Psr7\Response|null, error: \GuzzleHttp\Exception\GuzzleException|null, options: array}>
*/
protected $history = [];
protected array $history = [];

/**
* Guzzle HTTP handler.
*
* @var HandlerStack
*/
protected $handler;
protected HandlerStack $handler;

/**
* @inheritDoc
Expand Down
Loading

0 comments on commit 1081c3c

Please sign in to comment.