CatalogueManager::findMessages()   C
last analyzed

Complexity

Conditions 15
Paths 21

Size

Total Lines 49
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 15.0603

Importance

Changes 0
Metric Value
cc 15
eloc 31
c 0
b 0
f 0
nc 21
nop 1
dl 0
loc 49
ccs 29
cts 31
cp 0.9355
crap 15.0603
rs 5.9166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\Translation\MessageCatalogueInterface;
15
use Symfony\Component\Translation\MetadataAwareInterface;
16
use Translation\Bundle\Model\CatalogueMessage;
17
use Translation\Bundle\Model\Metadata;
18
19
/**
20
 * A manager that handle loaded catalogues.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
final class CatalogueManager
25
{
26
    /**
27
     * @var MessageCatalogueInterface[]
28
     */
29
    private $catalogues = [];
30
31
    /**
32
     * @param MessageCatalogueInterface[] $catalogues
33
     */
34 3
    public function load(array $catalogues): void
35
    {
36 3
        $this->catalogues = [];
37 3
        foreach ($catalogues as $c) {
38 3
            $this->catalogues[$c->getLocale()] = $c;
39
        }
40 3
    }
41
42
    public function getDomains(): array
43
    {
44
        /** @var MessageCatalogueInterface $c */
45
        $c = \reset($this->catalogues);
46
47
        return $c->getDomains();
48
    }
49
50
    /**
51
     * @return CatalogueMessage[]
52
     */
53 1
    public function getMessages(string $locale, string $domain): array
54
    {
55 1
        $messages = [];
56 1
        if (!isset($this->catalogues[$locale])) {
57
            return $messages;
58
        }
59
60 1
        foreach ($this->catalogues[$locale]->all($domain) as $key => $text) {
61 1
            $messages[] = $this->createMessage($this->catalogues[$locale], $locale, $domain, $key, $text ?? '');
62
        }
63
64 1
        return $messages;
65
    }
66
67
    /**
68
     * @param array $config {
69
     *
70
     *      @var string $domain
71
     *      @var string $locale
72
     *      @var bool $isNew
73
     *      @var bool $isObsolete
74
     *      @var bool $isApproved
75
     * }
76
     *
77
     * @return CatalogueMessage[]
78
     */
79 2
    public function findMessages(array $config = []): array
80
    {
81 2
        $inputDomain = $config['domain'] ?? null;
82 2
        $isNew = $config['isNew'] ?? null;
83 2
        $isObsolete = $config['isObsolete'] ?? null;
84 2
        $isApproved = $config['isApproved'] ?? null;
85 2
        $isEmpty = $config['isEmpty'] ?? null;
86
87 2
        $messages = [];
88 2
        $catalogues = [];
89 2
        if (isset($config['locale'])) {
90 2
            $locale = $config['locale'];
91 2
            if (isset($this->catalogues[$locale])) {
92 2
                $catalogues = [$locale => $this->catalogues[$locale]];
93
            }
94
        } else {
95 1
            $catalogues = $this->catalogues;
96
        }
97
98 2
        foreach ($catalogues as $locale => $catalogue) {
99 2
            $domains = $catalogue->getDomains();
100 2
            if (null !== $inputDomain) {
101
                $domains = [$inputDomain];
102
            }
103 2
            foreach ($domains as $domain) {
104 2
                foreach ($catalogue->all($domain) as $key => $text) {
105 2
                    $messages[] = $this->createMessage($catalogue, $locale, $domain, $key, $text);
106
                }
107
            }
108
        }
109
110
        $messages = \array_filter($messages, static function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved, $isEmpty) {
111 2
            if (null !== $isNew && $m->isNew() !== $isNew) {
112 1
                return false;
113
            }
114 2
            if (null !== $isObsolete && $m->isObsolete() !== $isObsolete) {
115 1
                return false;
116
            }
117 2
            if (null !== $isApproved && $m->isApproved() !== $isApproved) {
118 1
                return false;
119
            }
120 2
            if (null !== $isEmpty && empty($m->getMessage()) !== $isEmpty) {
121
                return false;
122
            }
123
124 2
            return true;
125 2
        });
126
127 2
        return $messages;
128
    }
129
130
    /**
131
     * @param string $domain
132
     * @param string $key
133
     */
134
    public function getTranslations($domain, $key): array
135
    {
136
        $translations = [];
137
        foreach ($this->catalogues as $locale => $catalogue) {
138
            if ($catalogue->has($key, $domain)) {
139
                $translations[$locale] = $catalogue->get($key, $domain);
140
            }
141
        }
142
143
        return $translations;
144
    }
145
146 3
    private function createMessage(MessageCatalogueInterface $catalogue, string $locale, string $domain, string $key, string $text): CatalogueMessage
147
    {
148 3
        $catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text);
149
150 3
        if ($catalogue instanceof MetadataAwareInterface) {
151 3
            $catalogueMessage->setMetadata(new Metadata($catalogue->getMetadata($key, $domain)));
152
        }
153
154 3
        return $catalogueMessage;
155
    }
156
}
157