MailMarketingManager::getDefaultDriver()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
4
namespace MailMarketing;
5
6
use Illuminate\Contracts\Foundation\Application;
7
use InvalidArgumentException;
8
use MailMarketing\Drivers\CampaignMonitor;
9
use MailMarketing\Drivers\MailChimp;
10
use MailMarketing\Drivers\MailMarketingInterface;
11
12
class MailMarketingManager
13
{
14
    /**
15
     * The application instance.
16
     */
17
    protected Application $app;
18
19
    /**
20
     * The array of resolved services.
21
     */
22
    protected array $services = [];
23
24
    /**
25
     * The registered custom driver creators.
26
     *
27
     * @var array
28
     */
29
    protected array $customCreators = [];
30
31
    /**
32
     * Create a new Mail marketing manager instance.
33
     * @param Application $app
34
     */
35 9
    public function __construct(Application $app)
36
    {
37 9
        $this->app = $app;
38
    }
39
40 2
    public function extend($driver, \Closure $callback)
41
    {
42 2
        $this->customCreators[$driver] = $callback->bindTo($this, $this);
43
44 2
        return $this;
45
    }
46
47 5
    public function getDefaultDriver()
48
    {
49 5
        return $this->app['config']['mail-marketing.default'];
50
    }
51
52 8
    protected function getConfig(string $name)
53
    {
54 8
        return $this->app['config']["mail-marketing.{$name}"] ?? null;
55
    }
56
57
    /**
58
     * Alternative of 'driver' function
59
     * @param null $service
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $service is correct as it would always require null to be passed?
Loading history...
60
     *
61
     * @return MailMarketingInterface
62
     */
63 2
    public function service($service = null): MailMarketingInterface
64
    {
65 2
        return $this->driver($service);
66
    }
67
68
    /**
69
     * Get a driver instance.
70
     *
71
     * @param string|null $driver
72
     * @return MailMarketingInterface
73
     */
74 8
    public function driver(?string $driver = null): MailMarketingInterface
75
    {
76 8
        return $this->get($driver ?? $this->getDefaultDriver());
77
    }
78
79
    /**
80
     * Create a driver instance.
81
     *
82
     * @param string $name
83
     * @return MailMarketingInterface
84
     */
85 8
    protected function get(string $name): MailMarketingInterface
86
    {
87 8
        return $this->services[$name] ?? ($this->services[$name] = $this->resolve($name));
88
    }
89
90
    /**
91
     * @param string $name
92
     * @return MailMarketingInterface
93
     */
94 8
    protected function resolve(string $name): MailMarketingInterface
95
    {
96 8
        $config = $this->getConfig($name);
97
98 8
        if (is_null($config)) {
99 2
            throw new InvalidArgumentException("Mail Marketing service [{$name}] is not defined.");
100
        }
101
102 8
        if (isset($this->customCreators[$name])) {
103 2
            return $this->callCustomCreator($name, $config);
104
        }
105
106 6
        $driverMethod = 'create' . ucfirst($name) . 'Driver';
107
108 6
        if (method_exists($this, $driverMethod)) {
109 5
            return $this->{$driverMethod}($config);
110
        }
111
112 1
        throw new InvalidArgumentException("Driver [{$name}] is not supported. [{$driverMethod}]");
113
    }
114
115
    /**
116
     * Call a custom driver creator.
117
     *
118
     * @param  array  $config
119
     * @return mixed
120
     */
121 2
    protected function callCustomCreator(string $name, array $config): MailMarketingInterface
122
    {
123 2
        return $this->customCreators[$name]($this->app, $config);
124
    }
125
126 4
    protected function createMailchimpDriver(array $config): MailChimp
127
    {
128 4
        return new MailChimp($config);
129
    }
130
131 1
    protected function createCampaignmonitorDriver(array $config): CampaignMonitor
132
    {
133 1
        return new CampaignMonitor($config);
134
    }
135
136
    /**
137
     * Dynamically call the default driver instance.
138
     *
139
     * @param string $method
140
     * @param array $parameters
141
     * @return mixed
142
     */
143 1
    public function __call($method, $parameters)
144
    {
145 1
        return $this->service()->$method(...$parameters);
146
    }
147
}
148