Completed
Push — master ( 9a9938...f96c7c )
by Mathieu
55:39 queued 13s
created

CatalogueManager::findMessages()   F

Complexity

Conditions 17
Paths 336

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 17.0131

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 27
cts 28
cp 0.9643
rs 2.6833
c 0
b 0
f 0
cc 17
nc 336
nop 1
crap 17.0131

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)
35
    {
36 3
        $this->catalogues = [];
37 3
        foreach ($catalogues as $c) {
38 3
            $this->catalogues[$c->getLocale()] = $c;
39
        }
40 3
    }
41
42
    /**
43
     * @return array
44
     */
45
    public function getDomains()
46
    {
47
        /** @var MessageCatalogueInterface $c */
48
        $c = \reset($this->catalogues);
49
50
        return $c->getDomains();
51
    }
52
53
    /**
54
     * @param string $locale
55
     * @param string $domain
56
     *
57
     * @return CatalogueMessage[]
58
     */
59 1
    public function getMessages($locale, $domain)
60
    {
61 1
        $messages = [];
62 1
        if (!isset($this->catalogues[$locale])) {
63
            return $messages;
64
        }
65
66 1
        foreach ($this->catalogues[$locale]->all($domain) as $key => $text) {
67 1
            $messages[] = $this->createMessage($this->catalogues[$locale], $locale, $domain, $key, $text);
68
        }
69
70 1
        return $messages;
71
    }
72
73
    /**
74
     * @param array $config {
75
     *
76
     *      @var string $domain
77
     *      @var string $locale
78
     *      @var bool $isNew
79
     *      @var bool $isObsolete
80
     *      @var bool $isApproved
81
     * }
82
     *
83
     * @return CatalogueMessage[]
84
     */
85 2
    public function findMessages(array $config = [])
86
    {
87 2
        $inputDomain = isset($config['domain']) ? $config['domain'] : null;
88 2
        $isNew = isset($config['isNew']) ? $config['isNew'] : null;
89 2
        $isObsolete = isset($config['isObsolete']) ? $config['isObsolete'] : null;
90 2
        $isApproved = isset($config['isApproved']) ? $config['isApproved'] : null;
91
92 2
        $messages = [];
93 2
        $catalogues = [];
94 2
        if (isset($config['locale'])) {
95 2
            $locale = $config['locale'];
96 2
            if (isset($this->catalogues[$locale])) {
97 2
                $catalogues = [$locale => $this->catalogues[$locale]];
98
            }
99
        } else {
100 1
            $catalogues = $this->catalogues;
101
        }
102
103 2
        foreach ($catalogues as $locale => $catalogue) {
104 2
            $domains = $catalogue->getDomains();
105 2
            if (null !== $inputDomain) {
106
                $domains = [$inputDomain];
107
            }
108 2
            foreach ($domains as $domain) {
109 2
                foreach ($catalogue->all($domain) as $key => $text) {
110 2
                    $messages[] = $this->createMessage($catalogue, $locale, $domain, $key, $text);
111
                }
112
            }
113
        }
114
115
        $messages = \array_filter($messages, function (CatalogueMessage $m) use ($isNew, $isObsolete, $isApproved) {
116 2
            if (null !== $isNew && $m->isNew() !== $isNew) {
117 1
                return false;
118
            }
119 2
            if (null !== $isObsolete && $m->isObsolete() !== $isObsolete) {
120 1
                return false;
121
            }
122 2
            if (null !== $isApproved && $m->isApproved() !== $isApproved) {
123 1
                return false;
124
            }
125
126 2
            return true;
127 2
        });
128
129 2
        return $messages;
130
    }
131
132
    /**
133
     * @param string $domain
134
     * @param string $key
135
     *
136
     * @return array
137
     */
138
    public function getTranslations($domain, $key)
139
    {
140
        $translations = [];
141
        foreach ($this->catalogues as $locale => $catalogue) {
142
            if ($catalogue->has($key, $domain)) {
143
                $translations[$locale] = $catalogue->get($key, $domain);
144
            }
145
        }
146
147
        return $translations;
148
    }
149
150
    /**
151
     * @param $locale
152
     * @param $domain
153
     * @param $key
154
     * @param $text
155
     *
156
     * @return CatalogueMessage
157
     */
158 3
    private function createMessage(MessageCatalogueInterface $catalogue, $locale, $domain, $key, $text)
159
    {
160 3
        $catalogueMessage = new CatalogueMessage($this, $locale, $domain, $key, $text);
161
162 3
        if ($catalogue instanceof MetadataAwareInterface) {
163 3
            $catalogueMessage->setMetadata(new Metadata($catalogue->getMetadata($key, $domain)));
164
        }
165
166 3
        return $catalogueMessage;
167
    }
168
}
169