GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 829c86...597d51 )
by Alex
01:50
created

ImportService::import()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 9.0808
c 0
b 0
f 0
cc 5
nc 6
nop 3
1
<?php
2
3
namespace Sigmapix\Sonata\ImportBundle\Service;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Port\Csv\CsvReader;
7
use Port\Doctrine\DoctrineWriter;
8
use Port\Excel\ExcelReader;
9
use Port\Steps\StepAggregator;
10
use Sonata\AdminBundle\Admin\AdminInterface;
11
use Symfony\Component\DependencyInjection\ContainerInterface;
12
use Symfony\Component\Form\Form;
13
use Symfony\Component\Form\SubmitButton;
14
use Symfony\Component\HttpFoundation\File\UploadedFile;
15
use Symfony\Component\HttpFoundation\Session\SessionInterface;
16
17
final class ImportService
18
{
19
    /**
20
     * @var EntityManagerInterface
21
     */
22
    private $em;
23
24
    /**
25
     * @var SessionInterface
26
     */
27
    private $session;
28
29
    /**
30
     * @var ContainerInterface
31
     */
32
    private $container;
33
34
    /**
35
     * @var string
36
     */
37
    private $doctrineWriterClass;
38
39
    /**
40
     * ImportService constructor.
41
     *
42
     * @param EntityManagerInterface $em
43
     * @param $session
44
     * @param ContainerInterface $container
45
     * @param string             $doctrineWriterClass
46
     */
47
    public function __construct(EntityManagerInterface $em, $session, ContainerInterface $container, string $doctrineWriterClass)
48
    {
49
        $this->em = $em;
50
        $this->session = $session;
51
        $this->container = $container;
52
        $this->doctrineWriterClass = $doctrineWriterClass;
53
    }
54
55
    /**
56
     * @param UploadedFile $file
57
     *
58
     * @return array
59
     */
60
    public function getHeaders(UploadedFile $file)
61
    {
62
        $reader = $this->getReader($file);
63
        $headers = array_flip($this->fixHeadersEncoding($reader));
64
        array_walk($headers, function (&$v, $k) use ($headers) { $v = $k; });
65
        return $headers;
66
    }
67
68
    private function fixHeadersEncoding($reader)
69
    {
70
        $columnHeaders = array_filter($reader->getColumnHeaders(), function ($h) {return null !== $h; });
71
        $columnHeaders = array_map(
72
            function ($h) {
73
                if (!empty($h) && is_string($h) && mb_detect_encoding($h) !== 'UTF-8') {
74
                    $h = utf8_encode($h);
75
                }
76
                return trim($h);
77
            }, $columnHeaders);
78
        return $columnHeaders;
79
    }
80
81
    /**
82
     * @param UploadedFile   $file
83
     * @param Form           $form
84
     * @param AdminInterface $admin
85
     *
86
     * @return mixed
87
     */
88
    public function import(UploadedFile $file, Form $form, AdminInterface $admin)
89
    {
90
        $mapping = [];
91
        foreach ($form as $f) {
92
            if ($f instanceof SubmitButton) {
93
                continue;
94
            }
95
            $mapping[$f->getName()] = $f->getNormData();
96
        }
97
98
        $reader = $this->getReader($file);
99
        // Replace columnsHeader names with entity field name in our $mapping
100
        $columnHeaders = array_map(function ($h) use ($mapping) {
101
            $k = array_search($h, (array) $mapping, true);
102
            return false === $k ? $h : $k;
103
        }, $this->fixHeadersEncoding($reader));
104
        $reader->setColumnHeaders($columnHeaders);
105
106
        /** @var DoctrineWriter $writer */
107
        $writer = new $this->doctrineWriterClass($this->em, get_class($form->getData()));
108
        if (method_exists($writer, 'setContainer')) {
109
            $writer->setContainer($this->container);
110
        }
111
        $writer->disableTruncate();
112
113
        $workflow = new StepAggregator($reader);
114
        $workflow->addWriter($writer);
115
        $admin->configureImportSteps($workflow);
0 ignored issues
show
Bug introduced by
The method configureImportSteps() does not exist on Sonata\AdminBundle\Admin\AdminInterface. Did you maybe mean configure()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
116
117
        $result = $workflow->process();
118
119
        return $result;
120
    }
121
122
    /**
123
     * Get session.
124
     *
125
     * @return
126
     */
127
    public function getSession()
128
    {
129
        return $this->session;
130
    }
131
132
    /**
133
     * Set session.
134
     *
135
     * @return $this
136
     */
137
    public function setSession($session)
138
    {
139
        $this->session = $session;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @param UploadedFile $file
146
     *
147
     * @return CsvReader|ExcelReader
148
     */
149
    private function getReader(UploadedFile $file)
150
    {
151
        $pathFile = $file->getRealPath();
152
        $fileExtension = $file->guessExtension();
153
        $excelExtensions = ['xls', 'xlsx', 'zip'];
154
155
        if (in_array($fileExtension, $excelExtensions)) {
156
            $reader = new ExcelReader(new \SplFileObject($pathFile), 0, 0, true);
157
        } else {
158
            $reader = new CsvReader(new \SplFileObject($pathFile), ';');
159
            $reader->setHeaderRowNumber(0, CsvReader::DUPLICATE_HEADERS_INCREMENT);
160
        }
161
162
        return $reader;
163
    }
164
}
165