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.
Passed
Push — master ( b43f4a...009a79 )
by Mewes
10:25
created

DocumentWrapper::expandPath()   C

Complexity

Conditions 7
Paths 3

Size

Total Lines 22
Code Lines 10

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