Passed
Push — master ( e872b0...c8fc29 )
by F
03:08
created

LanguageRepository::getLanguages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 3
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 5
rs 10
1
<?php
2
3
namespace PWWEB\Localisation\Repositories;
4
5
use App\Repositories\BaseRepository;
0 ignored issues
show
Bug introduced by
The type App\Repositories\BaseRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\Collection;
7
use PWWEB\Localisation\Contracts\LanguageContract;
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Contracts\LanguageContract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use PWWEB\Localisation\LocalisationRegistrar;
9
use PWWEB\Localisation\Models\Language;
10
11
/**
12
 * PWWEB\Localisation\Repositories\LanguageRepository LanguageRepository.
13
 *
14
 * The repository for Language.
15
 * Class LanguageRepository
16
 *
17
 * @package   pwweb/localisation
18
 * @author    Frank Pillukeit <[email protected]>
19
 * @author    Richard Browne <[email protected]
20
 * @copyright 2020 pw-websolutions.com
21
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
22
 */
23
class LanguageRepository extends BaseRepository
24
{
25
    /**
26
     * @var array
27
     */
28
    protected $fieldSearchable = [
29
        'name',
30
        'locale',
31
        'abbreviation',
32
        'installed',
33
        'active',
34
        'standard'
35
    ];
36
37
    /**
38
     * Return searchable fields.
39
     *
40
     * @return array
41
     */
42
    public function getFieldsSearchable()
43
    {
44
        return $this->fieldSearchable;
45
    }
46
47
    /**
48
     * Configure the Model.
49
     **/
50
    public function model()
51
    {
52
        return Language::class;
53
    }
54
55
    /**
56
     * Retrieve all active languages.
57
     *
58
     * @return \Illuminate\Database\Eloquent\Collection
59
     */
60
    public function getAllActive()
61
    {
62
        return Language::where('active', 1)->get();
63
    }
64
65
    /**
66
     * Find a language by its name.
67
     *
68
     * @param string $name language name to be used to retrieve the language
69
     *
70
     * @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist
71
     *
72
     * @return \PWWEB\Localisation\Contracts\Language
73
     */
74
    public static function findByName(string $name): LanguageContract
75
    {
76
        $language = static::getLanguages(['name' => $name])->first();
77
78
        if (null === $language) {
79
            throw LanguageDoesNotExist::create($name);
0 ignored issues
show
Bug introduced by
The type PWWEB\Localisation\Repos...es\LanguageDoesNotExist was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
80
        }
81
82
        return $language;
83
    }
84
85
    /**
86
     * Find a language by its id.
87
     *
88
     * @param int $id ID to be used to retrieve the language
89
     *
90
     * @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist
91
     *
92
     * @return \PWWEB\Localisation\Contracts\Language
93
     */
94
    public static function findById(int $id): LanguageContract
95
    {
96
        $language = static::getLanguages(['id' => $id])->first();
97
98
        if (null === $language) {
99
            throw LanguageDoesNotExist::withId($id);
100
        }
101
102
        return $language;
103
    }
104
105
    /**
106
     * Find a language by its locale, e.g. en-gb.
107
     *
108
     * @param string $locale locale to be used to retrieve the language
109
     *
110
     * @throws \PWWEB\Localisation\Exceptions\LanguageDoesNotExist
111
     *
112
     * @return \PWWEB\Localisation\Contracts\Language
113
     */
114
    public static function findByLocale(string $locale): LanguageContract
115
    {
116
        $language = static::getLanguages(['locale' => $locale])->first();
117
118
        if (null === $language) {
119
            throw LanguageDoesNotExist::create($locale);
120
        }
121
122
        return $language;
123
    }
124
125
    /**
126
     * Get the current cached languages.
127
     *
128
     * @param array $params additional parameters for the database query
129
     *
130
     * @return \Illuminate\Database\Eloquent\Collection collection of languages
131
     */
132
    protected static function getLanguages(array $params = []): Collection
133
    {
134
        return app(LocalisationRegistrar::class)
135
            ->setLanguageClass(static::class)
136
            ->getLanguages($params);
137
    }
138
139
    /**
140
     * Obtain the available locales.
141
     *
142
     * @param array $params Set of additional params for querying.
143
     *
144
     * @return array
145
     */
146
    protected static function getLocales(array $params = []): array
147
    {
148
        $locales = [];
149
        $languages = self::getLanguages($params);
150
151
        foreach ($languages as $language) {
152
            $locales[] = $language->locale;
153
        }
154
155
        return $locales;
156
    }
157
}
158