Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/post tweet after publishing video #260

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
"elasticsearch/elasticsearch": "^7.3",
"monolog/monolog": "^2.1",

"endclothing/prometheus_client_php": "^1.0"
"endclothing/prometheus_client_php": "^1.0",
"abraham/twitteroauth": "^2.0"
},
"require-dev": {
"ext-xdebug": "*",
Expand Down
139 changes: 138 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Mooc/Videos/Application/Create/VideoCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
use CodelyTv\Mooc\Videos\Domain\Video;
use CodelyTv\Mooc\Videos\Domain\VideoId;
use CodelyTv\Mooc\Videos\Domain\VideoRepository;
use CodelyTv\Mooc\Videos\Domain\VideoShare;
use CodelyTv\Mooc\Videos\Domain\VideoTitle;
use CodelyTv\Mooc\Videos\Domain\VideoType;
use CodelyTv\Shared\Domain\Bus\Event\EventBus;

final class VideoCreator
{
public function __construct(private VideoRepository $repository, private EventBus $bus)
public function __construct(private VideoRepository $repository, private VideoShare $videoShare, private EventBus $bus)
{
}

Expand All @@ -25,6 +26,8 @@ public function create(VideoId $id, VideoType $type, VideoTitle $title, VideoUrl

$this->repository->save($video);

$this->videoShare->post('New video '.$video->title()->value(). ' has been published. Check it out!');

$this->bus->publish(...$video->pullDomainEvents());
}
}
8 changes: 8 additions & 0 deletions src/Mooc/Videos/Domain/VideoShare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace CodelyTv\Mooc\Videos\Domain;

interface VideoShare
{
public function post(string $status);
}
37 changes: 37 additions & 0 deletions src/Mooc/Videos/Infrastructure/Share/Twitter/TwitterClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace CodelyTv\Mooc\Videos\Infrastructure\Share\Twitter;

use Abraham\TwitterOAuth\TwitterOAuth;
use CodelyTv\Mooc\Videos\Domain\VideoShare;

class TwitterClient implements VideoShare
{

private TwitterOAuth $connection;

public function __construct()
{
$twitter_consumer_key = $_SERVER['TWITTER_CONSUMER_KEY'];
$twitter_consumer_secret = $_SERVER['TWITTER_CONSUMER_SECRET'];
$twitter_access_token = $_SERVER['TWITTER_ACCESS_TOKEN'];
$twitter_access_token_secret = $_SERVER['TWITTER_ACCESS_TOKEN_SECRET'];

$this->connection = new TwitterOAuth (
$twitter_consumer_key,
$twitter_consumer_secret,
$twitter_access_token,
$twitter_access_token_secret
);
}

/**
* @param string $status
*
* @return array|object
*/
public function post(string $status)
{
return $this->connection->post("statuses/update", ["status" => $status]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace CodelyTv\Tests\Mooc\Videos\Infrastructure\Share\Twitter;

use CodelyTv\Mooc\Videos\Infrastructure\Share\Twitter\TwitterClient;
use PHPUnit\Framework\TestCase;

class TwitterClientTest extends TestCase
{
public function testTweetIsPublished()
{
$status = "Hola mundo";
$twitter_client = new TwitterClient();
$twitter_response = $twitter_client->post($status);
$this->assertIsObject($twitter_response);
$this->assertEquals("Hola mundo", $twitter_response->text);
}
}