SizeValidator::isBelowLimit()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 2
b 1
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ApacheSolrForTypo3\Tika\Service\File;
6
7
/*
8
 * This file is part of the TYPO3 CMS project.
9
 *
10
 * It is free software; you can redistribute it and/or modify it under
11
 * the terms of the GNU General Public License, either version 2
12
 * of the License, or any later version.
13
 *
14
 * For the full copyright and license information, please read the
15
 * LICENSE.txt file that was distributed with this source code.
16
 *
17
 * The TYPO3 project - inspiring people to share!
18
 */
19
20
use ApacheSolrForTypo3\Tika\Util;
21
use TYPO3\CMS\Core\Resource\FileInterface;
22
23
/**
24
 * Class SizeValidator
25
 *
26
 * @author Timo Hund <[email protected]>
27
 */
28
class SizeValidator
29
{
30
    /**
31
     * @var array
32
     */
33
    protected array $configuration;
34
35
    /**
36
     * Constructor
37
     * @param array|null $extensionConfiguration
38
     */
39 3
    public function __construct(array $extensionConfiguration = null)
40
    {
41 3
        $this->configuration = $extensionConfiguration ?? Util::getTikaExtensionConfiguration();
42
    }
43
44
    /**
45
     * @param FileInterface $file
46
     * @return bool
47
     */
48 2
    public function isBelowLimit(FileInterface $file)
49
    {
50 2
        return $file->getSize() < $this->getFileSizeLimit();
51
    }
52
53
    /**
54
     * Retrieves the size limit in byte when a text extraction on a file is done.
55
     *
56
     * Default value is 500MB.
57
     *
58
     * @return int
59
     */
60 2
    protected function getFileSizeLimit()
61
    {
62
        // default is 500 MB
63 2
        $bytesPerMegaByte = 1048576;
64 2
        $textExtractMegaBytes = (int)$this->getConfigurationOrDefaultValue('fileSizeLimit', 500);
65 2
        return $textExtractMegaBytes * $bytesPerMegaByte;
66
    }
67
68
    /**
69
     * @param string $key
70
     * @param mixed $defaultValue
71
     * @return mixed
72
     */
73 2
    protected function getConfigurationOrDefaultValue(string $key, $defaultValue)
74
    {
75 2
        return $this->configuration[$key] ?? $defaultValue;
76
    }
77
}
78