SimpleImageManager::__construct()   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 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace SimpleImageManager;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use InvalidArgumentException;
7
use SimpleImageManager\Contracts\ImageManagerInterface;
8
use SimpleImageManager\Managers\ImageManager;
9
10
class SimpleImageManager
11
{
12
    /**
13
     * Configuration filtration callback.
14
     *
15
     * @var \Closure|null
16
     */
17
    protected static ?\Closure $filterConfigCallback = null;
18
19
    /**
20
     * The application instance.
21
     */
22
    protected Application $app;
23
24
    /**
25
     * The array of resolved services.
26
     */
27
    protected array $services = [];
28
29
    /**
30
     * Create a new instance.
31
     *
32
     * @param Application $app
33
     */
34 13
    public function __construct(Application $app)
35
    {
36 13
        $this->app = $app;
37
    }
38
39
    /**
40
     * Add additional filter for config data. Can be user in app service provider.
41
     *
42
     * @param \Closure|null $callback
43
     */
44 1
    public static function filterConfigUsing(?\Closure $callback): void
45
    {
46 1
        static::$filterConfigCallback = $callback;
47
    }
48
49
    /**
50
     * Get default driver name.
51
     *
52
     * @return string
53
     */
54 2
    public function getDefaultDriver(): string
55
    {
56 2
        return $this->app->get('config')['simple-image-manager.default.driver'];
57
    }
58
59
    /**
60
     * @param string $name
61
     *
62
     * @return mixed
63
     */
64 13
    protected function getConfig(string $name): mixed
65
    {
66 13
        $configData = $this->app->get('config')["simple-image-manager.drivers.{$name}"] ?? null;
67
68 13
        return $this->filterConfig($name, $configData);
69
    }
70
71
    /**
72
     * @param string $driver
73
     * @param mixed $config
74
     *
75
     * @return mixed
76
     */
77 13
    protected function filterConfig(string $driver, mixed $config): mixed
78
    {
79 13
        if (is_callable(static::$filterConfigCallback)) {
80 1
            return call_user_func(static::$filterConfigCallback, $driver, $config);
0 ignored issues
show
Bug introduced by
It seems like static::filterConfigCallback can also be of type null; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

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

80
            return call_user_func(/** @scrutinizer ignore-type */ static::$filterConfigCallback, $driver, $config);
Loading history...
81
        }
82
83 12
        return $config;
84
    }
85
86
    /**
87
     * Get a driver instance.
88
     *
89
     * @param string|null $driver
90
     *
91
     * @return ImageManagerInterface
92
     */
93 13
    public function driver(?string $driver = null): ImageManagerInterface
94
    {
95 13
        return $this->get($driver ?? $this->getDefaultDriver());
96
    }
97
98
    /**
99
     * Create a driver instance.
100
     *
101
     * @param string $name
102
     *
103
     * @return ImageManagerInterface
104
     */
105 13
    protected function get(string $name): ImageManagerInterface
106
    {
107 13
        return $this->services[ $name ] ?? ($this->services[ $name ] = $this->resolve($name));
108
    }
109
110
    /**
111
     * @param string $name
112
     *
113
     * @return ImageManagerInterface
114
     */
115 13
    protected function resolve(string $name): ImageManagerInterface
116
    {
117 13
        $config = $this->getConfig($name);
118
119 13
        if (is_null($config)) {
120 1
            throw new InvalidArgumentException("Simple image manager driver [{$name}] is not defined.");
121
        }
122
123 12
        if (!empty($config['manager'])) {
124 2
            $manager = $config['manager'];
125 2
            if (is_string($manager)     &&
126 2
                 class_exists($manager) &&
127 2
                 is_subclass_of($manager, ImageManagerInterface::class)) {
128 1
                return new $manager($config);
129
            }
130
131 1
            throw new InvalidArgumentException("Driver [{$name}] has wong manager.");
132
        }
133
134 10
        return new ImageManager($config);
135
    }
136
137
    /**
138
     * Dynamically call the default driver instance.
139
     *
140
     * @param string $method
141
     * @param array $parameters
142
     *
143
     * @return mixed
144
     */
145 1
    public function __call(string $method, array $parameters)
146
    {
147 1
        return $this->driver()->$method(...$parameters);
148
    }
149
}
150