Passed
Push — master ( c0f464...9c6e80 )
by Gabriel
11:21
created

PaymentsModels::tokens()   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\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 ByTIC\Payments\Models\Tokens\Tokens;
9
use ByTIC\Payments\Models\Transactions\Transactions;
10
use Nip\Records\Locator\ModelLocator;
11
use Nip\Records\RecordManager;
12
13
/**
14
 * Class PaymentsModels
15
 * @package ByTIC\Payments\Utility
16
 */
17
class PaymentsModels
18
{
19
    protected static $purchaseModel = 'purchases';
20
    protected static $purchaseSessionsModel = 'purchase-sessions';
21
22
    protected static $models = [];
23
24
    /**
25
     * @return RecordManager
26
     */
27
    public static function purchases()
28
    {
29
        return static::getModels('purchases', 'purchases');
30
    }
31
32
    /**
33
     * @return RecordManager
34
     */
35
    public static function methods()
36
    {
37
        return static::getModels('methods', 'payment-methods');
38
    }
39
40
    /**
41
     * @return PurchaseSessionsTrait
42
     */
43
    public static function sessions()
44
    {
45
        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...
46
    }
47
48
    /**
49
     * @return Transactions
50
     */
51
    public static function transactions()
52
    {
53
        return static::getModels('transactions', 'payments-transactions');
54
    }
55
56
    /**
57
     * @return Tokens
58
     */
59
    public static function tokens()
60
    {
61
        return static::getModels('tokens', 'payments-tokens');
62
    }
63
64
    /**
65
     * @param string $type
66
     * @param string $default
67
     * @return mixed|\Nip\Records\AbstractModels\RecordManager
68
     */
69
    protected static function getModels($type, $default)
70
    {
71
        if (!isset(static::$models[$type])) {
72
            $modelManager = static::getConfigVar($type, $default);
73
            return static::$models[$type] = ModelLocator::get($modelManager);
74
        }
75
76
        return static::$models[$type];
77
    }
78
79
    /**
80
     * @param string $type
81
     * @param null|string $default
82
     * @return string
83
     */
84
    protected static function getConfigVar($type, $default = null)
85
    {
86
        if (!function_exists('config')) {
87
            return $default;
88
        }
89
        $varName = 'payments.models.' . $type;
90
        $config = config();
91
        if ($config->has($varName)) {
92
            return $config->get($varName);
93
        }
94
        return $default;
95
    }
96
}
97