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\Model\Card; |
10
|
|
|
use Application\Model\Change; |
11
|
|
|
use Application\Model\User; |
12
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
13
|
|
|
use Ecodev\Felix\Api\Field\FieldInterface; |
14
|
|
|
use Exception; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
|
17
|
|
|
class AcceptChange implements FieldInterface |
18
|
|
|
{ |
19
|
3 |
|
public static function build(): iterable |
20
|
|
|
{ |
21
|
1 |
|
yield 'acceptChange' => fn () => [ |
22
|
1 |
|
'type' => _types()->getOutput(Card::class), |
23
|
1 |
|
'description' => 'Accept the change and return the modified Image, unless if it has been deleted', |
24
|
1 |
|
'args' => [ |
25
|
1 |
|
'id' => Type::nonNull(_types()->getId(Change::class)), |
26
|
1 |
|
], |
27
|
1 |
|
'resolve' => function ($root, array $args): ?Card { |
28
|
|
|
/** @var Change $change */ |
29
|
3 |
|
$change = $args['id']->getEntity(); |
30
|
3 |
|
Helper::throwIfDenied($change, 'update'); |
31
|
|
|
|
32
|
3 |
|
$image = null; |
33
|
3 |
|
switch ($change->getType()) { |
34
|
3 |
|
case ChangeType::Create: |
35
|
1 |
|
$image = $change->getSuggestion(); |
36
|
1 |
|
$image->setOwner(User::getCurrent()); |
37
|
|
|
|
38
|
1 |
|
$changeSet = ['need some value here']; |
39
|
1 |
|
$fakeEvent = new PreUpdateEventArgs($image, _em(), $changeSet); |
40
|
1 |
|
$image->timestampUpdate($fakeEvent); |
41
|
|
|
|
42
|
1 |
|
break; |
43
|
2 |
|
case ChangeType::Update: |
44
|
1 |
|
$image = $change->getOriginal(); |
45
|
1 |
|
$change->getSuggestion()->copyInto($image); |
|
|
|
|
46
|
1 |
|
_em()->remove($change->getSuggestion()); |
47
|
|
|
|
48
|
1 |
|
break; |
49
|
1 |
|
case ChangeType::Delete: |
50
|
1 |
|
$original = $change->getOriginal(); |
51
|
|
|
|
52
|
|
|
// Trigger proxy loading, so the image on disk will be able to be deleted after the flush |
53
|
1 |
|
$original->getPath(); |
54
|
|
|
|
55
|
1 |
|
_em()->remove($original); |
56
|
|
|
|
57
|
1 |
|
break; |
58
|
|
|
default: |
59
|
|
|
throw new Exception('Unsupported change type: ' . $change->getType()->value); |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
_em()->remove($change); |
63
|
3 |
|
_em()->flush(); |
64
|
|
|
|
65
|
3 |
|
return $image; |
66
|
1 |
|
}, |
67
|
1 |
|
]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|