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
Branch travis (c4fbe0)
by Mewes
03:50
created

HeaderFooterWrapper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooter;
6
7
/**
8
 * Class HeaderFooterWrapper.
9
 */
10
class HeaderFooterWrapper extends BaseWrapper
11
{
12
    const ALIGNMENT_CENTER = 'center';
13
    const ALIGNMENT_LEFT = 'left';
14
    const ALIGNMENT_RIGHT = 'right';
15
16
    const BASETYPE_FOOTER = 'footer';
17
    const BASETYPE_HEADER = 'header';
18
19
    const TYPE_EVEN = 'even';
20
    const TYPE_FIRST = 'first';
21
    const TYPE_ODD = 'odd';
22
23
    /**
24
     * @var SheetWrapper
25
     */
26
    protected $sheetWrapper;
27
28
    /**
29
     * @var null|HeaderFooter
30
     */
31
    protected $object;
32
    /**
33
     * @var array
34
     */
35
    protected $alignmentParameters;
36
37
    /**
38
     * HeaderFooterWrapper constructor.
39
     *
40
     * @param array             $context
41
     * @param \Twig_Environment $environment
42
     * @param SheetWrapper      $sheetWrapper
43
     */
44
    public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper)
45
    {
46
        parent::__construct($context, $environment);
47
48
        $this->sheetWrapper = $sheetWrapper;
49
50
        $this->object = null;
51
        $this->alignmentParameters = [];
52
    }
53
54
    /**
55
     * @param string $alignment
56
     *
57
     * @throws \InvalidArgumentException
58
     *
59
     * @return string
60
     */
61 View Code Duplication
    public static function validateAlignment(string $alignment): string
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...
62
    {
63
        if (!in_array($alignment, [self::ALIGNMENT_CENTER, self::ALIGNMENT_LEFT, self::ALIGNMENT_RIGHT], true)) {
64
            throw new \InvalidArgumentException(sprintf('Unknown alignment "%s"', $alignment));
65
        }
66
67
        return $alignment;
68
    }
69
70
    /**
71
     * @param string $baseType
72
     *
73
     * @throws \InvalidArgumentException
74
     *
75
     * @return string
76
     */
77 View Code Duplication
    public static function validateBaseType(string $baseType): string
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...
78
    {
79
        if (!in_array($baseType, [self::BASETYPE_FOOTER, self::BASETYPE_HEADER], true)) {
80
            throw new \InvalidArgumentException(sprintf('Unknown base type "%s"', $baseType));
81
        }
82
83
        return $baseType;
84
    }
85
86
    /**
87
     * @param string $type
88
     *
89
     * @throws \InvalidArgumentException
90
     *
91
     * @return string
92
     */
93 View Code Duplication
    public static function validateType(string $type): string
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...
94
    {
95
        if (!in_array($type, [self::TYPE_EVEN, self::TYPE_FIRST, self::TYPE_ODD], true)) {
96
            throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $type));
97
        }
98
99
        return $type;
100
    }
101
102
    /**
103
     * @param string      $baseType
104
     * @param string|null $type
105
     * @param array       $properties
106
     *
107
     * @throws \InvalidArgumentException
108
     * @throws \LogicException
109
     * @throws \RuntimeException
110
     */
111
    public function start(string $baseType, string $type = null, array $properties = [])
112
    {
113
        if ($this->sheetWrapper->getObject() === null) {
114
            throw new \LogicException();
115
        }
116
117
        $this->object = $this->sheetWrapper->getObject()->getHeaderFooter();
118
        $this->parameters['baseType'] = self::validateBaseType(strtolower($baseType));
119
        $this->parameters['type'] = $type !== null ? self::validateType(strtolower($type)) : $type;
120
        $this->parameters['properties'] = $properties;
121
        $this->parameters['value'] = [self::ALIGNMENT_LEFT => null, self::ALIGNMENT_CENTER => null, self::ALIGNMENT_RIGHT => null]; // will be generated by the alignment tags
122
123
        $this->setProperties($properties);
124
    }
125
126
    /**
127
     * @throws \InvalidArgumentException
128
     * @throws \LogicException
129
     */
130
    public function end()
131
    {
132
        if (!$this->object) {
133
            throw new \LogicException();
134
        }
135
136
        $value = implode('', $this->parameters['value']);
137
138
        switch ($this->parameters['type']) {
139
            case null:
140
                if ($this->parameters['baseType'] === self::BASETYPE_HEADER) {
141
                    $this->object->setOddHeader($value);
142
                    $this->object->setEvenHeader($value);
143
                    $this->object->setFirstHeader($value);
144
                } else {
145
                    $this->object->setOddFooter($value);
146
                    $this->object->setEvenFooter($value);
147
                    $this->object->setFirstFooter($value);
148
                }
149
                break;
150 View Code Duplication
            case self::TYPE_EVEN:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
151
                $this->object->setDifferentOddEven(true);
152
                if ($this->parameters['baseType'] === self::BASETYPE_HEADER) {
153
                    $this->object->setEvenHeader($value);
154
                } else {
155
                    $this->object->setEvenFooter($value);
156
                }
157
                break;
158
            case self::TYPE_FIRST:
159
                $this->object->setDifferentFirst(true);
160
                if ($this->parameters['baseType'] === self::BASETYPE_HEADER) {
161
                    $this->object->setFirstHeader($value);
162
                } else {
163
                    $this->object->setFirstFooter($value);
164
                }
165
                break;
166 View Code Duplication
            case self::TYPE_ODD:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
167
                $this->object->setDifferentOddEven(true);
168
                if ($this->parameters['baseType'] === self::BASETYPE_HEADER) {
169
                    $this->object->setOddHeader($value);
170
                } else {
171
                    $this->object->setOddFooter($value);
172
                }
173
                break;
174
        }
175
176
        $this->object = null;
177
        $this->parameters = [];
178
    }
179
180
    /**
181
     * @param string $alignment
182
     * @param array  $properties
183
     *
184
     * @throws \InvalidArgumentException
185
     * @throws \LogicException
186
     */
187
    public function startAlignment(string $alignment, array $properties = [])
188
    {
189
        if (!$this->object) {
190
            throw new \LogicException();
191
        }
192
193
        $alignment = self::validateAlignment(strtolower($alignment));
194
195
        $this->alignmentParameters['type'] = $alignment;
196
        $this->alignmentParameters['properties'] = $properties;
197
198
        switch ($alignment) {
199
            case self::ALIGNMENT_LEFT:
200
                $this->parameters['value'][self::ALIGNMENT_LEFT] = '&L';
201
                break;
202
            case self::ALIGNMENT_CENTER:
203
                $this->parameters['value'][self::ALIGNMENT_CENTER] = '&C';
204
                break;
205
            case self::ALIGNMENT_RIGHT:
206
                $this->parameters['value'][self::ALIGNMENT_RIGHT] = '&R';
207
                break;
208
        }
209
    }
210
211
    /**
212
     * @param string $value
213
     *
214
     * @throws \InvalidArgumentException
215
     * @throws \LogicException
216
     */
217
    public function endAlignment($value)
218
    {
219
        if (!$this->object || !isset($this->alignmentParameters['type'])) {
220
            throw new \LogicException();
221
        }
222
223
        switch ($this->alignmentParameters['type']) {
224 View Code Duplication
            case self::ALIGNMENT_LEFT:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
225
                if (strpos($this->parameters['value'][self::ALIGNMENT_LEFT], '&G') === false) {
226
                    $this->parameters['value'][self::ALIGNMENT_LEFT] .= $value;
227
                }
228
                break;
229 View Code Duplication
            case self::ALIGNMENT_CENTER:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
230
                if (strpos($this->parameters['value'][self::ALIGNMENT_CENTER], '&G') === false) {
231
                    $this->parameters['value'][self::ALIGNMENT_CENTER] .= $value;
232
                }
233
                break;
234 View Code Duplication
            case self::ALIGNMENT_RIGHT:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
235
                if (strpos($this->parameters['value'][self::ALIGNMENT_RIGHT], '&G') === false) {
236
                    $this->parameters['value'][self::ALIGNMENT_RIGHT] .= $value;
237
                }
238
                break;
239
        }
240
241
        $this->alignmentParameters = [];
242
    }
243
244
    /**
245
     * @return null|HeaderFooter
246
     */
247
    public function getObject()
248
    {
249
        return $this->object;
250
    }
251
252
    /**
253
     * @param null|HeaderFooter $object
254
     */
255
    public function setObject(HeaderFooter $object = null)
256
    {
257
        $this->object = $object;
258
    }
259
260
    /**
261
     * @return array
262
     */
263
    public function getAlignmentParameters(): array
264
    {
265
        return $this->alignmentParameters;
266
    }
267
268
    /**
269
     * @param array $alignmentParameters
270
     */
271
    public function setAlignmentParameters(array $alignmentParameters)
272
    {
273
        $this->alignmentParameters = $alignmentParameters;
274
    }
275
276
    /**
277
     * {@inheritdoc}
278
     */
279
    protected function configureMappings(): array
280
    {
281
        return [
282
            'scaleWithDocument' => function ($value) { $this->object->setScaleWithDocument($value); },
283
            'alignWithMargins' => function ($value) { $this->object->setAlignWithMargins($value); },
284
        ];
285
    }
286
}
287