|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Application\Api\Field\Mutation; |
|
6
|
|
|
|
|
7
|
|
|
use Application\Api\Helper; |
|
8
|
|
|
use Application\Enum\ChangeType; |
|
9
|
|
|
use Application\Enum\Site; |
|
10
|
|
|
use Application\Model\Card; |
|
11
|
|
|
use Application\Model\Change; |
|
12
|
|
|
use Application\Repository\ChangeRepository; |
|
13
|
|
|
use Ecodev\Felix\Api\Exception; |
|
14
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
|
15
|
|
|
use GraphQL\Type\Definition\Type; |
|
16
|
|
|
|
|
17
|
|
|
class SuggestUpdate implements FieldInterface |
|
18
|
|
|
{ |
|
19
|
1 |
|
public static function build(): iterable |
|
20
|
|
|
{ |
|
21
|
1 |
|
yield 'suggestUpdate' => fn () => [ |
|
22
|
1 |
|
'type' => Type::nonNull(_types()->getOutput(Change::class)), |
|
23
|
1 |
|
'description' => 'Suggest the update of an existing image', |
|
24
|
1 |
|
'args' => [ |
|
25
|
1 |
|
'id' => Type::nonNull(_types()->getId(Card::class)), |
|
26
|
1 |
|
'request' => Type::nonNull(Type::string()), |
|
27
|
1 |
|
], |
|
28
|
1 |
|
'resolve' => function ($root, array $args): Change { |
|
29
|
|
|
/** @var Site $site */ |
|
30
|
1 |
|
$site = $root['site']; |
|
31
|
|
|
|
|
32
|
1 |
|
$suggestion = $args['id']->getEntity(); |
|
33
|
1 |
|
$original = $suggestion->getOriginal(); |
|
34
|
|
|
|
|
35
|
1 |
|
if (!$original) { |
|
36
|
|
|
throw new Exception('An suggestion must have an original defined'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** @var ChangeRepository $changeRepository */ |
|
40
|
1 |
|
$changeRepository = _em()->getRepository(Change::class); |
|
41
|
1 |
|
$change = $changeRepository->getOrCreate(ChangeType::Update, $suggestion, $args['request'], $site); |
|
42
|
1 |
|
Helper::throwIfDenied($change, 'create'); |
|
43
|
|
|
|
|
44
|
1 |
|
if (!$change->getId()) { |
|
|
|
|
|
|
45
|
1 |
|
_em()->flush(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $change; |
|
49
|
1 |
|
}, |
|
50
|
1 |
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: