unil-lettres /
dilps-tiresias
| 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 = ['fakeProperty' => ['foo' => 'bar']]; |
|||
| 39 | 1 | $fakeEvent = new PreUpdateEventArgs($image, _em(), $changeSet); |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 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); |
|||
|
0 ignored issues
–
show
It seems like
$image can also be of type null; however, parameter $original of Application\Model\Card::copyInto() does only seem to accept Application\Model\Card, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 46 | 1 | _em()->remove($change->getSuggestion()); |
|||
|
0 ignored issues
–
show
It seems like
$change->getSuggestion() can also be of type null; however, parameter $object of Doctrine\ORM\EntityManager::remove() does only seem to accept object, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 |