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

SuggestUpdate::build()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 3.0009

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 30
ccs 20
cts 21
cp 0.9524
rs 9.6333
cc 3
nc 1
nop 0
crap 3.0009
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\Repository\ChangeRepository;
11
use Ecodev\Felix\Api\Exception;
12
use Ecodev\Felix\Api\Field\FieldInterface;
13
use GraphQL\Type\Definition\Type;
14
15
class SuggestUpdate implements FieldInterface
16
{
17 1
    public static function build(): array
18
    {
19 1
        return [
20 1
            'name' => 'suggestUpdate',
21 1
            'type' => Type::nonNull(_types()->getOutput(Change::class)),
22 1
            'description' => 'Suggest the update of an existing image',
23 1
            'args' => [
24 1
                'id' => Type::nonNull(_types()->getId(Card::class)),
25 1
                'request' => Type::nonNull(Type::string()),
26 1
            ],
27 1
            'resolve' => function (array $root, array $args): Change {
28 1
                $site = $root['site'];
29
30 1
                $suggestion = $args['id']->getEntity();
31 1
                $original = $suggestion->getOriginal();
32
33 1
                if (!$original) {
34
                    throw new Exception('An suggestion must have an original defined');
35
                }
36
37
                /** @var ChangeRepository $changeRepository */
38 1
                $changeRepository = _em()->getRepository(Change::class);
39 1
                $change = $changeRepository->getOrCreate(Change::TYPE_UPDATE, $suggestion, $args['request'], $site);
40 1
                Helper::throwIfDenied($change, 'create');
41
42 1
                if (!$change->getId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $change->getId() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
43 1
                    _em()->flush();
44
                }
45
46 1
                return $change;
47 1
            },
48 1
        ];
49
    }
50
}
51