Test Failed
Push — master ( c9f7be...859573 )
by Gabriel
06:41
created

MediaModels::properties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\MediaLibrary\Support;
4
5
use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperties;
6
use ByTIC\MediaLibrary\Models\MediaRecords\MediaRecords;
7
use Nip\Records\Locator\ModelLocator;
8
9
/**
10
 * Class MediaModels
11
 * @package ByTIC\MediaLibrary\Support
12
 */
13
class MediaModels
14
{
15
    protected static $models = [];
16
17
    /**
18
     * @return MediaRecords
19
     */
20
    static public function records()
21
    {
22
        return static::getModels('records', MediaRecords::class);
23
    }
24
25
    /**
26
     * @return MediaProperties
27
     */
28
    static public function properties()
29
    {
30
        return static::getModels('properties', MediaProperties::class);
31
    }
32
33
    /**
34
     * @param string $type
35
     * @param string $default
36
     * @return mixed|\Nip\Records\AbstractModels\RecordManager
37
     */
38
    static protected function getModels($type, $default)
39
    {
40
        if (!isset(static::$models[$type])) {
41
            $modelManager = static::getConfigVar($type, $default);
42
            return static::$models[$type] = ModelLocator::get($modelManager);
43
        }
44
45
        return static::$models[$type];
46
    }
47
48
    /**
49
     * @param string $type
50
     * @param null|string $default
51
     * @return string
52
     */
53
    static protected function getConfigVar($type, $default = null)
54
    {
55
        if (!function_exists('config')) {
56
            return $default;
57
        }
58
        $varName = 'media-library.media_models.' . $type;
59
        $config = config();
60
        if ($config->has($varName)) {
61
            return $config->get($varName);
62
        }
63
        return $default;
64
    }
65
}
66