Skip to content

Commit fe97723

Browse files
authored
Merge pull request #17 from php-api-clients/improvement-extended-refresh-generated-code-on-sync-resource
Handle the promise coming from the sync to async command and call refresh on the resulting object
2 parents cb99ede + 8ba7957 commit fe97723

File tree

4 files changed

+52
-10
lines changed

4 files changed

+52
-10
lines changed

src/FileGenerators/SyncClassGenerator.php

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
66
use ApiClients\Tools\ResourceGenerator\FileGeneratorInterface;
7+
use Doctrine\Common\Inflector\Inflector;
78
use PhpParser\BuilderFactory;
89
use PhpParser\Node;
910

@@ -18,12 +19,14 @@ public function generate(): Node
1819
{
1920
$classChunks = explode('\\', $this->yaml['class']);
2021
$className = array_pop($classChunks);
22+
$interfaceName = $className . 'Interface';
2123
$namespace = $this->yaml['src']['namespace'] . '\\' . static::NAMESPACE;
2224
if (count($classChunks) > 0) {
2325
$namespace .= '\\' . implode('\\', $classChunks);
2426
$namespace = str_replace('\\\\', '\\', $namespace);
2527
}
2628
$baseClass = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'];
29+
$interfaceFQName = $this->yaml['src']['namespace'] . '\\' . $this->yaml['class'] . 'Interface';
2730

2831
$factory = new BuilderFactory();
2932

@@ -40,16 +43,45 @@ public function generate(): Node
4043
'wait',
4144
[
4245
new Node\Expr\MethodCall(
43-
new Node\Expr\Variable('this'),
44-
'handleCommand',
46+
new Node\Expr\MethodCall(
47+
new Node\Expr\Variable('this'),
48+
'handleCommand',
49+
[
50+
new Node\Expr\New_(
51+
new Node\Name('BuildAsyncFromSyncCommand'),
52+
[
53+
new Node\Expr\ClassConstFetch(
54+
new Node\Name('self'),
55+
'HYDRATE_CLASS'
56+
),
57+
new Node\Expr\Variable('this'),
58+
]
59+
),
60+
]
61+
),
62+
'then',
4563
[
46-
new Node\Expr\New_(
47-
new Node\Name('BuildAsyncFromSyncCommand'),
64+
new Node\Expr\Closure(
4865
[
49-
new Node\Scalar\String_($this->yaml['class']),
50-
new Node\Expr\Variable('this'),
66+
'params' => [
67+
new Node\Param(
68+
Inflector::camelize($className),
69+
null,
70+
$interfaceName
71+
)
72+
],
73+
'stmts' => [
74+
new Node\Stmt\Return_(
75+
new Node\Expr\MethodCall(
76+
new Node\Expr\Variable(
77+
Inflector::camelize($className)
78+
),
79+
'refresh'
80+
)
81+
)
82+
],
5183
]
52-
),
84+
)
5385
]
5486
),
5587
]
@@ -60,6 +92,7 @@ public function generate(): Node
6092
return $factory->namespace($namespace)
6193
->addStmt($factory->use(BuildAsyncFromSyncCommand::class))
6294
->addStmt($factory->use($baseClass)->as('Base' . $className))
95+
->addStmt($factory->use($interfaceFQName))
6396
->addStmt($class)
6497
->getNode()
6598
;

tests/expected-app/src/Resources/Sync/Project.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
66
use Example\Client\Resource\Project as BaseProject;
7+
use Example\Client\Resource\ProjectInterface;
78

89
class Project extends BaseProject
910
{
1011
public function refresh() : Project
1112
{
12-
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project', $this)));
13+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ProjectInterface $project) {
14+
return $project->refresh();
15+
}));
1316
}
1417
}

tests/expected-app/src/Resources/Sync/Project/Build.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
66
use Example\Client\Resource\Project\Build as BaseBuild;
7+
use Example\Client\Resource\Project\BuildInterface;
78

89
class Build extends BaseBuild
910
{
1011
public function refresh() : Build
1112
{
12-
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Build', $this)));
13+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (BuildInterface $build) {
14+
return $build->refresh();
15+
}));
1316
}
1417
}

tests/expected-app/src/Resources/Sync/Project/Config.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
use ApiClients\Foundation\Hydrator\CommandBus\Command\BuildAsyncFromSyncCommand;
66
use Example\Client\Resource\Project\Config as BaseConfig;
7+
use Example\Client\Resource\Project\ConfigInterface;
78

89
class Config extends BaseConfig
910
{
1011
public function refresh() : Config
1112
{
12-
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand('Project\\Config', $this)));
13+
return $this->wait($this->handleCommand(new BuildAsyncFromSyncCommand(self::HYDRATE_CLASS, $this))->then(function (ConfigInterface $config) {
14+
return $config->refresh();
15+
}));
1316
}
1417
}

0 commit comments

Comments
 (0)