From 641b83ba1f372ae0a3de117e5298b3f591b8a44e Mon Sep 17 00:00:00 2001 From: santiagoWilli Date: Wed, 27 Jul 2022 01:15:02 +0200 Subject: [PATCH 1/3] feat: new Shared/Domain/Notification notificator --- src/Shared/Domain/Notification/Notification.php | 14 ++++++++++++++ src/Shared/Domain/Notification/Notificator.php | 10 ++++++++++ .../Notification/TwitterNotificator.php | 15 +++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 src/Shared/Domain/Notification/Notification.php create mode 100644 src/Shared/Domain/Notification/Notificator.php create mode 100644 src/Shared/Infrastructure/Notification/TwitterNotificator.php diff --git a/src/Shared/Domain/Notification/Notification.php b/src/Shared/Domain/Notification/Notification.php new file mode 100644 index 000000000..417c6f523 --- /dev/null +++ b/src/Shared/Domain/Notification/Notification.php @@ -0,0 +1,14 @@ +text; + } +} diff --git a/src/Shared/Domain/Notification/Notificator.php b/src/Shared/Domain/Notification/Notificator.php new file mode 100644 index 000000000..b2c1328a7 --- /dev/null +++ b/src/Shared/Domain/Notification/Notificator.php @@ -0,0 +1,10 @@ + Date: Wed, 27 Jul 2022 01:30:23 +0200 Subject: [PATCH 2/3] feat: Shared/Infrastructure/Notification/TwitterNotificator --- composer.json | 3 +- composer.lock | 140 +++++++++++++++++- .../Notification/TwitterNotificator.php | 19 ++- 3 files changed, 155 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index ea800fcce..bdd5979aa 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "elasticsearch/elasticsearch": "^7", "monolog/monolog": "^3", - "endclothing/prometheus_client_php": "^1" + "endclothing/prometheus_client_php": "^1", + "abraham/twitteroauth": "^4.0" }, "require-dev": { "ext-xdebug": "*", diff --git a/composer.lock b/composer.lock index f12724bfe..a458c0ffe 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,70 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "c096ab9ab7ca0c43b0244f7dcbb55400", + "content-hash": "57d37923821ec1b1fbe8c2439e24812f", "packages": [ + { + "name": "abraham/twitteroauth", + "version": "4.0.0", + "source": { + "type": "git", + "url": "https://github.com/abraham/twitteroauth.git", + "reference": "55dcddb3bb8e87325acab2f219c08750b68efb74" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/abraham/twitteroauth/zipball/55dcddb3bb8e87325acab2f219c08750b68efb74", + "reference": "55dcddb3bb8e87325acab2f219c08750b68efb74", + "shasum": "" + }, + "require": { + "composer/ca-bundle": "^1.2", + "ext-curl": "*", + "php": "^7.4 || ^8.0 || ^8.1" + }, + "require-dev": { + "php-vcr/php-vcr": "^1", + "php-vcr/phpunit-testlistener-vcr": "dev-php-8", + "phpmd/phpmd": "^2", + "phpunit/phpunit": "^8 || ^9", + "rector/rector": "^0.12.19", + "squizlabs/php_codesniffer": "^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Abraham\\TwitterOAuth\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Abraham Williams", + "email": "abraham@abrah.am", + "homepage": "https://abrah.am", + "role": "Developer" + } + ], + "description": "The most popular PHP library for use with the Twitter OAuth REST API.", + "homepage": "https://twitteroauth.com", + "keywords": [ + "Twitter API", + "Twitter oAuth", + "api", + "oauth", + "rest", + "social", + "twitter" + ], + "support": { + "issues": "https://github.com/abraham/twitteroauth/issues", + "source": "https://github.com/abraham/twitteroauth" + }, + "time": "2022-04-02T16:47:22+00:00" + }, { "name": "brick/math", "version": "0.9.3", @@ -66,6 +128,82 @@ ], "time": "2021-08-15T20:50:18+00:00" }, + { + "name": "composer/ca-bundle", + "version": "1.3.3", + "source": { + "type": "git", + "url": "https://github.com/composer/ca-bundle.git", + "reference": "30897edbfb15e784fe55587b4f73ceefd3c4d98c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/composer/ca-bundle/zipball/30897edbfb15e784fe55587b4f73ceefd3c4d98c", + "reference": "30897edbfb15e784fe55587b4f73ceefd3c4d98c", + "shasum": "" + }, + "require": { + "ext-openssl": "*", + "ext-pcre": "*", + "php": "^5.3.2 || ^7.0 || ^8.0" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.55", + "psr/log": "^1.0", + "symfony/phpunit-bridge": "^4.2 || ^5", + "symfony/process": "^2.5 || ^3.0 || ^4.0 || ^5.0 || ^6.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\CaBundle\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "Lets you find a path to the system CA bundle, and includes a fallback to the Mozilla CA bundle.", + "keywords": [ + "cabundle", + "cacert", + "certificate", + "ssl", + "tls" + ], + "support": { + "irc": "irc://irc.freenode.org/composer", + "issues": "https://github.com/composer/ca-bundle/issues", + "source": "https://github.com/composer/ca-bundle/tree/1.3.3" + }, + "funding": [ + { + "url": "https://packagist.com", + "type": "custom" + }, + { + "url": "https://github.com/composer", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/composer/composer", + "type": "tidelift" + } + ], + "time": "2022-07-20T07:14:26+00:00" + }, { "name": "doctrine/cache", "version": "2.2.0", diff --git a/src/Shared/Infrastructure/Notification/TwitterNotificator.php b/src/Shared/Infrastructure/Notification/TwitterNotificator.php index b01b0463d..f6a6561ba 100644 --- a/src/Shared/Infrastructure/Notification/TwitterNotificator.php +++ b/src/Shared/Infrastructure/Notification/TwitterNotificator.php @@ -2,14 +2,23 @@ declare(strict_types=1); -namespace CodelyTv\Shared\Infrastructure; +namespace CodelyTv\Shared\Infrastructure\Notification; -use CodelyTv\Shared\Domain\RandomNumberGenerator; +use Abraham\TwitterOAuth\TwitterOAuth; +use CodelyTv\Shared\Domain\Notification\Notification; +use CodelyTv\Shared\Domain\Notification\Notificator; -final class PhpRandomNumberGenerator implements RandomNumberGenerator +final class TwitterNotificator implements Notificator { - public function generate(): int + private readonly TwitterOAuth $client; + + public function __construct(string $key, string $secret, string $accessToken, string $accessTokenSecret) + { + $this->client = new TwitterOAuth($key, $secret, $accessToken, $accessTokenSecret); + } + + public function notify(Notification $notification): void { - return random_int(1, 5); + $this->client->post("statuses/update", ["status" => $notification->getText()]); } } From 68075c8b5044410543025694f2c0d2ca7bc7f975 Mon Sep 17 00:00:00 2001 From: santiagoWilli Date: Wed, 27 Jul 2022 01:51:35 +0200 Subject: [PATCH 3/3] feat: notify when a video is created --- .../Application/Create/VideoCreator.php | 11 +++++++--- .../Videos/Domain/VideoNotificationSender.php | 20 +++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 src/Mooc/Videos/Domain/VideoNotificationSender.php diff --git a/src/Mooc/Videos/Application/Create/VideoCreator.php b/src/Mooc/Videos/Application/Create/VideoCreator.php index 91180306b..ea0eae6c4 100644 --- a/src/Mooc/Videos/Application/Create/VideoCreator.php +++ b/src/Mooc/Videos/Application/Create/VideoCreator.php @@ -8,6 +8,7 @@ use CodelyTv\Mooc\Shared\Domain\Videos\VideoUrl; use CodelyTv\Mooc\Videos\Domain\Video; use CodelyTv\Mooc\Videos\Domain\VideoId; +use CodelyTv\Mooc\Videos\Domain\VideoNotificationSender; use CodelyTv\Mooc\Videos\Domain\VideoRepository; use CodelyTv\Mooc\Videos\Domain\VideoTitle; use CodelyTv\Mooc\Videos\Domain\VideoType; @@ -15,9 +16,11 @@ final class VideoCreator { - public function __construct(private readonly VideoRepository $repository, private readonly EventBus $bus) - { - } + public function __construct( + private readonly VideoRepository $repository, + private readonly EventBus $bus, + private readonly ?VideoNotificationSender $notificationSender = null + ) {} public function create(VideoId $id, VideoType $type, VideoTitle $title, VideoUrl $url, CourseId $courseId): void { @@ -25,6 +28,8 @@ public function create(VideoId $id, VideoType $type, VideoTitle $title, VideoUrl $this->repository->save($video); + $this->notificationSender?->videoCreated($video); + $this->bus->publish(...$video->pullDomainEvents()); } } diff --git a/src/Mooc/Videos/Domain/VideoNotificationSender.php b/src/Mooc/Videos/Domain/VideoNotificationSender.php new file mode 100644 index 000000000..534f4379e --- /dev/null +++ b/src/Mooc/Videos/Domain/VideoNotificationSender.php @@ -0,0 +1,20 @@ +notificator->notify( + new Notification("A new video has been published: " . $video->title()->value() . ". Check it out!") + ); + } +}