Passed
Push — master ( 7e3913...f58fec )
by Sebastian
05:25
created

Localization_Source_Scanner::parseFile()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 23
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AppLocalize;
6
7
class Localization_Source_Scanner
8
{
9
    /**
10
     * @var Localization_Scanner
11
     */
12
    private $scanner;
13
14
    /**
15
     * @var Localization_Source
16
     */
17
    private $source;
18
19
    /**
20
     * @var Localization_Scanner_StringsCollection
21
     */
22
    private $collection;
23
24
    /**
25
     * @var Localization_Parser
26
     */
27
    private $parser;
28
29
    public function __construct(Localization_Source $source, Localization_Scanner $scanner)
30
    {
31
        $this->scanner = $scanner;
32
        $this->source = $source;
33
        $this->collection = $scanner->getCollection();
34
        $this->parser = $scanner->getParser();
35
    }
36
37
    /**
38
     * @return Localization_Parser
39
     */
40
    public function getParser() : Localization_Parser
41
    {
42
        return $this->parser;
43
    }
44
45
    /**
46
     * Parses the code of the target file to find all
47
     * supported function calls and extract the native
48
     * application language string from the code. Adds any
49
     * strings it finds to the results collection.
50
     *
51
     * @param string $file
52
     * @throws Localization_Exception
53
     */
54
    public function parseFile(string $file) : void
55
    {
56
        $this->log(sprintf('Parsing file [%s].', $file));
57
58
        $language = $this->parser->parseFile($file);
59
60
        $texts = $language->getTexts();
61
62
        foreach($texts as $text)
63
        {
64
            $this->collection->addFromFile(
65
                $this->source->getID(),
66
                $file,
67
                $language->getID(),
68
                $text
69
            );
70
        }
71
72
        $warnings = $language->getWarnings();
73
74
        foreach($warnings as $warning)
75
        {
76
            $this->collection->addWarning($warning);
77
        }
78
    }
79
80
    /**
81
     * @return Localization_Scanner_StringHash[]
82
     */
83
    public function getHashes() : array
84
    {
85
        $this->scanner->load();
86
        return $this->collection->getHashesBySourceID($this->source->getID());
87
    }
88
89
    public function countUntranslated() : int
90
    {
91
        $translator = Localization::getTranslator();
92
        $amount = 0;
93
94
        $hashes = $this->getHashes();
95
96
        foreach($hashes as $hash)
97
        {
98
            $text = $translator->getHashTranslation($hash->getHash());
99
100
            if(empty($text))
101
            {
102
                $amount++;
103
            }
104
        }
105
106
        return $amount;
107
    }
108
109
    protected function log(string $message) : void
110
    {
111
        Localization::log(sprintf(
112
            'Source [%s] | Scanner | %s',
113
            $this->source->getID(),
114
            $message
115
        ));
116
    }
117
}
118