Translator::translate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SaasReady\Services;
4
5
use SaasReady\Contracts\TranslationRepositoryContract;
6
7
class Translator
8
{
9
    public function __construct(private TranslationRepositoryContract $translationRepository)
10
    {
11
    }
12
13
    public function translate(string $key, array $variables = []): string
14
    {
15
        $translatedText = $this->translationRepository->getTranslation($key);
16
17
        $replacements = collect($variables)->mapWithKeys(fn ($value, $placeholder) => [
0 ignored issues
show
Bug introduced by
The function collect was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

17
        $replacements = /** @scrutinizer ignore-call */ collect($variables)->mapWithKeys(fn ($value, $placeholder) => [
Loading history...
18
            '{' . trim($placeholder, '{}') . '}' => $value,
19
        ])->toArray();
20
21
        return strtr($translatedText, $replacements);
22
    }
23
}
24