LocaleService   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 3
dl 0
loc 68
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isAvailableLocale() 0 4 1
A getLocale() 0 10 2
A saveLocaleOf() 0 4 1
A getAvailableLocales() 0 4 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\Repository\LocaleRepositoryInterface;
22
use OpenConext\Profile\Value\Locale;
23
use OpenConext\Profile\Api\ApiUserInterface;
24
use OpenConext\Profile\Value\LocaleSet;
25
26
final class LocaleService
27
{
28
    /**
29
     * @var LocaleRepositoryInterface
30
     */
31
    private $localeRepository;
32
33
    /**
34
     * @var LocaleSet
35
     */
36
    private $availableLocales;
37
38
    /**
39
     * @var Locale
40
     */
41
    private $defaultLocale;
42
43
    /**
44
     * @param LocaleRepositoryInterface $localeRepository
45
     * @param LocaleSet $availableLocales
46
     * @param Locale $defaultLocale
47
     */
48
    public function __construct(LocaleRepositoryInterface $localeRepository, LocaleSet $availableLocales, Locale $defaultLocale)
49
    {
50
        $this->localeRepository = $localeRepository;
51
        $this->availableLocales = $availableLocales;
52
        $this->defaultLocale    = $defaultLocale;
53
    }
54
55
    /**
56
     * @param Locale $locale
57
     * @return bool
58
     */
59
    public function isAvailableLocale(Locale $locale)
60
    {
61
        return $this->availableLocales->contains($locale);
62
    }
63
64
    /**
65
     * @return Locale
66
     */
67
    public function getLocale()
68
    {
69
        $locale = $this->localeRepository->findLocale();
70
71
        if ($locale) {
72
            return $locale;
73
        }
74
75
        return $this->defaultLocale;
76
    }
77
78
    /**
79
     * @param ApiUserInterface $user
80
     */
81
    public function saveLocaleOf(ApiUserInterface $user)
82
    {
83
        $this->localeRepository->save($user->getLocale());
84
    }
85
86
    /**
87
     * @return LocaleSet
88
     */
89
    public function getAvailableLocales()
90
    {
91
        return $this->availableLocales;
92
    }
93
}
94