Completed
Pull Request — develop (#28)
by A.
05:07
created

LocaleService::saveLocaleOf()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * Copyright 2015 SURFnet B.V.
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
namespace OpenConext\ProfileBundle\Service;
20
21
use OpenConext\Profile\Assert;
22
use OpenConext\Profile\Repository\LocaleRepository;
23
use OpenConext\Profile\Value\Locale;
24
use OpenConext\Profile\Api\User;
25
26
final class LocaleService
27
{
28
    /**
29
     * @var LocaleRepository
30
     */
31
    private $localeRepository;
32
33
    /**
34
     * @var string[]
35
     */
36
    private $availableLocales;
37
38
    /**
39
     * @var string
40
     */
41
    private $defaultLocale;
42
43
    /**
44
     * @param LocaleRepository $localeRepository
45
     * @param string[] $availableLocales
46
     * @param Locale $defaultLocale
47
     */
48
    public function __construct(LocaleRepository $localeRepository, array $availableLocales, Locale $defaultLocale)
49
    {
50
        Assert::allString($availableLocales);
51
52
        $this->localeRepository = $localeRepository;
53
        $this->availableLocales = $availableLocales;
54
        $this->defaultLocale    = $defaultLocale;
0 ignored issues
show
Documentation Bug introduced by
It seems like $defaultLocale of type object<OpenConext\Profile\Value\Locale> is incompatible with the declared type string of property $defaultLocale.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55
    }
56
57
    /**
58
     * @param Locale $locale
59
     * @return bool
60
     */
61
    public function isAvailableLocale(Locale $locale)
62
    {
63
        return in_array($this->availableLocales, $locale->getLocale());
64
    }
65
66
    /**
67
     * @return Locale
68
     */
69
    public function getLocale()
70
    {
71
        $locale = $this->localeRepository->findLocale();
72
73
        if ($locale) {
74
            return $locale;
75
        }
76
77
        return $this->defaultLocale;
78
    }
79
80
    /**
81
     * @param User $user
82
     */
83
    public function saveLocaleOf(User $user)
84
    {
85
        $this->localeRepository->save($user->getLocale());
86
    }
87
88
    /**
89
     * @return string[]
90
     */
91
    public function getAvailableLocales()
92
    {
93
        return $this->availableLocales;
94
    }
95
}
96