Failed Conditions
Push — master ( ce2a97...d73709 )
by Adrien
13:38 queued 11:33
created

UpdateConfiguration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 47.37%

Importance

Changes 0
Metric Value
wmc 2
eloc 18
dl 0
loc 31
ccs 9
cts 19
cp 0.4737
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 29 2
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\Configuration;
9
use Application\Repository\ConfigurationRepository;
10
use Ecodev\Felix\Api\Field\FieldInterface;
11
use GraphQL\Type\Definition\Type;
12
use Mezzio\Session\SessionInterface;
13
14
abstract class UpdateConfiguration implements FieldInterface
15
{
16 1
    public static function build(): iterable
17
    {
18 1
        yield 'updateConfiguration' => fn () => [
19 1
            'type' => Type::nonNull(Type::string()),
20 1
            'description' => 'Update configuration value.',
21 1
            'args' => [
22 1
                'key' => Type::nonNull(Type::string()),
23 1
                'value' => Type::nonNull(Type::string()),
24 1
            ],
25 1
            'resolve' => function ($root, array $args, SessionInterface $session): string {
26
                $key = $args['key'];
27
                $value = $args['value'];
28
29
                /** @var ConfigurationRepository $configurationRepository */
30
                $configurationRepository = _em()->getRepository(Configuration::class);
31
                $configuration = $configurationRepository->getOrCreate($key);
32
33
                // Check ACL, exceptionally test 'create' privilege instead of 'update'
34
                Helper::throwIfDenied($configuration, 'create');
35
36
                $configuration->setValue($value);
37
38
                if (!$configuration->getValue()) {
39
                    _em()->remove($configuration);
40
                }
41
42
                _em()->flush();
43
44
                return $configuration->getValue();
45 1
            },
46 1
        ];
47
    }
48
}
49