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

UpdateConfiguration::build()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2.5831

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 29
ccs 9
cts 19
cp 0.4737
rs 9.7
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2.5831
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