MapHandler::map()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 2
1
<?php declare(strict_types=1);
2
3
namespace FileEye\MimeMap;
4
5
use FileEye\MimeMap\Map\DefaultMap;
6
use FileEye\MimeMap\Map\MimeMapInterface;
7
8
/**
9
 * Class for managing map singletons.
10
 */
11
abstract class MapHandler
12
{
13
    /**
14
     * The default map PHP class.
15
     */
16
    const DEFAULT_MAP_CLASS = DefaultMap::class;
17
18
    /**
19
     * The default map class to use.
20
     *
21
     * It can be overridden by ::setDefaultMapClass.
22
     *
23
     * @var string
24
     */
25
    protected static $defaultMapClass = self::DEFAULT_MAP_CLASS;
26
27
    /**
28
     * Sets a map class as default for new instances.
29
     *
30
     * @param string $map_class A FQCN.
31
     */
32 1
    public static function setDefaultMapClass(string $map_class): void
33
    {
34 1
        static::$defaultMapClass = $map_class;
35
    }
36
37
    /**
38
     * Returns the map instance.
39
     *
40
     * @param string|null $map_class
41
     *   (Optional) The map FQCN to be used. If null, the default map will be
42
     *   used.
43
     */
44 105
    public static function map(string $map_class = null): MimeMapInterface
45
    {
46 105
        if ($map_class === null) {
47 98
            $map_class = static::$defaultMapClass;
48
        }
49 105
        return $map_class::getInstance();
50
    }
51
}
52