Failed Conditions
Push — master ( b1d4df...5a9cf3 )
by David
04:05
created

AcceptChange::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 4.0004

Importance

Changes 0
Metric Value
eloc 32
c 0
b 0
f 0
dl 0
loc 47
ccs 32
cts 33
cp 0.9697
rs 9.408
cc 4
nc 1
nop 0
crap 4.0004
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);
0 ignored issues
show
Bug introduced by
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 ignore-type  annotation

45
                        $change->getSuggestion()->copyInto(/** @scrutinizer ignore-type */ $image);
Loading history...
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