TranslationsImporter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Modules\Translation\Importers;
2
3
use League\Csv\Reader;
4
use Modules\Translation\Repositories\TranslationRepository;
5
use Symfony\Component\HttpFoundation\File\UploadedFile;
6
7
class TranslationsImporter
8
{
9
    /**
10
     * @var TranslationRepository
11
     */
12
    private $translation;
13
14
    public function __construct(TranslationRepository $translation)
15
    {
16
        $this->translation = $translation;
17
    }
18
19
    public function import(UploadedFile $file)
20
    {
21
        $csv = Reader::createFromPath($file->getRealPath());
22
        $csv->detectDelimiterList(5, [',', ';']);
0 ignored issues
show
Deprecated Code introduced by
The method League\Csv\Config\Controls::detectDelimiterList() has been deprecated with message: deprecated since version 7.2

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
        $headers = $csv->fetchOne();
24
        $csv->setOffset(1);
25
26
        $csv->each(function ($row) use ($headers) {
27
            try {
28
                $row = array_combine($headers, $row);
29
            } catch (\Exception $e) {
30
                return true;
31
            }
32
33
            $key = array_get($row, 'key');
34
            array_shift($row);
35
            $data = [];
36
            foreach ($row as $locale => $value) {
37
                $data[$locale] = ['value' => $value];
38
            }
39
40
            $this->translation->updateFromImport($key, $data);
41
42
            return true;
43
        });
44
    }
45
}
46