Completed
Pull Request — master (#71)
by Olatunbosun
16:04
created

PaystackFactory::make()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Laravel Paystack package.
7
 *
8
 * (c) Prosper Otemuyiwa <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Unicodeveloper\Paystack;
15
16
17
use Closure;
18
use Illuminate\Contracts\Cache\Factory;
19
use Madewithlove\IlluminatePsrCacheBridge\Laravel\CacheItemPool;
20
use Unicodeveloper\Paystack\Event\EventHandler;
21
use Unicodeveloper\Paystack\Http\ClientBuilder;
22
use Xeviant\Paystack\App\PaystackApplication;
23
use Xeviant\Paystack\Client;
24
use Xeviant\Paystack\Config;
25
use Xeviant\Paystack\Contract\Config as PaystackConfigContract;
26
use Xeviant\Paystack\Exception\InvalidArgumentException;
27
use Xeviant\Paystack\HttpClient\Builder;
28
29
class PaystackFactory
30
{
31
    /**
32
     * Laravel Cache Instance
33
     *
34
     * @var Factory
35
     */
36
    private $cache;
37
38
    /**
39
     * PaystackFactory constructor.
40
     *
41
     * @param Factory|null $cache
42
     */
43
    public function __construct(Factory $cache = null)
44
    {
45
        $this->cache = $cache;
46
    }
47
48
    /**
49
     * Creates A Paystack Client Object
50
     *
51
     * @param array $config
52
     * @return Client
53
     * @throws \ReflectionException
54
     * @throws \Illuminate\Contracts\Container\BindingResolutionException
55
     */
56
    public function make(array $config)
57
    {
58
        if ($this->secretKeyDoesNotExist($config)) {
59
            throw new InvalidArgumentException('You cannot use the Paystack Factory without a SECRET key, go into "config/paystack.php" to set one.');
60
        }
61
62
        $compatibleConfig = $this->createCompatibleConfiguration($config);
63
64
        $app = new PaystackApplication;
65
66
        $app->instance(Builder::class, $this->getBuilder($config));
67
        $app->instance(PaystackConfigContract::class, $compatibleConfig);
68
69
        $client = new Client($app);
70
71
        // We register a Global Event listener
72
        $client->getEvent()->listen('*', Closure::fromCallable([new EventHandler, 'handle']));
73
74
        return $client;
75
    }
76
77
    /**
78
     * Check to see if Secret key doesn't exists
79
     *
80
     * @param array $config
81
     * @return bool
82
     */
83
    protected function secretKeyDoesNotExist(array $config)
84
    {
85
        return !array_key_exists('secretKey', $config) || (isset($config['secretKey']) && empty($config['secretKey']));
86
    }
87
88
    /**
89
     * Creates a Compatible Paystack Client Configuration from a configuration array
90
     *
91
     * @param array $config
92
     * @return Config
93
     */
94
    public function createCompatibleConfiguration(array $config)
95
    {
96
        return new Config(null, $config['publicKey'] ?: null, $config['secretKey'] ?: null, 'v1');
97
    }
98
99
    /**
100
     * Prepares and retrieves the Paystack client builder
101
     *
102
     * @param $config
103
     * @return ClientBuilder
104
     * @throws \ReflectionException
105
     */
106
    protected function getBuilder($config)
107
    {
108
        $builder = new ClientBuilder();
109
110
        if ($this->cache && class_exists(CacheItemPool::class) && $cache = array_get($config, 'cache')) {
111
            $builder->addCache(new CacheItemPool($this->cache->store( $cache === true ? null : $cache)));
112
        }
113
114
        return $builder;
115
    }
116
}
117