generateSupportedGateways()   A
last analyzed

Complexity

Conditions 6
Paths 7

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 15
rs 9.2222
1
<?php
2
3
namespace ByTIC\Payments\Gateways\Manager\Traits;
4
5
use ByTIC\Payments\Gateways\GatewaysCollection;
6
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait;
7
use Nip\Container\Container;
8
use Omnipay\Common\AbstractGateway;
9
10
/**
11
 * Trait HasGatewaysCollectionTrait
12
 * @package ByTIC\Payments\Gateways\Manager\Traits
13
 */
14
trait HasGatewaysCollectionTrait
15
{
16
    protected $supportedGateways = null;
17
18
    /**
19
     * Internal factory storage
20
     *
21
     * @var GatewaysCollection|GatewayTrait[]
22
     */
23
    protected $collection;
24
25
    /**
26
     * @param $name
27
     * @return AbstractGateway
28
     */
29
    public static function gateway($name): AbstractGateway
30
    {
31
        return self::instance()->getCollection()->get($name);
32
    }
33
34
35
    /**
36
     * @return GatewaysCollection
37
     */
38
    public static function getAll(): GatewaysCollection
39
    {
40
        return self::instance()->getCollection();
41
    }
42
43
    /**
44
     * Get the gateway factory
45
     *
46
     * Creates a new empty GatewayFactory if none has been set previously.
47
     *
48
     * @return GatewaysCollection A GatewayFactory instance
49
     */
50
    public function getCollection(): GatewaysCollection
51
    {
52
        if (is_null($this->collection)) {
0 ignored issues
show
introduced by
The condition is_null($this->collection) is always false.
Loading history...
53
            $this->collection = new GatewaysCollection;
54
            $this->initCollection();
55
        }
56
57
        return $this->collection;
58
    }
59
60
    protected function initCollection()
61
    {
62
        $gatewayNames = self::getSupportedGateways();
0 ignored issues
show
Bug Best Practice introduced by
The method ByTIC\Payments\Gateways\...:getSupportedGateways() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $gatewayNames = self::getSupportedGateways();
Loading history...
63
        $collection = self::getCollection();
0 ignored issues
show
Bug Best Practice introduced by
The method ByTIC\Payments\Gateways\...nTrait::getCollection() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
        /** @scrutinizer ignore-call */ 
64
        $collection = self::getCollection();
Loading history...
64
        foreach ($gatewayNames as $gatewayClass) {
65
            $gateway = $this->getFactory()->create($gatewayClass);
0 ignored issues
show
Bug introduced by
It seems like getFactory() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            $gateway = $this->/** @scrutinizer ignore-call */ getFactory()->create($gatewayClass);
Loading history...
66
            $collection->offsetSet($gateway->getName(), $gateway);
67
        }
68
    }
69
70
    /**
71
     * Get a list of supported gateways which may be available
72
     *
73
     * @return array
74
     */
75
    public function getSupportedGateways(): array
76
    {
77
        if ($this->supportedGateways === null) {
78
            $this->supportedGateways = $this->generateSupportedGateways();
79
        }
80
        return $this->supportedGateways;
81
    }
82
83
    protected function generateSupportedGateways(): array
84
    {
85
        if (!function_exists('config') || !function_exists('app')) {
86
            return $this->generateSupportedGatewaysGeneric();
87
        }
88
        $container = function_exists('app') ? app() : Container::getInstance();
89
        if (!$container->has('config')) {
90
            return $this->generateSupportedGatewaysGeneric();
91
        }
92
        $config = $container->get('config');
93
94
        if ($config->has('payments.gateways')) {
95
            return $config->get('payments.gateways')->toArray();
96
        }
97
        return $this->generateSupportedGatewaysGeneric();
98
    }
99
100
    protected function generateSupportedGatewaysGeneric(): array
101
    {
102
        return [
103
            'Payu',
104
            'Mobilpay',
105
            'Euplatesc',
106
            'Librapay',
107
            'Romcard',
108
            'Twispay',
109
            'Paylike',
110
            'PlatiOnline'
111
        ];
112
    }
113
}
114