-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from PhantPHP/Init
Hello world!
- Loading branch information
Showing
37 changed files
with
1,197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.