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 Setup Failed
Push — master ( 78f8b7...0f754d )
by Elemér
04:15
created

HeaderFooterWrapper::configureMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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