Passed
Push — develop ( d0763f )
by Arjan
02:10
created

Client::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ArjanWestdorp\Imaginary;
4
5
use ArjanWestdorp\Imaginary\Exceptions\InvalidConfigException;
6
use ArjanWestdorp\Imaginary\Exceptions\UndefinedDefinitionException;
7
use Closure;
8
9
class Client
10
{
11
    /**
12
     * @var array;
13
     */
14
    protected $definitions = [];
15
    /**
16
     * @var string
17
     */
18
    protected $key;
19
    /**
20
     * @var array
21
     */
22
    protected $manipulations;
23
    /**
24
     * @var string
25
     */
26
    protected $url;
27
    /**
28
     * @var array
29
     */
30
    private $config;
31
32
    /**
33
     * Client constructor.
34
     *
35
     * @param $config
36
     */
37 13
    public function __construct($config)
38
    {
39 13
        $this->config = $config;
40 13
    }
41
42
    /**
43
     * Check if a predefined manipulation set need to be called.
44
     *
45
     * @param string $method
46
     * @param array $arguments
47
     * @return $this
48
     */
49 3
    public function __call($method, $arguments)
50
    {
51 3
        if (isset($this->definitions[$method]) && is_callable($this->definitions[$method])) {
52
53 2
            array_unshift($arguments, $this);
54
55 2
            call_user_func_array($this->definitions[$method], $arguments);
56
57 2
            return $this;
58
        }
59
60 1
        throw UndefinedDefinitionException::definitionNotDefined($method);
61
    }
62
63
    /**
64
     * Make a circle of the image with the given radius.
65
     *
66
     * @param string $radius
67
     * @return $this
68
     */
69 2
    public function circle($radius = 'max')
70
    {
71 2
        $this->manipulations['r'] = $radius;
72
73 2
        return $this;
74
    }
75
76
    /**
77
     * Define a predefined set of manipulations.
78
     *
79
     * @param string $key
80
     * @param Closure $callback
81
     * @return $this
82
     */
83 2
    public function define($key, Closure $callback)
84
    {
85 2
        $this->definitions[$key] = $callback;
86
87 2
        return $this;
88
    }
89
90
    /**
91
     * Let the client fetch the given url as image.
92
     *
93
     * @param string $url
94
     * @return $this
95
     */
96 13
    public function fetch($url)
97
    {
98 13
        $this->key = $url;
99
100 13
        return $this;
101
    }
102
103
    /**
104
     * Fit the image when resizing it.
105
     *
106
     * @param null|string $gravity
107
     * @return $this
108
     */
109 5
    public function fit($gravity = null)
110
    {
111 5
        $this->manipulations['c'] = 'fit';
112
113 5
        if (!is_null($gravity)) {
114 4
            $this->manipulations['g'] = $gravity;
115 4
        }
116
117 5
        return $this;
118
    }
119
120
    /**
121
     * Set the desired height of the image.
122
     *
123
     * @param int $height
124
     * @return $this
125
     */
126 2
    public function height($height)
127
    {
128 2
        $this->manipulations['h'] = $height;
129
130 2
        return $this;
131
    }
132
133
    /**
134
     * Retrieve the imaginary url.
135
     *
136
     * @return string
137
     * @throws InvalidConfigException
138
     */
139 12
    public function url()
140
    {
141 12
        if (!isset($this->config['url'])) {
142 1
            throw InvalidConfigException::urlNotDefined();
143
        }
144
145 11
        if (!isset($this->config['client'])) {
146 1
            throw InvalidConfigException::clientNotDefined();
147
        }
148
149 10
        return implode('/', array_filter([
150 10
            $this->config['url'],
151 10
            $this->config['client'],
152 10
            $this->getResourceKey(),
153 10
            $this->getTypeKey(),
154 10
            $this->getManipulations(),
155 10
            $this->key
156 10
        ]));
157
    }
158
159
    /**
160
     * Get the resource key.
161
     * We currently only support image.
162
     *
163
     * @return string
164
     */
165 10
    protected function getResourceKey()
166
    {
167 10
        return 'image';
168
    }
169
170
    /**
171
     * Get the type key.
172
     * We only support fetch at the moment.
173
     *
174
     * @return string
175
     */
176 10
    protected function getTypeKey()
177
    {
178 10
        return 'fetch';
179
    }
180
181
    /**
182
     * Get the string representation of all manipulations that
183
     * need to be executed on the given resource.
184
     *
185
     * @return string|null
186
     */
187
    public function getManipulations()
188
    {
189 10
        $manipulations = array_map(function ($key) {
190 9
            return $key . '_' . $this->manipulations[$key];
191 10
        }, array_keys($this->manipulations ?: []));
192
193 10
        return implode(',', $manipulations);
194
    }
195
196
    /**
197
     * Set the desired width of the image.
198
     *
199
     * @param int $width
200
     * @return $this
201
     */
202 5
    public function width($width)
203
    {
204 5
        $this->manipulations['w'] = $width;
205
206 5
        return $this;
207
    }
208
209
    /**
210
     * Return the url for this image.
211
     *
212
     * @return string
213
     */
214 1
    public function __toString()
215
    {
216 1
        return $this->url();
217
    }
218
}