Completed
Push — develop ( c73a77...c0f65e )
by Neomerx
02:32
created

getMessageDescriptionsFromProviders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 4
nop 0
crap 4
1
<?php namespace Limoncello\Application\Packages\L10n;
2
3
/**
4
 * Copyright 2015-2017 [email protected]
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
use Limoncello\Contracts\Application\ApplicationConfigurationInterface as A;
20
use Limoncello\Contracts\Provider\ProvidesMessageResourcesInterface;
21
use Limoncello\Contracts\Settings\SettingsInterface;
22
use Limoncello\l10n\Messages\FileBundleEncoder;
23
24
/**
25
 * @package Limoncello\Application
26
 */
27
abstract class L10nSettings implements SettingsInterface
28
{
29
    /** Settings key */
30
    const KEY_DEFAULT_LOCALE = 0;
31
32
    /** Settings key */
33
    const KEY_LOCALES_FOLDER = self::KEY_DEFAULT_LOCALE + 1;
34
35
    /** Settings key */
36
    const KEY_LOCALES_DATA = self::KEY_LOCALES_FOLDER + 1;
37
38
    /** Settings key */
39
    protected const KEY_LAST = self::KEY_LOCALES_DATA;
40
41
    /**
42
     * @var array
43
     */
44
    private $appConfig;
45
46
    /**
47
     * @inheritdoc
48
     */
49 1
    final public function get(array $appConfig): array
50
    {
51 1
        $this->appConfig = $appConfig;
52
53 1
        $defaults = $this->getSettings();
54
55 1
        $defaultLocale = $defaults[static::KEY_DEFAULT_LOCALE] ?? null;
56 1
        assert(empty($defaultLocale) === false, "Invalid default locale `$defaultLocale`.");
57
58 1
        $localesFolder = $defaults[static::KEY_LOCALES_FOLDER] ?? null;
59 1
        assert(
60 1
            $localesFolder !== null && empty(glob($localesFolder)) === false,
61 1
            "Invalid Locales folder `$localesFolder`."
62
        );
63
64 1
        $bundleEncoder = new FileBundleEncoder($this->getMessageDescriptionsFromProviders(), $localesFolder);
0 ignored issues
show
Documentation introduced by
$this->getMessageDescriptionsFromProviders() is of type object<Generator>, but the function expects a object<Limoncello\l10n\Messages\iterable>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
66
        return $defaults + [
67 1
                static::KEY_LOCALES_DATA => $bundleEncoder->getStorageData($defaultLocale),
68
            ];
69
    }
70
71
    /**
72
     * @return array
73
     */
74 1
    protected function getSettings(): array
75
    {
76
        return [
77 1
            static::KEY_DEFAULT_LOCALE => 'en',
78
        ];
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84 1
    protected function getAppConfig()
85
    {
86 1
        return $this->appConfig;
87
    }
88
89
    /**
90
     *
91
     * @return iterable
0 ignored issues
show
Documentation introduced by
Should the return type not be \Generator?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
92
     */
93 1
    private function getMessageDescriptionsFromProviders(): iterable
94
    {
95 1
        $providerClasses = $this->getAppConfig()[A::KEY_PROVIDER_CLASSES] ?? [];
96 1
        foreach ($providerClasses as $class) {
97 1
            if (in_array(ProvidesMessageResourcesInterface::class, class_implements($class)) === true) {
98
                /** @var ProvidesMessageResourcesInterface $class */
99 1
                foreach ($class::getMessageDescriptions() as $messageDescription) {
100 1
                    yield $messageDescription;
101
                }
102
            }
103
        }
104
    }
105
}
106