MessagesCatalogue   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 6
Bugs 0 Features 3
Metric Value
wmc 8
eloc 22
c 6
b 0
f 3
dl 0
loc 39
rs 10
ccs 20
cts 21
cp 0.9524

4 Methods

Rating   Name   Duplication   Size   Complexity  
A loadDomain() 0 2 1
A parseFile() 0 2 1
A loadTexts() 0 24 5
A getLanguageFilenameMask() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Nexendrie\Translation\Loaders;
5
6
use Nette\Utils\Finder;
7
use Nexendrie\Translation\FolderNotSetException;
8
9
/**
10
 * MessagesCatalogue
11
 * Loads texts from compiled (PHP) messages catalogue
12
 *
13
 * @author Jakub Konečný
14
 */
15 1
final class MessagesCatalogue extends FileLoader {
16
  protected $extension = "php";
17
18
  protected function parseFile(string $filename): array {
19 1
    return require $filename;
20
  }
21
22
  protected function loadDomain(string $name): array {
23
    return [];
24
  }
25
  
26
  protected function loadTexts(): void {
27 1
    if(count($this->folders) === 0) {
28 1
      throw new FolderNotSetException("Folder for translations was not set.");
29
    }
30 1
    $this->resources = $texts = [];
31 1
    $filename = str_replace(static::LANGUAGE_MASK, $this->lang, $this->getLanguageFilenameMask());
32 1
    $files = Finder::findFiles($filename)
33 1
      ->from($this->folders);
34
    /** @var \SplFileInfo $file */
35 1
    foreach($files as $file) {
36 1
      $texts = array_merge($texts, $this->parseFile($file->getPathname()));
37 1
      if(isset($texts["__resources"])) {
38 1
        $this->resources = array_merge($this->resources, $texts["__resources"]);
39 1
        unset($texts["__resources"]);
40
      } else {
41
        /** @var string[] $domains */
42 1
        $domains = array_keys($texts);
43 1
        foreach($domains as $domain) {
44 1
          $this->addResource($file->getPathname(), $domain);
45
        }
46
      }
47
    }
48 1
    $this->texts = $texts;
49 1
    $this->loadedLang = $this->lang;
50 1
  }
51
52
  protected function getLanguageFilenameMask(): string {
53 1
    return "catalogue." . static::LANGUAGE_MASK . "." . $this->extension;
54
  }
55
}
56
?>