Passed
Push — master ( a70513...c2cc3a )
by Robin
14:22 queued 13s
created

IMagickSupport::supportsFormat()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 13
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace OC\Preview;
4
5
use OCP\ICache;
6
use OCP\ICacheFactory;
7
8
class IMagickSupport {
9
	private ICache $cache;
10
	private ?\Imagick $imagick;
11
12
	public function __construct(ICacheFactory $cacheFactory) {
13
		$this->cache = $cacheFactory->createLocal('imagick');
14
15
		if (extension_loaded('imagick')) {
16
			$this->imagick = new \Imagick();
17
		} else {
18
			$this->imagick = null;
19
		}
20
	}
21
22
	public function hasExtension(): bool {
23
		return !is_null($this->imagick);
24
	}
25
26
	public function supportsFormat(string $format): bool {
27
		if (is_null($this->imagick)) {
28
			return false;
29
		}
30
31
		$cached = $this->cache->get($format);
32
		if (!is_null($cached)) {
33
			return $cached;
34
		}
35
36
		$formatSupported = count($this->imagick->queryFormats($format)) === 1;
37
		$this->cache->set($format, $cached);
38
		return $formatSupported;
39
	}
40
}
41