XlsDrawingWrapper::getMappings()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace MewesK\TwigExcelBundle\Wrapper;
4
use Twig_Environment;
5
6
/**
7
 * Class XlsDrawingWrapper
8
 *
9
 * @package MewesK\TwigExcelBundle\Wrapper
10
 */
11
class XlsDrawingWrapper extends AbstractWrapper
12
{
13
    /**
14
     * @var array
15
     */
16
    protected $context;
17
    /**
18
     * @var Twig_Environment
19
     */
20
    protected $environment;
21
    /**
22
     * @var XlsSheetWrapper
23
     */
24
    protected $sheetWrapper;
25
    /**
26
     * @var XlsHeaderFooterWrapper
27
     */
28
    protected $headerFooterWrapper;
29
30
    /**
31
     * @var \PHPExcel_Worksheet_Drawing | \PHPExcel_Worksheet_HeaderFooterDrawing
32
     */
33
    protected $object;
34
    /**
35
     * @var array
36
     */
37
    protected $attributes;
38
    /**
39
     * @var array
40
     */
41
    protected $mappings;
42
43
    /**
44
     * XlsDrawingWrapper constructor.
45
     * 
46
     * @param array $context
47
     * @param Twig_Environment $environment
48
     * @param XlsSheetWrapper $sheetWrapper
49
     * @param XlsHeaderFooterWrapper $headerFooterWrapper
50
     */
51
    public function __construct(array $context, Twig_Environment $environment, XlsSheetWrapper $sheetWrapper, XlsHeaderFooterWrapper $headerFooterWrapper)
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
    protected function initializeMappings()
66
    {
67
        $this->mappings['coordinates'] = function ($value) {
68
            $this->object->setCoordinates($value);
69
        };
70
        $this->mappings['description'] = function ($value) {
71
            $this->object->setDescription($value);
72
        };
73
        $this->mappings['height'] = function ($value) {
74
            $this->object->setHeight($value);
75
        };
76
        $this->mappings['name'] = function ($value) {
77
            $this->object->setName($value);
78
        };
79
        $this->mappings['offsetX'] = function ($value) {
80
            $this->object->setOffsetX($value);
81
        };
82
        $this->mappings['offsetY'] = function ($value) {
83
            $this->object->setOffsetY($value);
84
        };
85
        $this->mappings['resizeProportional'] = function ($value) {
86
            $this->object->setResizeProportional($value);
87
        };
88
        $this->mappings['rotation'] = function ($value) {
89
            $this->object->setRotation($value);
90
        };
91
        $this->mappings['shadow']['alignment'] = function ($value) {
92
            $this->object->getShadow()->setAlignment($value);
93
        };
94
        $this->mappings['shadow']['alpha'] = function ($value) {
95
            $this->object->getShadow()->setAlpha($value);
96
        };
97
        $this->mappings['shadow']['blurRadius'] = function ($value) {
98
            $this->object->getShadow()->setBlurRadius($value);
99
        };
100
        $this->mappings['shadow']['color'] = function ($value) {
101
            $this->object->getShadow()->getColor()->setRGB($value);
102
        };
103
        $this->mappings['shadow']['direction'] = function ($value) {
104
            $this->object->getShadow()->setDirection($value);
105
        };
106
        $this->mappings['shadow']['distance'] = function ($value) {
107
            $this->object->getShadow()->setDistance($value);
108
        };
109
        $this->mappings['shadow']['visible'] = function ($value) {
110
            $this->object->getShadow()->setVisible($value);
111
        };
112
        $this->mappings['width'] = function ($value) {
113
            $this->object->setWidth($value);
114
        };
115
    }
116
117
    /**
118
     * @param $path
119
     * @param array|null $properties
120
     * @throws \PHPExcel_Exception
121
     * @throws \LogicException
122
     * @throws \InvalidArgumentException
123
     * @throws \RuntimeException
124
     */
125
    public function start($path, array $properties = null)
126
    {
127
        if ($this->sheetWrapper->getObject() === null) {
128
            throw new \LogicException();
129
        }
130
131
        // create local copy of the asset
132
        $tempPath = $this->createTempCopy($path);
133
134
        // add to header/footer
135
        if ($this->headerFooterWrapper->getObject()) {
136
            $headerFooterAttributes = $this->headerFooterWrapper->getAttributes();
137
            $location = '';
138
139
            switch (strtolower($this->headerFooterWrapper->getAlignmentAttributes()['type'])) {
140
                case 'left':
141
                    $location .= 'L';
142
                    $headerFooterAttributes['value']['left'] .= '&G';
143
                    break;
144
                case 'center':
145
                    $location .= 'C';
146
                    $headerFooterAttributes['value']['center'] .= '&G';
147
                    break;
148
                case 'right':
149
                    $location .= 'R';
150
                    $headerFooterAttributes['value']['right'] .= '&G';
151
                    break;
152
                default:
153
                    throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->headerFooterWrapper->getAlignmentAttributes()['type']));
154
            }
155
156
            switch (strtolower($headerFooterAttributes['type'])) {
157
                case 'header':
158
                case 'oddheader':
159
                case 'evenheader':
160
                case 'firstheader':
161
                    $location .= 'H';
162
                    break;
163
                case 'footer':
164
                case 'oddfooter':
165
                case 'evenfooter':
166
                case 'firstfooter':
167
                    $location .= 'F';
168
                    break;
169
                default:
170
                    throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $headerFooterAttributes['type']));
171
            }
172
173
            $this->object = new \PHPExcel_Worksheet_HeaderFooterDrawing();
174
            $this->object->setPath($tempPath);
175
            $this->headerFooterWrapper->getObject()->addImage($this->object, $location);
176
            $this->headerFooterWrapper->setAttributes($headerFooterAttributes);
177
        }
178
179
        // add to worksheet
180
        else {
181
            $this->object = new \PHPExcel_Worksheet_Drawing();
182
            $this->object->setWorksheet($this->sheetWrapper->getObject());
183
            $this->object->setPath($tempPath);
184
        }
185
186
        if ($properties !== null) {
187
            $this->setProperties($properties, $this->mappings);
188
        }
189
    }
190
191
    public function end()
192
    {
193
        $this->object = null;
194
        $this->attributes = [];
195
    }
196
197
    //
198
    // Helpers
199
    //
200
201
    /**
202
     * @param $path
203
     * @return string
204
     * @throws \RuntimeException
205
     * @throws \InvalidArgumentException
206
     */
207
    private function createTempCopy($path)
208
    {
209
        // create temp path
210
        $pathExtension = pathinfo($path, PATHINFO_EXTENSION);
211
        $tempPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'xlsdrawing' . '_' . md5($path) . ($pathExtension ? '.' . $pathExtension : '');
212
213
        // create local copy
214
        if (!file_exists($tempPath)) {
215
            $data = file_get_contents($path);
216
            if ($data === false) {
217
                throw new \InvalidArgumentException($path . ' does not exist.');
218
            }
219
            $temp = fopen($tempPath, 'w+');
220
            if ($temp === false) {
221
                throw new \RuntimeException('Cannot open ' . $tempPath);
222
            }
223
            fwrite($temp, $data);
224
            if (fclose($temp) === false) {
225
                throw new \RuntimeException('Cannot close ' . $tempPath);
226
            }
227
            unset($data, $temp);
228
        }
229
230
        return $tempPath;
231
    }
232
233
    //
234
    // Getters/Setters
235
    //
236
237
    /**
238
     * @return array
239
     */
240
    public function getMappings()
241
    {
242
        return $this->mappings;
243
    }
244
245
    /**
246
     * @param array $mappings
247
     */
248
    public function setMappings($mappings)
249
    {
250
        $this->mappings = $mappings;
251
    }
252
253
    /**
254
     * @return \PHPExcel_Worksheet_Drawing|\PHPExcel_Worksheet_HeaderFooterDrawing
255
     */
256
    public function getObject()
257
    {
258
        return $this->object;
259
    }
260
261
    /**
262
     * @param \PHPExcel_Worksheet_Drawing|\PHPExcel_Worksheet_HeaderFooterDrawing $object
263
     */
264
    public function setObject($object)
265
    {
266
        $this->object = $object;
267
    }
268
269
    /**
270
     * @return array
271
     */
272
    public function getAttributes()
273
    {
274
        return $this->attributes;
275
    }
276
277
    /**
278
     * @param array $attributes
279
     */
280
    public function setAttributes($attributes)
281
    {
282
        $this->attributes = $attributes;
283
    }
284
}
285