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 ( dea5e9...4057a0 )
by Mewes
02:21
created

DocumentWrapper::expandPath()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 10
nc 3
nop 1
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use PhpOffice\PhpSpreadsheet\Exception;
6
use PhpOffice\PhpSpreadsheet\IOFactory;
7
use PhpOffice\PhpSpreadsheet\Settings;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter;
10
use Symfony\Bridge\Twig\AppVariable;
11
12
/**
13
 * Class DocumentWrapper.
14
 */
15
class DocumentWrapper extends BaseWrapper
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $context;
21
    /**
22
     * @var \Twig_Environment
23
     */
24
    protected $environment;
25
26
    /**
27
     * @var Spreadsheet|null
28
     */
29
    protected $object;
30
    /**
31
     * @var array
32
     */
33
    protected $attributes;
34
    /**
35
     * @var array
36
     */
37
    protected $mappings;
38
39
    /**
40
     * DocumentWrapper constructor.
41
     *
42
     * @param array             $context
43
     * @param \Twig_Environment $environment
44
     */
45
    public function __construct(array $context, \Twig_Environment $environment)
46
    {
47
        $this->context = $context;
48
        $this->environment = $environment;
49
50
        $this->object = null;
51
        $this->attributes = [];
52
        $this->mappings = [];
53
54
        $this->initializeMappings();
55
    }
56
57
    /**
58
     * @param array $properties
59
     *
60
     * @throws \RuntimeException
61
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
62
     * @throws \PhpOffice\PhpSpreadsheet\Exception
63
     */
64
    public function start(array $properties = [])
65
    {
66
        // load template
67
        if (isset($properties['template'])) {
68
            $templatePath = $this->expandPath($properties['template']);
69
            $reader = IOFactory::createReaderForFile($templatePath);
70
            $this->object = $reader->load($templatePath);
71
        }
72
73
        // create new
74
        else {
75
            $this->object = new Spreadsheet();
76
            $this->object->removeSheetByIndex(0);
77
        }
78
79
        $this->attributes['properties'] = $properties;
80
81
        $this->setProperties($properties, $this->mappings);
82
    }
83
84
    /**
85
     * @param bool        $preCalculateFormulas
86
     * @param null|string $diskCachingDirectory
87
     *
88
     * @throws \InvalidArgumentException
89
     * @throws \PhpOffice\PhpSpreadsheet\Exception
90
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
91
     */
92
    public function end(bool $preCalculateFormulas = true, string $diskCachingDirectory = null)
93
    {
94
        $format = null;
95
96
        // try document property
97
        if (isset($this->attributes['format'])) {
98
            $format = $this->attributes['format'];
99
        }
100
101
         // try Symfony request
102
        elseif (isset($this->context['app'])) {
103
            /**
104
             * @var AppVariable
105
             */
106
            $appVariable = $this->context['app'];
107
            if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) {
108
                $format = $appVariable->getRequest()->getRequestFormat();
109
            }
110
        }
111
112
        // set default
113
        if ($format === null || !is_string($format)) {
114
            $format = 'xlsx';
115
        }
116
117
        switch (strtolower($format)) {
118
            case 'csv':
119
                $writerType = 'Csv';
120
                break;
121
            case 'ods':
122
                $writerType = 'Ods';
123
                break;
124
            case 'pdf':
125
                $writerType = 'Pdf';
126
                if (!class_exists('mPDF')) {
127
                    throw new Exception('Error loading mPDF. Is mPDF correctly installed?');
128
                }
129
                Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF);
130
                break;
131
            case 'xls':
132
                $writerType = 'Xls';
133
                break;
134
            case 'xlsx':
135
                $writerType = 'Xlsx';
136
                break;
137
            default:
138
                throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format));
139
        }
140
141
        /**
142
         * @var BaseWriter $writer
143
         */
144
        $writer = IOFactory::createWriter($this->object, $writerType);
0 ignored issues
show
Bug introduced by
It seems like $this->object can be null; however, createWriter() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
145
        $writer->setPreCalculateFormulas($preCalculateFormulas);
146
        $writer->setUseDiskCaching($diskCachingDirectory !== null, $diskCachingDirectory);
147
        $writer->save('php://output');
148
149
        $this->object = null;
150
        $this->attributes = [];
151
    }
152
153
    /**
154
     * @return Spreadsheet|null
155
     */
156
    public function getObject()
157
    {
158
        return $this->object;
159
    }
160
161
    /**
162
     * @param Spreadsheet|null $object
163
     */
164
    public function setObject(Spreadsheet $object = null)
165
    {
166
        $this->object = $object;
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getAttributes(): array
173
    {
174
        return $this->attributes;
175
    }
176
177
    /**
178
     * @param array $attributes
179
     */
180
    public function setAttributes(array $attributes)
181
    {
182
        $this->attributes = $attributes;
183
    }
184
185
    /**
186
     * @return array
187
     */
188
    public function getMappings(): array
189
    {
190
        return $this->mappings;
191
    }
192
193
    /**
194
     * @param array $mappings
195
     */
196
    public function setMappings(array $mappings)
197
    {
198
        $this->mappings = $mappings;
199
    }
200
201
    protected function initializeMappings()
202
    {
203
        $this->mappings['category'] = function ($value) {
204
            $this->object->getProperties()->setCategory($value);
205
        };
206
        $this->mappings['company'] = function ($value) {
207
            $this->object->getProperties()->setCompany($value);
208
        };
209
        $this->mappings['created'] = function ($value) {
210
            $this->object->getProperties()->setCreated($value);
211
        };
212
        $this->mappings['creator'] = function ($value) {
213
            $this->object->getProperties()->setCreator($value);
214
        };
215
        $this->mappings['defaultStyle'] = function ($value) {
216
            $this->object->getDefaultStyle()->applyFromArray($value);
217
        };
218
        $this->mappings['description'] = function ($value) {
219
            $this->object->getProperties()->setDescription($value);
220
        };
221
        $this->mappings['format'] = function ($value) {
222
            $this->attributes['format'] = $value;
223
        };
224
        $this->mappings['keywords'] = function ($value) {
225
            $this->object->getProperties()->setKeywords($value);
226
        };
227
        $this->mappings['lastModifiedBy'] = function ($value) {
228
            $this->object->getProperties()->setLastModifiedBy($value);
229
        };
230
        $this->mappings['manager'] = function ($value) {
231
            $this->object->getProperties()->setManager($value);
232
        };
233
        $this->mappings['modified'] = function ($value) {
234
            $this->object->getProperties()->setModified($value);
235
        };
236
        $this->mappings['security']['lockRevision'] = function ($value) {
237
            $this->object->getSecurity()->setLockRevision($value);
238
        };
239
        $this->mappings['security']['lockStructure'] = function ($value) {
240
            $this->object->getSecurity()->setLockStructure($value);
241
        };
242
        $this->mappings['security']['lockWindows'] = function ($value) {
243
            $this->object->getSecurity()->setLockWindows($value);
244
        };
245
        $this->mappings['security']['revisionsPassword'] = function ($value) {
246
            $this->object->getSecurity()->setRevisionsPassword($value);
247
        };
248
        $this->mappings['security']['workbookPassword'] = function ($value) {
249
            $this->object->getSecurity()->setWorkbookPassword($value);
250
        };
251
        $this->mappings['subject'] = function ($value) {
252
            $this->object->getProperties()->setSubject($value);
253
        };
254
        $this->mappings['template'] = function ($value) {
255
            $this->attributes['template'] = $value;
256
        };
257
        $this->mappings['title'] = function ($value) {
258
            $this->object->getProperties()->setTitle($value);
259
        };
260
    }
261
262
    /**
263
     * Resolves paths using Twig namespaces.
264
     * The path must start with the namespace.
265
     * Namespaces are case sensitive.
266
     *
267
     * @param string $path
268
     *
269
     * @return string
270
     */
271
    private function expandPath(string $path): string
272
    {
273
        $loader = $this->environment->getLoader();
274
275
        if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) {
276
            /*
277
             * @var \Twig_Loader_Filesystem
278
             */
279
            foreach ($loader->getNamespaces() as $namespace) {
280
                if (mb_strpos($path, $namespace) === 1) {
281
                    foreach ($loader->getPaths($namespace) as $namespacePath) {
282
                        $expandedPathAttribute = str_replace('@'.$namespace, $namespacePath, $path);
283
                        if (file_exists($expandedPathAttribute)) {
284
                            return $expandedPathAttribute;
285
                        }
286
                    }
287
                }
288
            }
289
        }
290
291
        return $path;
292
    }
293
}
294