Skip to content
Merged
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
17 changes: 17 additions & 0 deletions examples/emojis-async.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use ApiClients\Client\Github\AsyncClient;
use ApiClients\Client\Github\Resource\EmojiInterface;
use React\EventLoop\Factory;
use function ApiClients\Foundation\resource_pretty_print;

require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor/autoload.php';

$loop = Factory::create();
$client = AsyncClient::create($loop, require 'resolve_token.php');

$client->emojis()->subscribe(function (EmojiInterface $emoji) {
resource_pretty_print($emoji);
}, 'display_throwable');

$loop->run();
5 changes: 5 additions & 0 deletions src/AsyncClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public function whoami(): PromiseInterface
return $this->client->handle(new Command\UserCommand());
}

public function emojis(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\EmojisCommand()));
}

public function myOrganizations(): Observable
{
return unwrapObservableFromPromise($this->client->handle(new Command\MyOrganizationsCommand()));
Expand Down
2 changes: 2 additions & 0 deletions src/AsyncClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,7 @@ public function user(string $user): PromiseInterface;

public function whoami(): PromiseInterface;

public function emojis(): Observable;

public function myOrganizations(): Observable;
}
12 changes: 12 additions & 0 deletions src/CommandBus/Command/EmojisCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Command;

use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;

/**
* @Handler("ApiClients\Client\Github\CommandBus\Handler\EmojisHandler")
*/
final class EmojisCommand
{
}
59 changes: 59 additions & 0 deletions src/CommandBus/Handler/EmojisHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\EmojisCommand;
use ApiClients\Client\Github\Resource\EmojiInterface;
use ApiClients\Client\Github\Service\IteratePagesService;
use ApiClients\Foundation\Hydrator\Hydrator;
use React\Promise\PromiseInterface;
use Rx\Observable;
use function React\Promise\resolve;

final class EmojisHandler
{
/**
* @var IteratePagesService
*/
private $service;

/**
* @var Hydrator
*/
private $hydrator;

/**
* @param IteratePagesService $service
* @param Hydrator $hydrator
*/
public function __construct(IteratePagesService $service, Hydrator $hydrator)
{
$this->service = $service;
$this->hydrator = $hydrator;
}

/**
* @param EmojisCommand $command
* @return PromiseInterface
*/
public function handle(EmojisCommand $command): PromiseInterface
{
return resolve(
$this->service->iterate('emojis')
->flatMap(function ($emojis) {
$structuredEmojis = [];

foreach ($emojis as $name => $image) {
$structuredEmojis[] = [
'name' => $name,
'image' => $image,
];
}

return Observable::fromArray($structuredEmojis);
})->map(function ($emoji) {
return $this->hydrator->hydrate(EmojiInterface::HYDRATE_CLASS, $emoji);
})
);
}
}
13 changes: 13 additions & 0 deletions src/Resource/Async/Emoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Async;

use ApiClients\Client\Github\Resource\Emoji as BaseEmoji;

class Emoji extends BaseEmoji
{
public function refresh() : Emoji
{
throw new \Exception('TODO: create refresh method!');
}
}
9 changes: 9 additions & 0 deletions src/Resource/Async/EmptyEmoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Async;

use ApiClients\Client\Github\Resource\EmptyEmoji as BaseEmptyEmoji;

class EmptyEmoji extends BaseEmptyEmoji
{
}
38 changes: 38 additions & 0 deletions src/Resource/Emoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource;

use ApiClients\Foundation\Hydrator\Annotations\EmptyResource;
use ApiClients\Foundation\Resource\AbstractResource;

/**
* @EmptyResource("EmptyEmoji")
*/
abstract class Emoji extends AbstractResource implements EmojiInterface
{
/**
* @var string
*/
protected $name;

/**
* @var string
*/
protected $image;

/**
* @return string
*/
public function name() : string
{
return $this->name;
}

/**
* @return string
*/
public function image() : string
{
return $this->image;
}
}
20 changes: 20 additions & 0 deletions src/Resource/EmojiInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource;

use ApiClients\Foundation\Resource\ResourceInterface;

interface EmojiInterface extends ResourceInterface
{
const HYDRATE_CLASS = 'Emoji';

/**
* @return string
*/
public function name() : string;

/**
* @return string
*/
public function image() : string;
}
24 changes: 24 additions & 0 deletions src/Resource/EmptyEmoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource;

use ApiClients\Foundation\Resource\EmptyResourceInterface;

abstract class EmptyEmoji implements EmojiInterface, EmptyResourceInterface
{
/**
* @return string
*/
public function name() : string
{
return null;
}

/**
* @return string
*/
public function image() : string
{
return null;
}
}
19 changes: 19 additions & 0 deletions src/Resource/Sync/Emoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Sync;

use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
use ApiClients\Client\Github\Resource\Emoji as BaseEmoji;
use ApiClients\Client\Github\Resource\EmojiInterface;

class Emoji extends BaseEmoji
{
public function refresh() : Emoji
{
return $this->wait($this->handleCommand(
new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this)
)->then(function (EmojiInterface $emoji) {
return $emoji->refresh();
}));
}
}
9 changes: 9 additions & 0 deletions src/Resource/Sync/EmptyEmoji.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace ApiClients\Client\Github\Resource\Sync;

use ApiClients\Client\Github\Resource\EmptyEmoji as BaseEmptyEmoji;

class EmptyEmoji extends BaseEmptyEmoji
{
}
47 changes: 47 additions & 0 deletions tests/CommandBus/Handler/EmojisHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Github\CommandBus\Handler;

use ApiClients\Client\Github\CommandBus\Command\EmojisCommand;
use ApiClients\Client\Github\CommandBus\Handler\EmojisHandler;
use ApiClients\Client\Github\Resource\EmojiInterface;
use ApiClients\Client\Github\Service\IteratePagesService;
use ApiClients\Foundation\Hydrator\Hydrator;
use ApiClients\Tools\TestUtilities\TestCase;
use Rx\Observable;
use Rx\React\Promise;
use function ApiClients\Tools\Rx\unwrapObservableFromPromise;

final class EmojisHandlerTest extends TestCase
{
public function testCommand()
{
$emojis = [
'foo' => 'bar',
'bar' => 'foo',
];
$service = $this->prophesize(IteratePagesService::class);
$service->iterate('emojis')->shouldBeCalled()->willReturn(Observable::fromArray([$emojis]));

$hydrator = $this->prophesize(Hydrator::class);
$hydrator->hydrate(
EmojiInterface::HYDRATE_CLASS,
[
'name' => 'foo',
'image' => 'bar',
]
)->shouldBeCalled()->willReturn($this->prophesize(EmojiInterface::class)->reveal());
$hydrator->hydrate(
EmojiInterface::HYDRATE_CLASS,
[
'name' => 'bar',
'image' => 'foo',
]
)->shouldBeCalled()->willReturn($this->prophesize(EmojiInterface::class)->reveal());

$handler = new EmojisHandler($service->reveal(), $hydrator->reveal());
$emojiResources = $this->await(Promise::fromObservable(unwrapObservableFromPromise($handler->handle(new EmojisCommand()))->toArray()));

self::assertCount(2, $emojiResources);
}
}
23 changes: 23 additions & 0 deletions tests/Resource/Async/EmojiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Github\Resource\Async;

use ApiClients\Tools\ResourceTestUtilities\AbstractResourceTest;
use ApiClients\Client\Github\ApiSettings;
use ApiClients\Client\Github\Resource\Emoji;

class EmojiTest extends AbstractResourceTest
{
public function getSyncAsync() : string
{
return 'Async';
}
public function getClass() : string
{
return Emoji::class;
}
public function getNamespace() : string
{
return Apisettings::NAMESPACE;
}
}
18 changes: 18 additions & 0 deletions tests/Resource/Async/EmptyEmojiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Github\Resource\Async;

use ApiClients\Tools\ResourceTestUtilities\AbstractEmptyResourceTest;
use ApiClients\Client\Github\Resource\Async\EmptyEmoji;

final class EmptyEmojiTest extends AbstractEmptyResourceTest
{
public function getSyncAsync() : string
{
return 'Async';
}
public function getClass() : string
{
return EmptyEmoji::class;
}
}
23 changes: 23 additions & 0 deletions tests/Resource/Sync/EmojiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Github\Resource\Sync;

use ApiClients\Tools\ResourceTestUtilities\AbstractResourceTest;
use ApiClients\Client\Github\ApiSettings;
use ApiClients\Client\Github\Resource\Emoji;

class EmojiTest extends AbstractResourceTest
{
public function getSyncAsync() : string
{
return 'Sync';
}
public function getClass() : string
{
return Emoji::class;
}
public function getNamespace() : string
{
return Apisettings::NAMESPACE;
}
}
18 changes: 18 additions & 0 deletions tests/Resource/Sync/EmptyEmojiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace ApiClients\Tests\Client\Github\Resource\Sync;

use ApiClients\Tools\ResourceTestUtilities\AbstractEmptyResourceTest;
use ApiClients\Client\Github\Resource\Sync\EmptyEmoji;

final class EmptyEmojiTest extends AbstractEmptyResourceTest
{
public function getSyncAsync() : string
{
return 'Sync';
}
public function getClass() : string
{
return EmptyEmoji::class;
}
}
4 changes: 4 additions & 0 deletions yaml/emoji.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class: Emoji
properties:
name: string
image: string