Passed
Push — master ( 1d5fb4...e5c3c4 )
by Sam
06:33 queued 01:55
created

UpdateConfiguration   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
wmc 2
eloc 19
dl 0
loc 32
ccs 6
cts 16
cp 0.375
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A build() 0 30 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(): array
17
    {
18
        return [
19 1
            'name' => 'updateConfiguration',
20 1
            'type' => Type::nonNull(Type::string()),
21
            'description' => 'Update configuration value.',
22
            'args' => [
23 1
                'key' => Type::nonNull(Type::string()),
24 1
                'value' => Type::nonNull(Type::string()),
25
            ],
26 1
            'resolve' => function ($root, array $args, SessionInterface $session): string {
27
                $key = $args['key'];
28
                $value = $args['value'];
29
30
                /** @var ConfigurationRepository $configurationRepository */
31
                $configurationRepository = _em()->getRepository(Configuration::class);
32
                $configuration = $configurationRepository->getOrCreate($key);
33
34
                // Check ACL, exceptionally test 'create' privilege instead of 'update'
35
                Helper::throwIfDenied($configuration, 'create');
36
37
                $configuration->setValue($value);
38
39
                if (!$configuration->getValue()) {
40
                    _em()->remove($configuration);
41
                }
42
43
                _em()->flush();
44
45
                return $configuration->getValue();
46
            },
47
        ];
48
    }
49
}
50