Skip to content

Commit

Permalink
Merge pull request #1 from PhantPHP/Init
Browse files Browse the repository at this point in the history
Hello world!
  • Loading branch information
lennyrouanet committed Jan 13, 2023
2 parents cb768e6 + 2fde138 commit 016cef6
Show file tree
Hide file tree
Showing 37 changed files with 1,197 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Dependencies
composer.lock
.package/*

# Test
.coverage*
.phpunit*

# Dev
.DS_Store
.nova/*
.php-cs-fixer.cache
57 changes: 56 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,56 @@
# notifier
# Notifier

## Requirements

PHP >= 8.1


## Install

`composer require phant/notifier`

## Usages

```php
use Phant\Notifier\Service\Handler as NotifierHandler;
use Phant\Notifier\Entity\Notification\Action as NotificationAction;
use Phant\Notifier\Entity\Notification\Type as NotificationType;
use Phant\Notifier\Factory\Notification as NotificationFactory;
use Phant\Notifier\Factory\Notification\Content as NotificationContentFactory;
use Phant\Notifier\Entity\Transport\Channel;
use Phant\Notifier\Entity\Transport\Collection as NotifierTransportCollection;
use Phant\Notifier\Entity\Transport\Email as NotifierTransportEmail;
use App\EmailBuilder;
use App\EmailSender;

$notifierHandler = new NotifierHandler(
(new NotifierTransportCollection())
->add(
new NotifierTransportEmail(
new EmailBuilder(),
new EmailSender(),
'user@domain.ext',
'Best app'
)
)
);

$notifierHandler->dispatch(
NotificationFactory::make(
NotificationType::Information,
NotificationContentFactory::make(
'Hello world!',
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
[
'Foo' => 'Bar'
]
),
new NotificationAction(
'https://domain.ext/path',
'Action'
),
'john.doe@domain.ext'
Channel::Email
)
);
```
33 changes: 33 additions & 0 deletions component/Entity/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity;

use Phant\Error\NotCompliant;
use Phant\DataStructure\Web\EmailAddressAndName;
use Phant\Notifier\Entity\Notification\Action;
use Phant\Notifier\Entity\Notification\Content;
use Phant\Notifier\Entity\Notification\Recipient;
use Phant\Notifier\Entity\Notification\Type;
use Phant\Notifier\Entity\Transport\Channel;

class Notification
{
public function __construct(
public readonly Type $type,
public readonly Content $content,
public readonly null|Action $action,
public readonly Recipient $recipient,
public readonly Channel $channel
) {
$this->isCompliant();
}

protected function isCompliant(
): void {
if (!$this->recipient->isCompliantWithChannel($this->channel)) {
throw new NotCompliant('Recipient must be compliant with channel');
}
}
}
14 changes: 14 additions & 0 deletions component/Entity/Notification/Action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification;

class Action
{
public function __construct(
public readonly string $url,
public readonly string $label
) {
}
}
19 changes: 19 additions & 0 deletions component/Entity/Notification/Content.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification;

use Phant\Notifier\Entity\Notification\Content\Body;
use Phant\Notifier\Entity\Notification\Content\Metadatas;
use Phant\Notifier\Entity\Notification\Content\Subject;

class Content
{
public function __construct(
public readonly Subject $subject,
public readonly ?Body $body,
public readonly ?Metadatas $metadatas
) {
}
}
9 changes: 9 additions & 0 deletions component/Entity/Notification/Content/Body.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification\Content;

class Body extends \Phant\DataStructure\Abstract\Value\Varchar
{
}
76 changes: 76 additions & 0 deletions component/Entity/Notification/Content/Metadatas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification\Content;

use Phant\Error\NotCompliant;
use Phant\Error\NotFound;

class Metadatas
{
protected array $items;

public function __construct()
{
}

public function add(
string $key,
int|float|string|bool|array $value
): self {
self::cleanKey($key);

$this->items[$key] = $value;

return $this;
}

public function remove(
string $key
): self {
self::cleanKey($key);

if (!isset($this->items[$key])) {
throw new NotFound('Metadada not exist');
}

unset($this->items[$key]);

return $this;
}

public function get(
string $key
): int|float|string|bool|array {
self::cleanKey($key);

if (!isset($this->items[$key])) {
throw new NotFound('Metadada not exist');
}

return $this->items[$key];
}

public function iterate(): \Generator
{
foreach ($this->items as $key => $value) {
yield $key => $value;
}
}

public function toArray(): array
{
return $this->items;
}

private static function cleanKey(
string &$key
): void {
$key = trim($key);

if (!$key) {
throw new NotCompliant('Incorrect key');
}
}
}
9 changes: 9 additions & 0 deletions component/Entity/Notification/Content/Subject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification\Content;

class Subject extends \Phant\DataStructure\Abstract\Value\Varchar
{
}
36 changes: 36 additions & 0 deletions component/Entity/Notification/Recipient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification;

use Phant\DataStructure\Web\EmailAddress;
use Phant\DataStructure\Web\EmailAddressAndName;
use Phant\Notifier\Entity\Transport\Channel;

class Recipient
{
public function __construct(
public readonly string|EmailAddress|EmailAddressAndName $value
) {
}

public function isCompliantWithChannel(
Channel $channel
): bool {
switch ($channel) {
case Channel::Email:
if (is_a($this->value, EmailAddress::class)) {
return true;
}
if (is_a($this->value, EmailAddressAndName::class)) {
return true;
}

return false;
break;
}

return true;
}
}
16 changes: 16 additions & 0 deletions component/Entity/Notification/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Notification;

enum Type: string
{
case Error = 'error';

case Warning = 'warning';

case Confirmation = 'confirmation';

case Information = 'information';
}
22 changes: 22 additions & 0 deletions component/Entity/Transport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity;

use Phant\Notifier\Entity\Transport\Channel;

abstract class Transport
{
abstract public function dispatch(
Notification $notification
): bool;

abstract public function getChannel(): Channel;

public function canBeTransported(
Notification $notification
): bool {
return $notification->channel == $this->getChannel();
}
}
18 changes: 18 additions & 0 deletions component/Entity/Transport/Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Transport;

enum Channel: string
{
case Browser = 'browser';

case Chat = 'chat';

case Email = 'email';

case Push = 'push';

case Sms = 'sms';
}
16 changes: 16 additions & 0 deletions component/Entity/Transport/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Phant\Notifier\Entity\Transport;

use Phant\Notifier\Entity\Transport;

final class Collection extends \Phant\DataStructure\Abstract\Collection
{
public function add(
Transport $item
): self {
return parent::addItem($item);
}
}
Loading

0 comments on commit 016cef6

Please sign in to comment.