LocaleOptionsTrait::configureLocaleOptions()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 18
rs 9.9666
cc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AppBundle\Integration\MinistryOfTruth;
17
18
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
19
use Symfony\Component\OptionsResolver\Options;
20
use Symfony\Component\OptionsResolver\OptionsResolver;
21
use SymfonyDoge\MinistryOfTruthClient\Dto\RequestDto;
22
23
/**
24
 * Grants a quick shortcut for configuring locale options to service that uses the Ministry of Truth API client
25
 *
26
 * @see RequestDto::$locale
27
 *
28
 * TODO: move to the motc symfony bridge layer as a service (?)
29
 */
30
trait LocaleOptionsTrait
31
{
32
    /**
33
     * Configures locale options of service for building requests to the Ministry of Truth API
34
     *
35
     * @param OptionsResolver $optionsResolver Validates options and merges them with default values
36
     *
37
     * @return void
38
     */
39
    protected function configureLocaleOptions(OptionsResolver $optionsResolver): void
40
    {
41
        $optionsResolver->setDefault('locales', null);
42
43
        $optionsResolver->setDefault(
44
            'default_locale',
45
            function (Options $options, $previousValue) {
46
                if (!is_array($options['locales']) || !in_array($previousValue, $options['locales'], true)) {
47
                    throw new InvalidOptionsException(
48
                        'Value of the "default_locale" option is not present in locales array'
49
                    );
50
                }
51
52
                return $previousValue;
53
            }
54
        );
55
56
        $optionsResolver->setRequired(['default_locale', 'locales']);
57
    }
58
}
59