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.
Passed
Push — master ( b43f4a...009a79 )
by Mewes
10:25
created

DrawingWrapper::end()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MewesK\TwigSpreadsheetBundle\Wrapper;
4
5
use MewesK\TwigSpreadsheetBundle\Helper\Filesystem;
6
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
7
use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing;
8
9
/**
10
 * Class DrawingWrapper.
11
 */
12
class DrawingWrapper extends BaseWrapper
13
{
14
    /**
15
     * @var SheetWrapper
16
     */
17
    protected $sheetWrapper;
18
    /**
19
     * @var HeaderFooterWrapper
20
     */
21
    protected $headerFooterWrapper;
22
23
    /**
24
     * @var Drawing|HeaderFooterDrawing|null
25
     */
26
    protected $object;
27
    /**
28
     * @var array
29
     */
30
    protected $attributes;
31
32
    /**
33
     * DrawingWrapper constructor.
34
     *
35
     * @param array               $context
36
     * @param \Twig_Environment   $environment
37
     * @param SheetWrapper        $sheetWrapper
38
     * @param HeaderFooterWrapper $headerFooterWrapper
39
     * @param array             $attributes
40
     */
41 115
    public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper, HeaderFooterWrapper $headerFooterWrapper, array $attributes = [])
42
    {
43 115
        parent::__construct($context, $environment);
44
45 115
        $this->sheetWrapper = $sheetWrapper;
46 115
        $this->headerFooterWrapper = $headerFooterWrapper;
47
48 115
        $this->object = null;
49 115
        $this->attributes = $attributes;
50 115
    }
51
52
    /**
53
     * @param string $path
54
     * @param array  $properties
55
     *
56
     * @throws \LogicException
57
     * @throws \InvalidArgumentException
58
     * @throws \RuntimeException
59
     * @throws \PhpOffice\PhpSpreadsheet\Exception
60
     */
61 6
    public function start(string $path, array $properties = [])
62
    {
63 6
        if ($this->sheetWrapper->getObject() === null) {
64
            throw new \LogicException();
65
        }
66
67
        // create local copy of the asset
68 6
        $tempPath = $this->createTempCopy($path);
69
70
        // add to header/footer
71 6
        if ($this->headerFooterWrapper->getObject()) {
72 1
            $headerFooterParameters = $this->headerFooterWrapper->getParameters();
73 1
            $alignment = $this->headerFooterWrapper->getAlignmentParameters()['type'];
74 1
            $location = '';
75
76
            switch ($alignment) {
77 1
                case HeaderFooterWrapper::ALIGNMENT_CENTER:
78
                    $location .= 'C';
79
                    $headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_CENTER] .= '&G';
80
                    break;
81 1
                case HeaderFooterWrapper::ALIGNMENT_LEFT:
82 1
                    $location .= 'L';
83 1
                    $headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_LEFT] .= '&G';
84 1
                    break;
85 1
                case HeaderFooterWrapper::ALIGNMENT_RIGHT:
86 1
                    $location .= 'R';
87 1
                    $headerFooterParameters['value'][HeaderFooterWrapper::ALIGNMENT_RIGHT] .= '&G';
88 1
                    break;
89
                default:
90
                    throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $alignment));
91
            }
92
93 1
            $location .= $headerFooterParameters['baseType'] === HeaderFooterWrapper::BASETYPE_HEADER ? 'H' : 'F';
94
95 1
            $this->object = new HeaderFooterDrawing();
96 1
            $this->object->setPath($tempPath);
97 1
            $this->headerFooterWrapper->getObject()->addImage($this->object, $location);
98 1
            $this->headerFooterWrapper->setParameters($headerFooterParameters);
99
        }
100
101
        // add to worksheet
102
        else {
103 5
            $this->object = new Drawing();
104 5
            $this->object->setWorksheet($this->sheetWrapper->getObject());
105 5
            $this->object->setPath($tempPath);
106
        }
107
108 6
        $this->setProperties($properties);
109 6
    }
110
111 6
    public function end()
112
    {
113 6
        $this->object = null;
114 6
        $this->parameters = [];
115 6
    }
116
117
    /**
118
     * @return Drawing
119
     */
120
    public function getObject(): Drawing
121
    {
122
        return $this->object;
123
    }
124
125
    /**
126
     * @param Drawing $object
127
     */
128
    public function setObject(Drawing $object)
129
    {
130
        $this->object = $object;
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    protected function configureMappings(): array
137
    {
138
        return [
139
            'coordinates' => function ($value) { $this->object->setCoordinates($value); },
140
            'description' => function ($value) { $this->object->setDescription($value); },
141
            'height' => function ($value) { $this->object->setHeight($value); },
142
            'name' => function ($value) { $this->object->setName($value); },
143
            'offsetX' => function ($value) { $this->object->setOffsetX($value); },
144
            'offsetY' => function ($value) { $this->object->setOffsetY($value); },
145
            'resizeProportional' => function ($value) { $this->object->setResizeProportional($value); },
146
            'rotation' => function ($value) { $this->object->setRotation($value); },
147
            'shadow' => [
148
                'alignment' => function ($value) { $this->object->getShadow()->setAlignment($value); },
149
                'alpha' => function ($value) { $this->object->getShadow()->setAlpha($value); },
150
                'blurRadius' => function ($value) { $this->object->getShadow()->setBlurRadius($value); },
151
                'color' => function ($value) { $this->object->getShadow()->getColor()->setRGB($value); },
152
                'direction' => function ($value) { $this->object->getShadow()->setDirection($value); },
153
                'distance' => function ($value) { $this->object->getShadow()->setDistance($value); },
154
                'visible' => function ($value) { $this->object->getShadow()->setVisible($value); },
155
            ],
156
            'width' => function ($value) { $this->object->setWidth($value); },
157
        ];
158
    }
159
160
    /**
161
     * @param string $path
162
     *
163
     * @throws \InvalidArgumentException
164
     * @throws \Symfony\Component\Filesystem\Exception\IOException
165
     *
166
     * @return string
167
     */
168 6
    private function createTempCopy(string $path): string
169
    {
170
        // create temp path
171 6
        $extension = pathinfo($path, PATHINFO_EXTENSION);
172 6
        $tempPath = sprintf('%s/tsb_%s%s', $this->attributes['cache']['bitmap'], md5($path), $extension ? '.'.$extension : '');
173
174
        // create local copy
175 6
        if (!Filesystem::exists($tempPath)) {
176 2
            $data = file_get_contents($path);
177 2
            if ($data === false) {
178
                throw new \InvalidArgumentException($path.' does not exist.');
179
            }
180 2
            Filesystem::dumpFile($tempPath, $data);
181 2
            unset($data);
182
        }
183
184 6
        return $tempPath;
185
    }
186
}
187