Test Failed
Push — master ( 817d84...d52e3d )
by Raffael
05:44
created

Office::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Office\Converter\Adapter;
13
14
use Balloon\Converter\Adapter\AdapterInterface;
15
use Balloon\Converter\Exception;
16
use Balloon\Filesystem\Node\File;
17
use GuzzleHttp\ClientInterface as GuzzleHttpClientInterface;
18
use GuzzleHttp\Psr7\StreamWrapper;
19
use Imagick;
20
use Psr\Log\LoggerInterface;
21
22
class Office implements AdapterInterface
23
{
24
    /**
25
     * Preview format.
26
     */
27
    const PREVIEW_FORMAT = 'png';
28
29
    /**
30
     * GuzzleHttpClientInterface.
31
     *
32
     * @var GuzzleHttpClientInterface
33
     */
34
    protected $client;
35
36
    /**
37
     * preview max size.
38
     *
39
     * @var int
40
     */
41
    protected $preview_max_size = 500;
42
43
    /**
44
     * LoggerInterface.
45
     *
46
     * @var LoggerInterface
47
     */
48
    protected $logger;
49
50
    /**
51
     * Formats.
52
     *
53
     * @var array
54
     */
55
    protected $formats = [
56
        'spreadsheet' => [
57
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
58
            'fods' => 'application/vnd.oasis.opendocument.spreadsheet-flat-xml',
59
            'xls' => 'application/vnd.ms-excel',
60
            'xlb' => 'application/vnd.ms-excel',
61
            'xlt' => 'application/vnd.ms-excel',
62
            'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
63
            'xlsm' => 'application/vnd.ms-excel.sheet.macroEnabled.12',
64
            'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
65
            'csv' => 'text/csv',
66
            'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12',
67
            'xslb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12',
68
            'xltm' => 'application/vnd.ms-excel.template.macroEnabled.12',
69
            'pdf' => 'application/pdf',
70
        ],
71
        'text' => [
72
            'odt' => 'application/vnd.oasis.opendocument.text',
73
            'fodt' => 'application/vnd.oasis.opendocument.text-flat-xml',
74
            'ott' => 'application/vnd.oasis.opendocument.text-template',
75
            'oth' => 'application/vnd.oasis.opendocument.text-web',
76
            'odm' => 'application/vnd.oasis.opendocument.text-master',
77
            'otm' => 'application/vnd.oasis.opendocument.text-master-template',
78
            'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
79
            'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template',
80
            'doc' => 'application/msword',
81
            'dot' => 'application/msword',
82
            'docm' => 'application/vnd.ms-word.document.macroEnabled.12',
83
            'dotm' => 'application/vnd.ms-word.template.macroEnabled.12',
84
            'txt' => 'text/plain',
85
            'html' => 'text/html',
86
            'pdf' => 'application/pdf',
87
        ],
88
        'presentation' => [
89
            'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
90
            'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide',
91
            'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow',
92
            'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template',
93
            'ppam' => 'application/vnd.ms-powerpoint.addin.macroEnabled.12',
94
            'pptm' => 'application/vnd.ms-powerpoint.presentation.macroEnabled.12',
95
            'sldm' => 'application/vnd.ms-powerpoint.slide.macroEnabled.12',
96
            'ppsm' => 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12',
97
            'potm' => 'application/vnd.ms-powerpoint.template.macroEnabled.12',
98
            'odp' => 'application/vnd.oasis.opendocument.presentation',
99
            'fodp' => 'application/vnd.oasis.opendocument.presentation-flat-xml',
100
            'otp' => 'application/vnd.oasis.opendocument.presentation-template',
101
            'pdf' => 'application/pdf',
102
        ],
103
    ];
104
105
    /**
106
     * One way formats.
107
     *
108
     * @param array
109
     */
110
    protected $locked_formats = [
111
        'pdf' => 'application/pdf',
112
    ];
113
114
    /**
115
     * Initialize.
116
     */
117
    public function __construct(GuzzleHttpClientInterface $client, LoggerInterface $logger, array $config = [])
118
    {
119
        $this->client = $client;
120
        $this->logger = $logger;
121
        $this->setOptions($config);
122
    }
123
124
    /**
125
     * Set options.
126
     */
127 View Code Duplication
    public function setOptions(array $config = []): AdapterInterface
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...
128
    {
129
        foreach ($config as $option => $value) {
130
            switch ($option) {
131
                case 'preview_max_size':
132
                    $this->preview_max_size = (int) $value;
133
134
                    break;
135
                default:
136
                    throw new Exception('invalid option '.$option.' given');
137
            }
138
        }
139
140
        return $this;
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function match(File $file): bool
147
    {
148 View Code Duplication
        foreach ($this->formats as $type => $formats) {
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...
149
            if (in_array($file->getContentType(), $formats, true)) {
150
                return true;
151
            }
152
        }
153
154
        return false;
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160
    public function matchPreview(File $file): bool
161
    {
162
        return $this->match($file);
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function getSupportedFormats(File $file): array
169
    {
170 View Code Duplication
        if (in_array($file->getContentType(), $this->locked_formats, true)) {
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...
171
            return [
172
                array_search($file->getContentType(), $this->locked_formats, true),
173
            ];
174
        }
175
176 View Code Duplication
        foreach ($this->formats as $type => $formats) {
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...
177
            if (in_array($file->getContentType(), $formats, true)) {
178
                return array_keys($formats);
179
            }
180
        }
181
182
        return [];
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     */
188
    public function createPreview(File $file)
189
    {
190
        return $this->createPreviewFromStream($file->get());
191
    }
192
193
    /**
194
     * {@inheritdoc}
195
     */
196
    public function convert(File $file, string $format)
197
    {
198
        return $this->convertFromStream($file->get(), $format);
199
    }
200
201
    /**
202
     * {@inheritdoc}
203
     */
204 View Code Duplication
    protected function createPreviewFromStream($stream)
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...
205
    {
206
        try {
207
            $result = $this->convertFromStream($stream, self::PREVIEW_FORMAT);
208
        } catch (Exception $e) {
209
            throw new Exception('failed create preview');
210
        }
211
212
        $desth = tmpfile();
213
        stream_copy_to_stream($result, $desth);
214
        $dest = stream_get_meta_data($desth)['uri'];
215
        $image = new Imagick($dest);
216
217
        $width = $image->getImageWidth();
218
        $height = $image->getImageHeight();
219
220
        if ($height <= $width && $width > $this->preview_max_size) {
221
            $image->scaleImage($this->preview_max_size, 0);
222
        } elseif ($height > $this->preview_max_size) {
223
            $image->scaleImage(0, $this->preview_max_size);
224
        }
225
226
        $image->writeImage($dest);
227
228
        rewind($desth);
229
230
        return $desth;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 View Code Duplication
    protected function convertFromStream($stream, string $format)
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...
237
    {
238
        $this->logger->debug('execute convert-to from [/lool/convert-to/'.$format.']', [
239
            'category' => get_class($this),
240
        ]);
241
242
        $response = $this->client->request(
243
            'POST',
244
            'lool/convert-to/'.$format,
245
            [
246
                'multipart' => [
247
                    [
248
                        'name' => 'data',
249
                        'contents' => $stream,
250
                        'filename' => 'data',
251
                    ],
252
                ],
253
            ]
254
        );
255
256
        $this->logger->debug('convert-to request ended with status code ['.$response->getStatusCode().']', [
257
            'category' => get_class($this),
258
        ]);
259
260
        return StreamWrapper::getResource($response->getBody());
261
    }
262
}
263