Completed
Push — master ( 974bfd...0f2825 )
by Mewes
02:21
created

XlsCellWrapper::start()   D

Complexity

Conditions 10
Paths 26

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 20
nc 26
nop 3

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace MewesK\TwigExcelBundle\Wrapper;
4
5
use PHPExcel_Cell;
6
use Twig_Environment;
7
8
/**
9
 * Class XlsCellWrapper
10
 *
11
 * @package MewesK\TwigExcelBundle\Wrapper
12
 */
13
class XlsCellWrapper extends AbstractWrapper
14
{
15
    /**
16
     * @var array
17
     */
18
    protected $context;
19
    /**
20
     * @var Twig_Environment
21
     */
22
    protected $environment;
23
    /**
24
     * @var XlsSheetWrapper
25
     */
26
    protected $sheetWrapper;
27
28
    /**
29
     * @var \PHPExcel_Cell
30
     */
31
    protected $object;
32
    /**
33
     * @var array
34
     */
35
    protected $attributes;
36
    /**
37
     * @var array
38
     */
39
    protected $mappings;
40
41
    /**
42
     * XlsCellWrapper constructor.
43
     * 
44
     * @param array $context
45
     * @param Twig_Environment $environment
46
     * @param XlsSheetWrapper $sheetWrapper
47
     */
48
    public function __construct(array $context, Twig_Environment $environment, XlsSheetWrapper $sheetWrapper)
49
    {
50
        $this->context = $context;
51
        $this->environment = $environment;
52
        $this->sheetWrapper = $sheetWrapper;
53
54
        $this->object = null;
55
        $this->attributes = [];
56
        $this->mappings = [];
57
58
        $this->initializeMappings();
59
    }
60
61
    protected function initializeMappings()
62
    {
63
        $this->mappings['break'] = function ($value) {
64
            $this->sheetWrapper->getObject()->setBreak($this->object->getCoordinate(), $value);
65
        };
66
        $this->mappings['dataType'] = function ($value) {
67
            $this->object->setDataType($value);
68
        };
69
        $this->mappings['dataValidation']['allowBlank'] = function ($value) {
70
            $this->object->getDataValidation()->setAllowBlank($value);
71
        };
72
        $this->mappings['dataValidation']['error'] = function ($value) {
73
            $this->object->getDataValidation()->setError($value);
74
        };
75
        $this->mappings['dataValidation']['errorStyle'] = function ($value) {
76
            $this->object->getDataValidation()->setErrorStyle($value);
77
        };
78
        $this->mappings['dataValidation']['errorTitle'] = function ($value) {
79
            $this->object->getDataValidation()->setErrorTitle($value);
80
        };
81
        $this->mappings['dataValidation']['formula1'] = function ($value) {
82
            $this->object->getDataValidation()->setFormula1($value);
83
        };
84
        $this->mappings['dataValidation']['formula2'] = function ($value) {
85
            $this->object->getDataValidation()->setFormula2($value);
86
        };
87
        $this->mappings['dataValidation']['operator'] = function ($value) {
88
            $this->object->getDataValidation()->setOperator($value);
89
        };
90
        $this->mappings['dataValidation']['prompt'] = function ($value) {
91
            $this->object->getDataValidation()->setPrompt($value);
92
        };
93
        $this->mappings['dataValidation']['promptTitle'] = function ($value) {
94
            $this->object->getDataValidation()->setPromptTitle($value);
95
        };
96
        $this->mappings['dataValidation']['showDropDown'] = function ($value) {
97
            $this->object->getDataValidation()->setShowDropDown($value);
98
        };
99
        $this->mappings['dataValidation']['showErrorMessage'] = function ($value) {
100
            $this->object->getDataValidation()->setShowErrorMessage($value);
101
        };
102
        $this->mappings['dataValidation']['showInputMessage'] = function ($value) {
103
            $this->object->getDataValidation()->setShowInputMessage($value);
104
        };
105
        $this->mappings['dataValidation']['type'] = function ($value) {
106
            $this->object->getDataValidation()->setType($value);
107
        };
108
        $this->mappings['merge'] = function ($value) {
109
            if (is_int($value)) {
110
                $value = PHPExcel_Cell::stringFromColumnIndex($value) . $this->sheetWrapper->getRow();
111
            }
112
            $this->sheetWrapper->getObject()->mergeCells(sprintf('%s:%s', $this->object->getCoordinate(), $value));
113
        };
114
        $this->mappings['style'] = function ($value) {
115
            $this->sheetWrapper->getObject()->getStyle($this->object->getCoordinate())->applyFromArray($value);
116
        };
117
        $this->mappings['url'] = function ($value) {
118
            $this->object->getHyperlink()->setUrl($value);
119
        };
120
    }
121
122
    /**
123
     * @param null|int $index
124
     * @param null|mixed $value
125
     * @param null|array $properties
126
     * @throws \PHPExcel_Exception
127
     * @throws \LogicException
128
     * @throws \InvalidArgumentException
129
     */
130
    public function start($index = null, $value = null, array $properties = null)
131
    {
132
        if ($this->sheetWrapper->getObject() === null) {
133
            throw new \LogicException();
134
        }
135
        if ($index !== null && !is_int($index)) {
136
            throw new \InvalidArgumentException('Invalid index');
137
        }
138
139
        if ($index === null) {
140
            $this->sheetWrapper->increaseColumn();
141
        } else {
142
            $this->sheetWrapper->setColumn($index);
143
        }
144
145
        $this->object = $this->sheetWrapper->getObject()->getCellByColumnAndRow($this->sheetWrapper->getColumn(),
146
            $this->sheetWrapper->getRow());
147
148
        if ($value !== null) {
149
            if (array_key_exists('isExplicitValue', $properties) && $properties['isExplicitValue'] === true) {
150
                $this->object->setValueExplicit($value);
151
            } else {
152
                $this->object->setValue($value);
153
            }
154
        }
155
156
        if ($properties !== null) {
157
            $this->setProperties($properties, $this->mappings);
158
        }
159
160
        $this->attributes['value'] = $value;
161
        $this->attributes['properties'] = $properties ?: [];
162
    }
163
164
    public function end()
165
    {
166
        $this->object = null;
167
        $this->attributes = [];
168
    }
169
170
    //
171
    // Getters/Setters
172
    //
173
174
    /**
175
     * @return \PHPExcel_Cell
176
     */
177
    public function getObject()
178
    {
179
        return $this->object;
180
    }
181
182
    /**
183
     * @param \PHPExcel_Cell $object
184
     */
185
    public function setObject($object)
186
    {
187
        $this->object = $object;
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function getAttributes()
194
    {
195
        return $this->attributes;
196
    }
197
198
    /**
199
     * @param array $attributes
200
     */
201
    public function setAttributes($attributes)
202
    {
203
        $this->attributes = $attributes;
204
    }
205
206
    /**
207
     * @return array
208
     */
209
    public function getMappings()
210
    {
211
        return $this->mappings;
212
    }
213
214
    /**
215
     * @param array $mappings
216
     */
217
    public function setMappings($mappings)
218
    {
219
        $this->mappings = $mappings;
220
    }
221
}
222