Completed
Pull Request — development (#546)
by Nick
07:44 queued 58s
created

LanguageService   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetchAll() 0 4 1
A fetchAllTranslated() 0 4 1
A create() 0 4 1
A update() 0 4 1
A remove() 0 4 1
A getAvailableTranslations() 0 12 2
1
<?php
2
3
namespace Oc\Language;
4
5
/**
6
 * Class LanguageService
7
 *
8
 * @package Oc\Language
9
 * @author Nick Lubisch <[email protected]>
10
 */
11
class LanguageService
12
{
13
    /**
14
     * @var LanguageRepository
15
     */
16
    private $languageRepository;
17
18
    /**
19
     * LanguageService constructor.
20
     *
21
     * @param LanguageRepository $languageRepository
22
     */
23
    public function __construct(LanguageRepository $languageRepository)
24
    {
25
        $this->languageRepository = $languageRepository;
26
    }
27
28
    /**
29
     * Fetches all languages.
30
     *
31
     * @return LanguageEntity[]
32
     */
33
    public function fetchAll()
34
    {
35
        return $this->languageRepository->fetchAll();
36
    }
37
38
    /**
39
     * Fetches all translated languages.
40
     *
41
     * @return LanguageEntity[]
42
     */
43
    public function fetchAllTranslated()
44
    {
45
        return $this->languageRepository->fetchAllTranslated();
46
    }
47
48
    /**
49
     * Creates a language in the database.
50
     *
51
     * @param LanguageEntity $entity
52
     *
53
     * @return LanguageEntity
54
     */
55
    public function create(LanguageEntity $entity)
56
    {
57
        return $this->languageRepository->create($entity);
58
    }
59
60
    /**
61
     * Update a language in the database.
62
     *
63
     * @param LanguageEntity $entity
64
     *
65
     * @return LanguageEntity
66
     */
67
    public function update(LanguageEntity $entity)
68
    {
69
        return $this->languageRepository->update($entity);
70
    }
71
72
    /**
73
     * Removes a language from the database.
74
     *
75
     * @param LanguageEntity $entity
76
     *
77
     * @return LanguageEntity
78
     */
79
    public function remove(LanguageEntity $entity)
80
    {
81
        return $this->languageRepository->remove($entity);
82
    }
83
84
    /**
85
     * Fetches all translated languages and aggregates them to an array of locales.
86
     *
87
     * @return string[]
88
     */
89
    public function getAvailableTranslations()
90
    {
91
        $translatedLanguages = $this->fetchAllTranslated();
92
93
        $locales = [];
94
95
        foreach ($translatedLanguages as $translatedLanguage) {
0 ignored issues
show
Bug introduced by
The expression $translatedLanguages of type null|array<integer,objec...nguage\LanguageEntity>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
96
            $locales[] = strtolower($translatedLanguage->short);
97
        }
98
99
        return $locales;
100
    }
101
}
102