Completed
Push — development ( 4cbc56...9c238f )
by Thomas
27s
created

CrowdinImport   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A importTranslations() 0 20 3
A readCrowdinCsv() 0 18 4
1
<?php
2
3
namespace Oc\Translation;
4
5
use Doctrine\DBAL\Connection;
6
7
/**
8
 * very quick and dirty solution to import crowdin snippets into the legacy translation system
9
 */
10
class CrowdinImport
11
{
12
    /**
13
     * @var Connection
14
     */
15
    private $connection;
16
17
    public function __construct(Connection $connection)
18
    {
19
        $this->connection = $connection;
20
    }
21
22
    public function importTranslations()
23
    {
24
        $translationArray = $this
25
            ->readCrowdinCsv(__DIR__ . '/../../../app/Resources/translations/legacycode/oc_legacy.de.csv');
26
27
        foreach ($translationArray as $translation) {
28
            foreach (['de', 'fr', 'nl', 'es', 'pl', 'it', 'ru'] as $languageKey) {
29
                $this->connection->executeUpdate(
30
                    'UPDATE sys_trans_text SET `text` = :text
31
                     WHERE lang = :langKey AND trans_id = :identifier',
32
                    [
33
                        'text' => $translation->$languageKey,
34
                        'id' => $translation->identifier,
35
                        'langKey' => $languageKey,
36
                        'identifier' => $translation->identifier
37
                    ]
38
                );
39
            }
40
        }
41
    }
42
43
    /**
44
     * @param $path
45
     * @return TranslationStruct[]
46
     */
47
    private function readCrowdinCsv($path)
48
    {
49
        $csvHeadline = [];
50
        $translationStructs = [];
51
        if (($handle = fopen($path, 'rb')) !== false) {
52
            while (($data = fgetcsv($handle, 0, ';')) !== false) {
53
                if (!$csvHeadline) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $csvHeadline of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
                    $csvHeadline = array_values($data);
55
                    continue;
56
                }
57
58
                $translationStructs[] = (new TranslationStruct())->fromCsvArray(array_combine($csvHeadline, $data));
59
            }
60
            fclose($handle);
61
        }
62
63
        return $translationStructs;
64
    }
65
}
66