Skip to content

Handle the promise coming from the sync to async command and call refresh on the resulting object #17

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

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
47 changes: 40 additions & 7 deletions src/FileGenerators/SyncClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
use Doctrine\Common\Inflector\Inflector;
use PhpParser\BuilderFactory;
use PhpParser\Node;

Expand All @@ -18,12 +19,14 @@ public function generate(): Node
{
$classChunks = explode('\\', $this->yaml['class']);
$className = array_pop($classChunks);
$interfaceName = $className . 'Interface';
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE;
if (count($classChunks) > 0) {
$namespace .= '\\' . implode('\\', $classChunks);
$namespace = str_replace('\\\\', '\\', $namespace);
}
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'];
$interfaceFQName = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'] . 'Interface';

$factory = new BuilderFactory();

Expand All @@ -40,16 +43,45 @@ public function generate(): Node
'wait',
[
new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
'handleCommand',
new Node\Expr\MethodCall(
new Node\Expr\Variable('this'),
'handleCommand',
[
new Node\Expr\New_(
new Node\Name('BuildAsyncFromSyncCommand'),
[
new Node\Expr\ClassConstFetch(
new Node\Name('self'),
'HYDRATE_CLASS'
),
new Node\Expr\Variable('this'),
]
),
]
),
'then',
[
new Node\Expr\New_(
new Node\Name('BuildAsyncFromSyncCommand'),
new Node\Expr\Closure(
[
new Node\Scalar\String_($this->yaml['class']),
new Node\Expr\Variable('this'),
'params' => [
new Node\Param(
Inflector::camelize($className),
null,
$interfaceName
)
],
'stmts' => [
new Node\Stmt\Return_(
new Node\Expr\MethodCall(
new Node\Expr\Variable(
Inflector::camelize($className)
),
'refresh'
)
)
],
]
),
)
]
),
]
Expand All @@ -60,6 +92,7 @@ public function generate(): Node
return $factory->namespace($namespace)
->addStmt($factory->use(BuildAsyncFromSyncCommand::class))
->addStmt($factory->use($baseClass)->as('Base' . $className))
->addStmt($factory->use($interfaceFQName))
->addStmt($class)
->getNode()
;
Expand Down
5 changes: 4 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
use Example\Client\Resource\Project as BaseProject;
use Example\Client\Resource\ProjectInterface;

class Project extends BaseProject
{
public function refresh() : Project
{
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project', $this)));
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ProjectInterface $project) {
return $project->refresh();
}));
}
}
5 changes: 4 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project/Build.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
use Example\Client\Resource\Project\Build as BaseBuild;
use Example\Client\Resource\Project\BuildInterface;

class Build extends BaseBuild
{
public function refresh() : Build
{
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Build', $this)));
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (BuildInterface $build) {
return $build->refresh();
}));
}
}
5 changes: 4 additions & 1 deletion tests/expected-app/src/Resources/Sync/Project/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
use Example\Client\Resource\Project\Config as BaseConfig;
use Example\Client\Resource\Project\ConfigInterface;

class Config extends BaseConfig
{
public function refresh() : Config
{
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Config', $this)));
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ConfigInterface $config) {
return $config->refresh();
}));
}
}