Failed Conditions
Push — master ( 27554e...857869 )
by Luca
09:08
created

AcceptChange::build()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 48
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 4.0005

Importance

Changes 0
Metric Value
eloc 33
c 0
b 0
f 0
dl 0
loc 48
ccs 30
cts 31
cp 0.9677
rs 9.392
cc 4
nc 1
nop 0
crap 4.0005
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Api\Field\Mutation;
6
7
use Application\Api\Helper;
8
use Application\Model\Card;
9
use Application\Model\Change;
10
use Application\Model\User;
11
use Doctrine\ORM\Event\PreUpdateEventArgs;
12
use Ecodev\Felix\Api\Field\FieldInterface;
13
use Exception;
14
use GraphQL\Type\Definition\Type;
15
16
class AcceptChange implements FieldInterface
17
{
18 3
    public static function build(): array
19
    {
20 1
        return [
21 1
            'name' => 'acceptChange',
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 (array $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
                    case Change::TYPE_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
                    case Change::TYPE_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
                    case Change::TYPE_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());
60
                }
61
62 3
                _em()->remove($change);
63 3
                _em()->flush();
64
65 3
                return $image;
66 1
            },
67 1
        ];
68
    }
69
}
70