Skip to content

Commit 453f3d4

Browse files
committed
Adding logged/right/failwith annotation to junction tables
1 parent ded5356 commit 453f3d4

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
4+
namespace TheCodingMachine\FluidSchema;
5+
6+
7+
use function addslashes;
8+
use function var_export;
9+
10+
class TdbmFluidJunctionTableGraphqlOptions
11+
{
12+
/**
13+
* @var TdbmFluidJunctionTableOptions
14+
*/
15+
private $tdbmFluidJunctionTableOptions;
16+
/**
17+
* @var TdbmFluidTable
18+
*/
19+
private $tdbmFluidTable;
20+
21+
public function __construct(TdbmFluidJunctionTableOptions $tdbmFluidJunctionTableOptions, TdbmFluidTable $tdbmFluidTable)
22+
{
23+
$this->tdbmFluidJunctionTableOptions = $tdbmFluidJunctionTableOptions;
24+
$this->tdbmFluidTable = $tdbmFluidTable;
25+
}
26+
27+
public function logged(bool $mustBeLogged = true): self
28+
{
29+
if ($mustBeLogged) {
30+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
31+
} else {
32+
$this->removeAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Logged');
33+
}
34+
return $this;
35+
}
36+
37+
public function right(string $rightName): self
38+
{
39+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Right', '(name="'.addslashes($rightName).'")');
40+
return $this;
41+
}
42+
43+
public function failWith($value): self
44+
{
45+
$this->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\FailWith', '('.var_export($value, true).')');
46+
return $this;
47+
}
48+
49+
public function addAnnotation(string $annotation, string $content = '', bool $replaceExisting = true): self
50+
{
51+
$this->tdbmFluidTable->addAnnotation($annotation, $content, $replaceExisting);
52+
return $this;
53+
}
54+
55+
public function removeAnnotation(string $annotation): self
56+
{
57+
$this->tdbmFluidTable->removeAnnotation($annotation);
58+
return $this;
59+
}
60+
61+
public function endGraphql(): TdbmFluidJunctionTableOptions
62+
{
63+
return $this->tdbmFluidJunctionTableOptions;
64+
}
65+
}

src/TdbmFluidJunctionTableOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public function __construct(TdbmFluidTable $tdbmFluidTable)
2020
$this->tdbmFluidTable = $tdbmFluidTable;
2121
}
2222

23-
public function graphql(): void
23+
public function graphql(): TdbmFluidJunctionTableGraphqlOptions
2424
{
2525
$this->tdbmFluidTable->addAnnotation('TheCodingMachine\\GraphQLite\\Annotations\\Field');
26+
return new TdbmFluidJunctionTableGraphqlOptions($this, $this->tdbmFluidTable);
2627
}
2728
}

src/TdbmFluidTable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public function addAnnotation(string $annotation, string $content = '', bool $re
131131
return $this;
132132
}
133133

134+
public function removeAnnotation(string $annotation): self
135+
{
136+
$comment = $this->getComment()->removeAnnotation($annotation);
137+
$this->saveComment($comment);
138+
return $this;
139+
}
140+
134141
public function getDbalTable(): Table
135142
{
136143
return $this->fluidTable->getDbalTable();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace TheCodingMachine\FluidSchema;
4+
5+
use Doctrine\DBAL\Schema\Schema;
6+
use Doctrine\DBAL\Types\Type;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class TdbmFluidJunctionTableGraphqlOptionsTest extends TestCase
10+
{
11+
public function testGraphql()
12+
{
13+
$schema = new Schema();
14+
$fluid = new TdbmFluidSchema($schema);
15+
16+
$fluid->table('posts')->uuid();
17+
$fluid->table('users')->uuid();
18+
19+
$junctionTableOptions = $fluid->junctionTable('posts', 'users');
20+
$graphqlOptions = $junctionTableOptions->graphql();
21+
22+
$graphqlOptions->logged(true)
23+
->right('CAN_EDIT')
24+
->failWith(null);
25+
26+
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field
27+
@TheCodingMachine\GraphQLite\Annotations\Logged
28+
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
29+
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts_users')->getOptions()['comment']);
30+
31+
$graphqlOptions->logged(false);
32+
33+
$this->assertSame("\n@TheCodingMachine\GraphQLite\Annotations\Field
34+
@TheCodingMachine\GraphQLite\Annotations\Right(name=\"CAN_EDIT\")
35+
@TheCodingMachine\GraphQLite\Annotations\FailWith(NULL)", $schema->getTable('posts_users')->getOptions()['comment']);
36+
37+
$this->assertSame($junctionTableOptions, $graphqlOptions->endGraphql());
38+
}
39+
}

0 commit comments

Comments
 (0)