Passed
Push — master ( a30cf1...9051a7 )
by Gabriel
14:38
created

HasGatewaysCollectionTrait::initCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

63
        /** @scrutinizer ignore-call */ 
64
        $gatewayNames = self::getSupportedGateways();
Loading history...
64
        $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

64
        /** @scrutinizer ignore-call */ 
65
        $collection = self::getCollection();
Loading history...
65
        foreach ($gatewayNames as $gatewayClass) {
66
            $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

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