Completed
Push — master ( 130d29...c9c04e )
by Tobias
187:22 queued 181:29
created

CatalogueFetcher   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 5
dl 0
loc 77
ccs 0
cts 44
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
B getCatalogues() 0 28 7
A isValidDomain() 0 15 5
1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Catalogue;
13
14
use Nyholm\NSA;
15
use Symfony\Bundle\FrameworkBundle\Translation\TranslationLoader as SymfonyTranslationLoader;
16
use Symfony\Component\Translation\MessageCatalogue;
17
use Symfony\Component\Translation\Reader\TranslationReaderInterface;
18
use Translation\Bundle\Model\Configuration;
19
use Translation\SymfonyStorage\LegacyTranslationReader;
20
use Translation\SymfonyStorage\TranslationLoader;
21
22
/**
23
 * Fetches catalogues from source files. This will only work with local file storage
24
 * and the actions are read only.
25
 *
26
 * This should be considered as a "ReadFromCache" service.
27
 *
28
 * @author Tobias Nyholm <[email protected]>
29
 */
30
final class CatalogueFetcher
31
{
32
    /**
33
     * @var TranslationReaderInterface
34
     */
35
    private $reader;
36
37
    /**
38
     * @param SymfonyTranslationLoader|TranslationLoader|TranslationReaderInterface $reader
39
     */
40
    public function __construct($reader)
41
    {
42
        if (!$reader instanceof TranslationReaderInterface) {
43
            $reader = new LegacyTranslationReader($reader);
44
        }
45
46
        $this->reader = $reader;
0 ignored issues
show
Documentation Bug introduced by
It seems like $reader can also be of type object<Translation\Symfo...egacyTranslationReader>. However, the property $reader is declared as type object<Symfony\Component...slationReaderInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
47
    }
48
49
    /**
50
     * load any existing messages from the translation files.
51
     *
52
     * @param Configuration $config
53
     * @param array         $locales
54
     *
55
     * @return MessageCatalogue[]
56
     */
57
    public function getCatalogues(Configuration $config, array $locales = [])
58
    {
59
        $dirs = $config->getPathsToTranslationFiles();
60
        if (empty($locales)) {
61
            $locales = $config->getLocales();
62
        }
63
        $catalogues = [];
64
        foreach ($locales as $locale) {
65
            $currentCatalogue = new MessageCatalogue($locale);
66
            foreach ($dirs as $path) {
67
                if (is_dir($path)) {
68
                    $this->reader->read($path, $currentCatalogue);
69
                }
70
            }
71
72
            foreach ($currentCatalogue->getDomains() as $domain) {
73
                if (!$this->isValidDomain($config, $domain)) {
74
                    $messages = $currentCatalogue->all();
75
                    unset($messages[$domain]);
76
                    NSA::setProperty($currentCatalogue, 'messages', $messages);
77
                }
78
            }
79
80
            $catalogues[] = $currentCatalogue;
81
        }
82
83
        return $catalogues;
84
    }
85
86
    /**
87
     * @param string $domain
88
     *
89
     * @return bool
90
     */
91
    private function isValidDomain(Configuration $config, $domain)
92
    {
93
        $blacklist = $config->getBlacklistDomains();
94
        $whitelist = $config->getWhitelistDomains();
95
96
        if (!empty($blacklist) && in_array($domain, $blacklist)) {
97
            return false;
98
        }
99
100
        if (!empty($whitelist) && !in_array($domain, $whitelist)) {
101
            return false;
102
        }
103
104
        return true;
105
    }
106
}
107