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.
Test Failed
Push — master ( 1dd304...6c6f43 )
by Mewes
06:12
created

DocumentWrapper::expandPath()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 10
cts 10
cp 1
rs 8.6346
c 0
b 0
f 0
cc 7
nc 3
nop 1
crap 7
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use MewesK\TwigSpreadsheetBundle\Helper\Filesystem;
6
use PhpOffice\PhpSpreadsheet\Exception;
7
use PhpOffice\PhpSpreadsheet\IOFactory;
8
use PhpOffice\PhpSpreadsheet\Spreadsheet;
9
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter;
10
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf;
11
use Symfony\Bridge\Twig\AppVariable;
12
13
/**
14
 * Class DocumentWrapper.
15
 */
16
class DocumentWrapper extends BaseWrapper
17
{
18
    /**
19
     * @var Spreadsheet|null
20
     */
21
    protected $object;
22
    /**
23
     * @var array
24
     */
25
    protected $attributes;
26
27
    /**
28
     * DocumentWrapper constructor.
29
     *
30
     * @param array             $context
31
     * @param \Twig_Environment $environment
32
     * @param array             $attributes
33
     */
34 112
    public function __construct(array $context, \Twig_Environment $environment, array $attributes = [])
35
    {
36 112
        parent::__construct($context, $environment);
37
38 112
        $this->object = null;
39 112
        $this->attributes = $attributes;
40 112
    }
41
42
    /**
43
     * @param array $properties
44
     *
45
     * @throws \RuntimeException
46
     * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception
47
     * @throws \PhpOffice\PhpSpreadsheet\Exception
48
     */
49 112
    public function start(array $properties = [])
50
    {
51
        // load template
52 112
        if (isset($properties['template'])) {
53 17
            $templatePath = $this->expandPath($properties['template']);
54 17
            $reader = IOFactory::createReaderForFile($templatePath);
55 17
            $this->object = $reader->load($templatePath);
56
        }
57
58
        // create new
59
        else {
60 95
            $this->object = new Spreadsheet();
61 95
            $this->object->removeSheetByIndex(0);
62
        }
63
64 112
        $this->parameters['properties'] = $properties;
65
66 112
        $this->setProperties($properties);
67 112
    }
68
69
    /**
70
     * @throws \LogicException
71
     * @throws \RuntimeException
72
     * @throws \InvalidArgumentException
73
     * @throws \PhpOffice\PhpSpreadsheet\Exception
74
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
75
     * @throws \Symfony\Component\Filesystem\Exception\IOException
76
     */
77 104
    public function end()
78
    {
79 104
        if ($this->object === null) {
80
            throw new \LogicException();
81
        }
82
83 104
        $format = null;
84
85
        // try document property
86 104
        if (isset($this->parameters['format'])) {
87
            $format = $this->parameters['format'];
88
        }
89
90
         // try Symfony request
91 104
        elseif (isset($this->context['app'])) {
92
            /**
93
             * @var AppVariable
94
             */
95 104
            $appVariable = $this->context['app'];
96 104
            if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) {
97 104
                $format = $appVariable->getRequest()->getRequestFormat();
98
            }
99
        }
100
101
        // set default
102 104
        if ($format === null || !\is_string($format)) {
103
            $format = 'xlsx';
104
        } else {
105 104
            $format = strtolower($format);
106
        }
107
108
        // set up mPDF
109 104
        if ($format === 'pdf') {
110 1
            if (!class_exists('\Mpdf\Mpdf')) {
111
                throw new \RuntimeException('Error loading mPDF. Is mPDF correctly installed?');
112
            }
113 1
            IOFactory::registerWriter('Pdf', Mpdf::class);
114
        }
115
116
        /**
117
         * @var BaseWriter $writer
118
         */
119 104
        $writer = IOFactory::createWriter($this->object, ucfirst($format));
120 104
        $writer->setPreCalculateFormulas($this->attributes['preCalculateFormulas'] ?? true);
121
122
        // set up XML cache
123 104
        if ($this->attributes['cache']['xml'] !== false) {
124 25
            Filesystem::mkdir($this->attributes['cache']['xml']);
125 25
            $writer->setUseDiskCaching(true, $this->attributes['cache']['xml']);
126
        }
127
128 104
        $writer->save('php://output');
129
130 104
        $this->object = null;
131 104
        $this->parameters = [];
132 104
    }
133
134
    /**
135
     * @return Spreadsheet|null
136
     */
137 100
    public function getObject()
138
    {
139 100
        return $this->object;
140
    }
141
142
    /**
143
     * @param Spreadsheet|null $object
144
     */
145
    public function setObject(Spreadsheet $object = null)
146
    {
147
        $this->object = $object;
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     *
153
     * @throws \PhpOffice\PhpSpreadsheet\Exception
154
     */
155
    protected function configureMappings(): array
156
    {
157
        return [
158
            'category' => function ($value) { $this->object->getProperties()->setCategory($value); },
159
            'company' => function ($value) { $this->object->getProperties()->setCompany($value); },
160
            'created' => function ($value) { $this->object->getProperties()->setCreated($value); },
161
            'creator' => function ($value) { $this->object->getProperties()->setCreator($value); },
162
            'defaultStyle' => function ($value) { $this->object->getDefaultStyle()->applyFromArray($value); },
163
            'description' => function ($value) { $this->object->getProperties()->setDescription($value); },
164
            'format' => function ($value) { $this->parameters['format'] = $value; },
165
            'keywords' => function ($value) { $this->object->getProperties()->setKeywords($value); },
166
            'lastModifiedBy' => function ($value) { $this->object->getProperties()->setLastModifiedBy($value); },
167
            'manager' => function ($value) { $this->object->getProperties()->setManager($value); },
168
            'modified' => function ($value) { $this->object->getProperties()->setModified($value); },
169
            'security' => [
170
                'lockRevision' => function ($value) { $this->object->getSecurity()->setLockRevision($value); },
171
                'lockStructure' => function ($value) { $this->object->getSecurity()->setLockStructure($value); },
172
                'lockWindows' => function ($value) { $this->object->getSecurity()->setLockWindows($value); },
173
                'revisionsPassword' => function ($value) { $this->object->getSecurity()->setRevisionsPassword($value); },
174
                'workbookPassword' => function ($value) { $this->object->getSecurity()->setWorkbookPassword($value); },
175
            ],
176
            'subject' => function ($value) { $this->object->getProperties()->setSubject($value); },
177
            'template' => function ($value) { $this->parameters['template'] = $value; },
178
            'title' => function ($value) { $this->object->getProperties()->setTitle($value); },
179
        ];
180
    }
181
182
    /**
183
     * Resolves paths using Twig namespaces.
184
     * The path must start with the namespace.
185
     * Namespaces are case sensitive.
186
     *
187
     * @param string $path
188
     *
189
     * @return string
190
     */
191 17
    private function expandPath(string $path): string
192
    {
193 17
        $loader = $this->environment->getLoader();
194
195 17
        if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) {
196
            /*
197
             * @var \Twig_Loader_Filesystem
198
             */
199 11
            foreach ($loader->getNamespaces() as $namespace) {
200 11
                if (mb_strpos($path, $namespace) === 1) {
201 11
                    foreach ($loader->getPaths($namespace) as $namespacePath) {
202 11
                        $expandedPathAttribute = str_replace('@'.$namespace, $namespacePath, $path);
203 11
                        if (Filesystem::exists($expandedPathAttribute)) {
204 11
                            return $expandedPathAttribute;
205
                        }
206
                    }
207
                }
208
            }
209
        }
210
211 6
        return $path;
212
    }
213
}
214