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

UpdateConfiguration::build()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.9765

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 30
ccs 6
cts 16
cp 0.375
rs 9.6666
c 0
b 0
f 0
cc 2
nc 1
nop 0
crap 2.9765
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