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

DrawingWrapper   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 267
Duplicated Lines 4.87 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 7
dl 13
loc 267
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 13 13 1
C start() 0 63 14
A end() 0 5 1
A getMappings() 0 4 1
A setMappings() 0 4 1
A getObject() 0 4 1
A setObject() 0 4 1
A getAttributes() 0 4 1
A setAttributes() 0 4 1
A initializeMappings() 0 51 1
B createTempCopy() 0 25 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
6
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
7
8
/**
9
 * Class DrawingWrapper.
10
 */
11
class DrawingWrapper extends BaseWrapper
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $context;
17
    /**
18
     * @var \Twig_Environment
19
     */
20
    protected $environment;
21
    /**
22
     * @var SheetWrapper
23
     */
24
    protected $sheetWrapper;
25
    /**
26
     * @var HeaderFooterWrapper
27
     */
28
    protected $headerFooterWrapper;
29
30
    /**
31
     * @var Drawing | HeaderFooterDrawing
32
     */
33
    protected $object;
34
    /**
35
     * @var array
36
     */
37
    protected $attributes;
38
    /**
39
     * @var array
40
     */
41
    protected $mappings;
42
43
    /**
44
     * DrawingWrapper constructor.
45
     *
46
     * @param array               $context
47
     * @param \Twig_Environment   $environment
48
     * @param SheetWrapper        $sheetWrapper
49
     * @param HeaderFooterWrapper $headerFooterWrapper
50
     */
51 View Code Duplication
    public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper, HeaderFooterWrapper $headerFooterWrapper)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $this->context = $context;
54
        $this->environment = $environment;
55
        $this->sheetWrapper = $sheetWrapper;
56
        $this->headerFooterWrapper = $headerFooterWrapper;
57
58
        $this->object = null;
59
        $this->attributes = [];
60
        $this->mappings = [];
61
62
        $this->initializeMappings();
63
    }
64
65
    /**
66
     * @param string $path
67
     * @param array  $properties
68
     *
69
     * @throws \LogicException
70
     * @throws \InvalidArgumentException
71
     * @throws \RuntimeException
72
     * @throws \PhpOffice\PhpSpreadsheet\Exception
73
     */
74
    public function start(string $path, array $properties = [])
75
    {
76
        if ($this->sheetWrapper->getObject() === null) {
77
            throw new \LogicException();
78
        }
79
80
        // create local copy of the asset
81
        $tempPath = $this->createTempCopy($path);
82
83
        // add to header/footer
84
        if ($this->headerFooterWrapper->getObject()) {
85
            $headerFooterAttributes = $this->headerFooterWrapper->getAttributes();
86
            $location = '';
87
88
            switch (strtolower($this->headerFooterWrapper->getAlignmentAttributes()['type'])) {
89
                case 'left':
90
                    $location .= 'L';
91
                    $headerFooterAttributes['value']['left'] .= '&G';
92
                    break;
93
                case 'center':
94
                    $location .= 'C';
95
                    $headerFooterAttributes['value']['center'] .= '&G';
96
                    break;
97
                case 'right':
98
                    $location .= 'R';
99
                    $headerFooterAttributes['value']['right'] .= '&G';
100
                    break;
101
                default:
102
                    throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->headerFooterWrapper->getAlignmentAttributes()['type']));
103
            }
104
105
            switch (strtolower($headerFooterAttributes['type'])) {
106
                case 'header':
107
                case 'oddheader':
108
                case 'evenheader':
109
                case 'firstheader':
110
                    $location .= 'H';
111
                    break;
112
                case 'footer':
113
                case 'oddfooter':
114
                case 'evenfooter':
115
                case 'firstfooter':
116
                    $location .= 'F';
117
                    break;
118
                default:
119
                    throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $headerFooterAttributes['type']));
120
            }
121
122
            $this->object = new HeaderFooterDrawing();
123
            $this->object->setPath($tempPath);
124
            $this->headerFooterWrapper->getObject()->addImage($this->object, $location);
125
            $this->headerFooterWrapper->setAttributes($headerFooterAttributes);
126
        }
127
128
        // add to worksheet
129
        else {
130
            $this->object = new Drawing();
131
            $this->object->setWorksheet($this->sheetWrapper->getObject());
132
            $this->object->setPath($tempPath);
133
        }
134
135
        $this->setProperties($properties, $this->mappings);
136
    }
137
138
    public function end()
139
    {
140
        $this->object = null;
141
        $this->attributes = [];
142
    }
143
144
    /**
145
     * @return array
146
     */
147
    public function getMappings(): array
148
    {
149
        return $this->mappings;
150
    }
151
152
    /**
153
     * @param array $mappings
154
     */
155
    public function setMappings(array $mappings)
156
    {
157
        $this->mappings = $mappings;
158
    }
159
160
    /**
161
     * @return Drawing
162
     */
163
    public function getObject(): Drawing
164
    {
165
        return $this->object;
166
    }
167
168
    /**
169
     * @param Drawing $object
170
     */
171
    public function setObject(Drawing $object)
172
    {
173
        $this->object = $object;
174
    }
175
176
    /**
177
     * @return array
178
     */
179
    public function getAttributes(): array
180
    {
181
        return $this->attributes;
182
    }
183
184
    /**
185
     * @param array $attributes
186
     */
187
    public function setAttributes(array $attributes)
188
    {
189
        $this->attributes = $attributes;
190
    }
191
192
    protected function initializeMappings()
193
    {
194
        $this->mappings['coordinates'] = function ($value) {
195
            $this->object->setCoordinates($value);
196
        };
197
        $this->mappings['description'] = function ($value) {
198
            $this->object->setDescription($value);
199
        };
200
        $this->mappings['height'] = function ($value) {
201
            $this->object->setHeight($value);
202
        };
203
        $this->mappings['name'] = function ($value) {
204
            $this->object->setName($value);
205
        };
206
        $this->mappings['offsetX'] = function ($value) {
207
            $this->object->setOffsetX($value);
208
        };
209
        $this->mappings['offsetY'] = function ($value) {
210
            $this->object->setOffsetY($value);
211
        };
212
        $this->mappings['resizeProportional'] = function ($value) {
213
            $this->object->setResizeProportional($value);
214
        };
215
        $this->mappings['rotation'] = function ($value) {
216
            $this->object->setRotation($value);
217
        };
218
        $this->mappings['shadow']['alignment'] = function ($value) {
219
            $this->object->getShadow()->setAlignment($value);
220
        };
221
        $this->mappings['shadow']['alpha'] = function ($value) {
222
            $this->object->getShadow()->setAlpha($value);
223
        };
224
        $this->mappings['shadow']['blurRadius'] = function ($value) {
225
            $this->object->getShadow()->setBlurRadius($value);
226
        };
227
        $this->mappings['shadow']['color'] = function ($value) {
228
            $this->object->getShadow()->getColor()->setRGB($value);
229
        };
230
        $this->mappings['shadow']['direction'] = function ($value) {
231
            $this->object->getShadow()->setDirection($value);
232
        };
233
        $this->mappings['shadow']['distance'] = function ($value) {
234
            $this->object->getShadow()->setDistance($value);
235
        };
236
        $this->mappings['shadow']['visible'] = function ($value) {
237
            $this->object->getShadow()->setVisible($value);
238
        };
239
        $this->mappings['width'] = function ($value) {
240
            $this->object->setWidth($value);
241
        };
242
    }
243
244
    /**
245
     * @param string $path
246
     *
247
     * @throws \RuntimeException
248
     * @throws \InvalidArgumentException
249
     *
250
     * @return string
251
     */
252
    private function createTempCopy(string $path): string
253
    {
254
        // create temp path
255
        $pathExtension = pathinfo($path, PATHINFO_EXTENSION);
256
        $tempPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'xlsdrawing'.'_'.md5($path).($pathExtension ? '.'.$pathExtension : '');
257
258
        // create local copy
259
        if (!file_exists($tempPath)) {
260
            $data = file_get_contents($path);
261
            if ($data === false) {
262
                throw new \InvalidArgumentException($path.' does not exist.');
263
            }
264
            $temp = fopen($tempPath, 'wb+');
265
            if ($temp === false) {
266
                throw new \RuntimeException('Cannot open '.$tempPath);
267
            }
268
            fwrite($temp, $data);
269
            if (fclose($temp) === false) {
270
                throw new \RuntimeException('Cannot close '.$tempPath);
271
            }
272
            unset($data, $temp);
273
        }
274
275
        return $tempPath;
276
    }
277
}
278