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
|
|
|
|