Passed
Push — master ( 96d33b...43186b )
by Gabriel
13:27
created

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