Test Setup Failed
Push — master ( 3fe332...564b7b )
by F
03:14
created

Language::findOrCreate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * PWWeb\Localisation\Models\Language Model
5
 *
6
 * Standard Language Model.
7
 *
8
 * @package   PWWeb\Localisation
9
 * @author    Frank Pillukeit <[email protected]>
10
 * @copyright 2020 pw-websolutions.com
11
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
12
 */
13
14
namespace PWWeb\Localisation\Models;
15
16
use Illuminate\Database\Eloquent\Collection;
17
use Illuminate\Database\Eloquent\Model;
18
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
19
20
use PWWeb\Localisation\LocalisationRegistrar;
21
use PWWeb\Localisation\Contracts\Language as LanguageContract;
22
use PWWeb\Localisation\Exceptions\LanguageDoesNotExist;
23
24
class Language extends Model implements LanguageContract
25
{
26
    /**
27
     * Constructor
28
     *
29
     * @param array $attributes Additional attributes for model initialisation.
30
     */
31
    public function __construct(array $attributes = [])
32
    {
33
        parent::__construct($attributes);
34
35
        $this->setTable(config('localisation.table_names.languages'));
36
    }
37
38
    /**
39
     * A language can be applied to countries.
40
     *
41
     * @return BelongsToMany Countries the language belongs to.
42
     */
43
    public function countries(): BelongsToMany
44
    {
45
        return $this->belongsToMany(
46
            config('localisation.models.countries'),
47
            config('localisation.table_names.country_has_language'),
48
            'language_id',
49
            'country_id'
50
        );
51
    }
52
53
    /**
54
     * Find a language by its name.
55
     *
56
     * @param string $name Language name to be used to retrieve the language.
57
     *
58
     * @throws \PWWeb\Localisation\Exceptions\LanguageDoesNotExist
59
     *
60
     * @return \PWWeb\Localisation\Contracts\Language
61
     */
62
    public static function findByName(string $name): LanguageContract
63
    {
64
        $language = static::getLanguages(['name' => $name])->first();
65
66
        if ($language === null) {
67
            throw LanguageDoesNotExist::create($name);
68
        }
69
70
        return $language;
71
    }
72
73
    /**
74
     * Find a language by its id.
75
     *
76
     * @param int $id ID to be used to retrieve the language.
77
     *
78
     * @throws \PWWeb\Localisation\Exceptions\LanguageDoesNotExist
79
     *
80
     * @return \PWWeb\Localisation\Contracts\Language
81
     */
82
    public static function findById(int $id): LanguageContract
83
    {
84
        $language = static::getLanguages(['id' => $id])->first();
85
86
        if ($language === null) {
87
            throw LanguageDoesNotExist::withId($id);
88
        }
89
90
        return $language;
91
    }
92
93
    /**
94
     * Find a language by its locale, e.g. en-gb.
95
     *
96
     * @param string $locale Locale to be used to retrieve the language.
97
     *
98
     * @throws \PWWeb\Localisation\Exceptions\LanguageDoesNotExist
99
     *
100
     * @return \PWWeb\Localisation\Contracts\Language
101
     */
102
    public static function findByLocale(string $locale): LanguageContract
103
    {
104
        $locale = strtolower($locale);
105
106
        $language = static::getLanguages(['locale' => $locale])->first();
107
108
        if ($language === null) {
109
            throw LanguageDoesNotExist::create($locale);
110
        }
111
112
        return $language;
113
    }
114
115
    /**
116
     * Get the current cached languages.
117
     *
118
     * @param array $params Additional parameters for the database query.
119
     *
120
     * @return Collection Collection of languages.
121
     */
122
    protected static function getLanguages(array $params = []): Collection
123
    {
124
        return app(LocalisationRegistrar::class)
125
            ->setLanguageClass(static::class)
0 ignored issues
show
Bug introduced by
static::class of type string is incompatible with the type PWWeb\Localisation\Contracts\Language expected by parameter $languageClass of PWWeb\Localisation\Local...rar::setLanguageClass(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
            ->setLanguageClass(/** @scrutinizer ignore-type */ static::class)
Loading history...
126
            ->getLanguages($params);
127
    }
128
}
129