Passed
Push — master ( cfa70a...460ec1 )
by Gabriel
11:48
created

PaymentsModels::purchases()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace ByTIC\Payments\Utility;
4
5
use ByTIC\MediaLibrary\Models\MediaProperties\MediaProperties;
6
use ByTIC\MediaLibrary\Models\MediaRecords\MediaRecords;
7
use ByTIC\Payments\Models\PurchaseSessions\PurchaseSessionsTrait;
8
use Nip\Records\Locator\ModelLocator;
9
use Nip\Records\RecordManager;
10
11
/**
12
 * Class PaymentsModels
13
 * @package ByTIC\Payments\Utility
14
 */
15
class PaymentsModels
16
{
17
    protected static $purchaseModel = 'purchases';
18
    protected static $purchaseSessionsModel = 'purchase-sessions';
19
20
    protected static $models = [];
21
22
    /**
23
     * @return RecordManager
24
     */
25
    public static function purchases()
26
    {
27
        return static::getModels('purchases', 'purchases');
28
    }
29
30
    /**
31
     * @return PurchaseSessionsTrait
32
     */
33
    public static function sessions()
34
    {
35
        return static::getModels('purchasesSessions', 'purchase-sessions');
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::getModels...', 'purchase-sessions') also could return the type Nip\Records\AbstractModels\RecordManager which is incompatible with the documented return type ByTIC\Payments\Models\Pu...s\PurchaseSessionsTrait.
Loading history...
36
    }
37
38
    /**
39
     * @param string $type
40
     * @param string $default
41
     * @return mixed|\Nip\Records\AbstractModels\RecordManager
42
     */
43
    protected static function getModels($type, $default)
44
    {
45
        if (!isset(static::$models[$type])) {
46
            $modelManager = static::getConfigVar($type, $default);
47
            return static::$models[$type] = ModelLocator::get($modelManager);
48
        }
49
50
        return static::$models[$type];
51
    }
52
53
    /**
54
     * @param string $type
55
     * @param null|string $default
56
     * @return string
57
     */
58
    protected static function getConfigVar($type, $default = null)
59
    {
60
        if (!function_exists('config')) {
61
            return $default;
62
        }
63
        $varName = 'payments.models.' . $type;
64
        $config = config();
65
        if ($config->has($varName)) {
66
            return $config->get($varName);
67
        }
68
        return $default;
69
    }
70
}
71