Translation::getDomainName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Locastic\SymfonyTranslationBundle\Model;
6
7
class Translation implements TranslationInterface
8
{
9
    private ?string $domainName = null;
10
11
    private ?string $key = null;
12
13
    /** @var array|TranslationValueInterface[] */
14
    private array $values = [];
15
16
    public function getDomainName(): ?string
17
    {
18
        return $this->domainName;
19
    }
20
21
    public function setDomainName(?string $domainName): void
22
    {
23
        $this->domainName = $domainName;
24
    }
25
26
    public function getKey(): ?string
27
    {
28
        return $this->key;
29
    }
30
31
    public function setKey(?string $key): void
32
    {
33
        $this->key = $key;
34
    }
35
36
    public function getValues(): array
37
    {
38
        return $this->values;
39
    }
40
41
    public function getValuesByTheme(string $themeName): array
42
    {
43
        return \array_filter($this->getValues(), function (TranslationValueInterface $translationValue) use ($themeName): bool {
44
            return $themeName === $translationValue->getTheme();
45
        });
46
    }
47
48
    public function getKeyByLocaleAndTheme(string $localeCode, string $themeName): ?int
49
    {
50
        foreach ($this->getValues() as $key => $value) {
51
            if ($value->getLocaleCode() === $localeCode && $value->getTheme() === $themeName) {
52
                return $key;
53
            }
54
        }
55
56
        return null;
57
    }
58
59
    public function hasLocaleAndTheme(string $localeCode, string $themeName): bool
60
    {
61
        return null !== $this->getKeyByLocaleAndTheme($localeCode, $themeName);
62
    }
63
64
    public function hasValue(TranslationValueInterface $translationValue): bool
65
    {
66
        return $this->hasLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme()) !== null;
0 ignored issues
show
Bug introduced by
It seems like $translationValue->getLocaleCode() can also be of type null; however, parameter $localeCode of Locastic\SymfonyTranslat...on::hasLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        return $this->hasLocaleAndTheme(/** @scrutinizer ignore-type */ $translationValue->getLocaleCode(), $translationValue->getTheme()) !== null;
Loading history...
Bug introduced by
It seems like $translationValue->getTheme() can also be of type null; however, parameter $themeName of Locastic\SymfonyTranslat...on::hasLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        return $this->hasLocaleAndTheme($translationValue->getLocaleCode(), /** @scrutinizer ignore-type */ $translationValue->getTheme()) !== null;
Loading history...
67
    }
68
69
    public function addValue(TranslationValueInterface $translationValue): void
70
    {
71
        if (!$this->hasLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme())) {
0 ignored issues
show
Bug introduced by
It seems like $translationValue->getTheme() can also be of type null; however, parameter $themeName of Locastic\SymfonyTranslat...on::hasLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

71
        if (!$this->hasLocaleAndTheme($translationValue->getLocaleCode(), /** @scrutinizer ignore-type */ $translationValue->getTheme())) {
Loading history...
Bug introduced by
It seems like $translationValue->getLocaleCode() can also be of type null; however, parameter $localeCode of Locastic\SymfonyTranslat...on::hasLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

71
        if (!$this->hasLocaleAndTheme(/** @scrutinizer ignore-type */ $translationValue->getLocaleCode(), $translationValue->getTheme())) {
Loading history...
72
            $this->values[] = $translationValue;
73
            $translationValue->setTranslation($this);
74
        }
75
    }
76
77
    public function removeValue(TranslationValueInterface $translationValue): void
78
    {
79
        if (null !== $key = $this->getKeyByLocaleAndTheme($translationValue->getLocaleCode(), $translationValue->getTheme())) {
0 ignored issues
show
Bug introduced by
It seems like $translationValue->getLocaleCode() can also be of type null; however, parameter $localeCode of Locastic\SymfonyTranslat...etKeyByLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
        if (null !== $key = $this->getKeyByLocaleAndTheme(/** @scrutinizer ignore-type */ $translationValue->getLocaleCode(), $translationValue->getTheme())) {
Loading history...
Bug introduced by
It seems like $translationValue->getTheme() can also be of type null; however, parameter $themeName of Locastic\SymfonyTranslat...etKeyByLocaleAndTheme() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
        if (null !== $key = $this->getKeyByLocaleAndTheme($translationValue->getLocaleCode(), /** @scrutinizer ignore-type */ $translationValue->getTheme())) {
Loading history...
80
            unset($this->values[$key]);
81
            $translationValue->setTranslation(null);
82
        }
83
    }
84
85
    public function getValueByLocaleAndTheme(string $localeCode, string $themeName): ?string
86
    {
87
        if (null !== $key = $this->getKeyByLocaleAndTheme($localeCode, $themeName)) {
88
            return $this->values[$key];
89
        }
90
91
        return null;
92
    }
93
}
94