SuggestUpdate   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 3
eloc 19
c 0
b 0
f 0
dl 0
loc 32
ccs 19
cts 20
cp 0.95
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 30 3
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\Enum\Site;
10
use Application\Model\Card;
11
use Application\Model\Change;
12
use Application\Repository\ChangeRepository;
13
use Ecodev\Felix\Api\Exception;
14
use Ecodev\Felix\Api\Field\FieldInterface;
15
use GraphQL\Type\Definition\Type;
16
17
class SuggestUpdate implements FieldInterface
18
{
19 1
    public static function build(): iterable
20
    {
21 1
        yield 'suggestUpdate' => fn () => [
22 1
            'type' => Type::nonNull(_types()->getOutput(Change::class)),
23 1
            'description' => 'Suggest the update of an existing image',
24 1
            'args' => [
25 1
                'id' => Type::nonNull(_types()->getId(Card::class)),
26 1
                'request' => Type::nonNull(Type::string()),
27 1
            ],
28 1
            'resolve' => function ($root, array $args): Change {
29
                /** @var Site $site */
30 1
                $site = $root['site'];
31
32 1
                $suggestion = $args['id']->getEntity();
33 1
                $original = $suggestion->getOriginal();
34
35 1
                if (!$original) {
36
                    throw new Exception('An suggestion must have an original defined');
37
                }
38
39
                /** @var ChangeRepository $changeRepository */
40 1
                $changeRepository = _em()->getRepository(Change::class);
41 1
                $change = $changeRepository->getOrCreate(ChangeType::Update, $suggestion, $args['request'], $site);
42 1
                Helper::throwIfDenied($change, 'create');
43
44 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...
45 1
                    _em()->flush();
46
                }
47
48 1
                return $change;
49 1
            },
50 1
        ];
51
    }
52
}
53