Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
papac committed Jan 28, 2025
1 parent 23c3597 commit e32dfc3
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
- run: docker run -p 21:21 -p 20:20 -p 12020:12020 -p 12021:12021 -p 12022:12022 -p 12023:12023 -p 12024:12024 -p 12025:12025 -e USER=$FTP_USER -e PASS=$FTP_PASSWORD -d --name ftp papacdev/vsftpd
- run: docker run -p 1080:1080 -p 1025:1025 -d --name maildev soulteary/maildev
- run: docker run -p 6379:6379 -d --name redis redis
- run: docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -e POSTGRES_PASSWORD=postgres -d postgis/postgis
- run: docker run -p 5432:5432 --name postgres -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=postgres -d postgis/postgis
- run: docker run -d -p 11300:11300 schickling/beanstalkd

- name: Cache Composer packages
Expand Down
61 changes: 47 additions & 14 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
version: "3"
volumes:
ftp_storage:

services:
db:
container_name: mysql
command: --default-authentication-plugin=mysql_native_password --max_allowed_packet=1073741824
image: mysql
mysql:
container_name: bowphp_mysql
image: mysql/mysql-server
ports:
- "3306:3306"
environment:
MYSQL_DATABASE: test
MYSQL_USERNAME: travis
MYSQL_DATABASE: test_db
MYSQL_USERNAME: root
MYSQL_ROOT_PASSWORD: password
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
postgres:
container_name: bowphp_postgres
image: postgis/postgis
ports:
- "5432:5432"
environment:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
ftp:
container_name: ftp-server
image: emilybache/vsftpd-server
container_name: bowphp_ftp
image: papacdev/vsftpd
ports:
- "21"
- "21:21"
- "20:20"
- "12020:12020"
- "12021:12021"
- "12022:12022"
- "12023:12023"
- "12024:12024"
- "12025:12025"
environment:
USER: bob
PASS: "12345"
volumes:
- "ftp_storage:/ftp/$USER"
mail:
container_name: mail
container_name: bowphp_mail
image: maildev/maildev
ports:
- "1025:25"
- "1080:80"

volumes:
ftp_storage:
beanstalkd:
container_name: bowphp_beanstalkd
image: schickling/beanstalkd
ports:
- "11300:11300"
redis:
container_name: bowphp_redis
image: redis
ports:
- "6379:6379"
memcached:
container_name: bowphp_memcached
image: memcached
command:
- --conn-limit=1024
- --memory-limit=64
- --threads=4
ports:
- "11211:11211"
2 changes: 1 addition & 1 deletion src/Mail/Adapters/SesAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function send(Envelop $envelop): bool

$email = [
'Destination' => [
'ToAddresses' => array_map(fn ($value) => $value[0] !== null ? $value[0] . ' <' . $value[1] . '>' : '<' . $value[1] . '>', $envelop->getTo()),
'ToAddresses' => array_map(fn($value) => $value[0] !== null ? $value[0] . ' <' . $value[1] . '>' : '<' . $value[1] . '>', $envelop->getTo()),
],
'Source' => $envelop->getFrom(),
'Envelop' => [
Expand Down
108 changes: 54 additions & 54 deletions src/Mail/Envelop.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Bow\Mail;

use Bow\View\View;
use Bow\Mail\Exception\MailException;
use Bow\Support\Str;
use Bow\View\View;
use InvalidArgumentException;
use Bow\Mail\Exception\MailException;

class Envelop
{
Expand Down Expand Up @@ -148,7 +148,7 @@ public function addHeader(string $key, string $value): void
*/
public function to(string|array $to): Envelop
{
$recipients = (array) $to;
$recipients = (array)$to;

foreach ($recipients as $to) {
$this->to[] = $this->formatEmail($to);
Expand All @@ -157,6 +157,31 @@ public function to(string|array $to): Envelop
return $this;
}

/**
* Format the email receiver
*
* @param string $email
* @return array
*/
private function formatEmail(string $email): array
{
/**
* Organization of the list of senders
*/
$name = null;
if (preg_match('/^(.+)\s+<(.*)>\z$/', $email, $matches)) {
array_shift($matches);
$name = $matches[0];
$email = $matches[1];
}

if (!Str::isMail($email)) {
throw new InvalidArgumentException("$email is not valid email.", E_USER_ERROR);
}

return [$name, $email];
}

/**
* Add an attachment file
*
Expand Down Expand Up @@ -238,6 +263,22 @@ public function html(string $html): Envelop
return $this->type($html, "text/html");
}

/**
* Add message body and set message type
*
* @param string $message
* @param string $type
* @return Envelop
*/
private function type(string $message, string $type): Envelop
{
$this->type = $type;

$this->message = $message;

return $this;
}

/**
* Add message body
*
Expand Down Expand Up @@ -342,16 +383,6 @@ public function getHeaders(): array
return $this->headers;
}

/**
* Get the list of receivers
*
* @return array
*/
public function getTo(): array
{
return $this->to;
}

/**
* Get the subject of the email
*
Expand Down Expand Up @@ -451,47 +482,6 @@ public function message(string $message, string $type = 'text/html'): void
$this->setMessage($message, $type);
}

/**
* Format the email receiver
*
* @param string $email
* @return array
*/
private function formatEmail(string $email): array
{
/**
* Organization of the list of senders
*/
$name = null;
if (preg_match('/^(.+)\s+<(.*)>\z$/', $email, $matches)) {
array_shift($matches);
$name = $matches[0];
$email = $matches[1];
}

if (!Str::isMail($email)) {
throw new InvalidArgumentException("$email is not valid email.", E_USER_ERROR);
}

return [$name, $email];
}

/**
* Add message body and set message type
*
* @param string $message
* @param string $type
* @return Envelop
*/
private function type(string $message, string $type): Envelop
{
$this->type = $type;

$this->message = $message;

return $this;
}

public function composeTo()
{
$to = '';
Expand All @@ -505,4 +495,14 @@ public function composeTo()
$this->write('RCPT TO: ' . $to, 250);
}
}

/**
* Get the list of receivers
*
* @return array
*/
public function getTo(): array
{
return $this->to;
}
}
36 changes: 36 additions & 0 deletions src/Messaging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class User extends Model

- `mail` : Envoi par email
- `database` : Stockage en base de données
- `sms` : Envoi par SMS avec Twilio
- `slack` : Envoi par Slack
- `telegram` : Envoi par Telegram
- Possibilité d'ajouter des canaux personnalisés

## Bonnes pratiques
Expand All @@ -99,3 +102,36 @@ class User extends Model
2. Utilisez les files d'attente pour les notifications non urgentes
3. Personnalisez les canaux en fonction du contexte
4. Utilisez les vues pour les templates d'emails

## Exemple de configuration

```mermaid
sequenceDiagram
participant User as Utilisateur
participant Model as Modèle (User)
participant Message as WelcomeMessage
participant Mail as Canal Email
participant DB as Canal Database
participant Services as Services (SMTP/BDD)
Note over User,Services: Envoi d'une notification de bienvenue
User->>Model: sendMessage(new WelcomeMessage("Bienvenue!"))
Model->>Message: process(context)
Message->>Message: channels(context)
par Canal Email
Message->>Mail: toMail(context)
Mail->>Services: Envoie via SMTP
Services-->>User: Email reçu
and Canal Database
Message->>DB: toDatabase(context)
DB->>Services: Sauvegarde notification
Services-->>User: Notification in-app
end
Note over User,Services: Envoi asynchrone
User->>Model: setMessageQueue(new WelcomeMessage())
Model->>Services: Ajout à la file d'attente
Services-->>Model: Confirmation
```
4 changes: 2 additions & 2 deletions tests/Config/TestingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct()
* @param array $configurations
* @return void
*/
public static function withConfigurations(array $configurations)
public static function withConfigurations(array $configurations): void
{
KernelTesting::withConfigurations($configurations);
}
Expand All @@ -32,7 +32,7 @@ public static function withConfigurations(array $configurations)
* @param array $middlewares
* @return void
*/
public static function withMiddlewares(array $middlewares)
public static function withMiddlewares(array $middlewares): void
{
KernelTesting::withMiddlewares($middlewares);
}
Expand Down
24 changes: 12 additions & 12 deletions tests/Messaging/MessagingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Bow\Tests\Messaging;

use Bow\View\View;
use Bow\Mail\Envelop;
use Bow\Database\Barry\Model;
use PHPUnit\Framework\TestCase;
use Bow\Mail\Envelop;
use Bow\Tests\Config\TestingConfiguration;
use Bow\Tests\Messaging\Stubs\TestMessage;
use PHPUnit\Framework\MockObject\MockObject;
use Bow\Tests\Messaging\Stubs\TestNotifiableModel;
use Bow\View\View;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MessagingTest extends TestCase
{
Expand All @@ -26,14 +26,6 @@ public static function setUpBeforeClass(): void
View::configure($config["view"]);
}

protected function setUp(): void
{
parent::setUp();

$this->context = new TestNotifiableModel();
$this->message = $this->createMock(TestMessage::class);
}

public function test_can_send_message_synchronously(): void
{
$this->message->expects($this->once())
Expand Down Expand Up @@ -123,4 +115,12 @@ public function test_message_can_send_to_telegram(): void
$this->assertEquals('Test Telegram message', $data['message']);
$this->assertEquals('HTML', $data['parse_mode']);
}

protected function setUp(): void
{
parent::setUp();

$this->context = new TestNotifiableModel();
$this->message = $this->createMock(TestMessage::class);
}
}
Loading

0 comments on commit e32dfc3

Please sign in to comment.