Completed
Push — develop ( 511001...8b4d6d )
by A.
03:05
created

LocaleService::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 9.4286
cc 1
eloc 4
nc 1
nop 3
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
use OpenConext\Profile\Value\LocaleSet;
26
27
final class LocaleService
28
{
29
    /**
30
     * @var LocaleRepository
31
     */
32
    private $localeRepository;
33
34
    /**
35
     * @var LocaleSet
36
     */
37
    private $availableLocales;
38
39
    /**
40
     * @var Locale
41
     */
42
    private $defaultLocale;
43
44
    /**
45
     * @param LocaleRepository $localeRepository
46
     * @param LocaleSet $availableLocales
47
     * @param Locale $defaultLocale
48
     */
49
    public function __construct(LocaleRepository $localeRepository, LocaleSet $availableLocales, Locale $defaultLocale)
50
    {
51
        $this->localeRepository = $localeRepository;
52
        $this->availableLocales = $availableLocales;
53
        $this->defaultLocale    = $defaultLocale;
54
    }
55
56
    /**
57
     * @param Locale $locale
58
     * @return bool
59
     */
60
    public function isAvailableLocale(Locale $locale)
61
    {
62
        return $this->availableLocales->contains($locale);
63
    }
64
65
    /**
66
     * @return Locale
67
     */
68
    public function getLocale()
69
    {
70
        $locale = $this->localeRepository->findLocale();
71
72
        if ($locale) {
73
            return $locale;
74
        }
75
76
        return $this->defaultLocale;
77
    }
78
79
    /**
80
     * @param User $user
81
     */
82
    public function saveLocaleOf(User $user)
83
    {
84
        $this->localeRepository->save($user->getLocale());
85
    }
86
87
    /**
88
     * @return LocaleSet
89
     */
90
    public function getAvailableLocales()
91
    {
92
        return $this->availableLocales;
93
    }
94
}
95