Test Failed
Push — master ( 578cfa...759be6 )
by Arjan
02:07
created

Client::default()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 9 and the first side effect is on line 167.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace ArjanWestdorp\Imaginary;
4
5
use ArjanWestdorp\Imaginary\Exceptions\InvalidConfigurationException;
6
use ArjanWestdorp\Imaginary\Exceptions\UndefinedDefinitionException;
7
use Closure;
8
9
class Client
10
{
11
    /**
12
     * @var Builder
13
     */
14
    protected $builder;
15
    /**
16
     * @var array;
17
     */
18
    protected $definitions = [];
19
    /**
20
     * @var array
21
     */
22
    private $config;
23
    /**
24
     * @var array
25
     */
26
    protected $defaults = [];
27
28
    /**
29 15
     * Client constructor.
30
     *
31 15
     * @param $config
32 15
     */
33
    public function __construct($config)
34
    {
35
        $this->config = $config;
36
    }
37
38
    /**
39
     * Check if we can call a method on the manipulations builder.
40
     * Else check if a predefined manipulation set need to be called.
41
     *
42
     * @param string $method
43 11
     * @param array $arguments
44
     * @return $this
45 11
     * @throws UndefinedDefinitionException
46 8
     */
47
    public function __call($method, $arguments)
48 8
    {
49 3
        if (method_exists($this->builder, $method)) {
50 2
            call_user_func_array([$this->builder, $method], $arguments);
51
52 2
            return $this;
53
        } elseif (isset($this->definitions[$method]) && is_callable($this->definitions[$method])) {
54 2
            array_unshift($arguments, $this->builder);
55
56
            call_user_func_array($this->definitions[$method], $arguments);
57 1
58
            return $this;
59
        }
60
61
        throw UndefinedDefinitionException::definitionNotDefined($method);
62
    }
63
64
    /**
65
     * Define a predefined set of manipulations.
66
     *
67 2
     * @param string $key
68
     * @param Closure $callback
69 2
     * @return $this
70
     */
71 2
    public function define($key, Closure $callback)
72
    {
73
        $this->definitions[$key] = $callback;
74
75
        return $this;
76
    }
77
78
    /**
79
     * Let the client fetch the given url as image.
80 15
     *
81
     * @param string $url
82 15
     * @return $this
83
     */
84 15
    public function fetch($url)
85
    {
86
        $this->builder = new Builder(urlencode($url));
87
88
        foreach ($this->defaults as $key => $value) {
89
            if (method_exists($this->builder, $key)) {
90
                call_user_func([$this->builder, $key], $value);
91
            }
92
        }
93 14
94
        return $this;
95 14
    }
96 2
97
    /**
98
     * Set the default dpr.
99 12
     *
100 1
     * @param string $key
101
     * @param mixed $value
102
     * @return $this
103 11
     */
104 11
    public function default($key, $value)
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
105 11
    {
106 11
        $this->defaults[$key] = $value;
107 11
108 11
        return $this;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
109 11
    }
110 11
111
    /**
112
     * Retrieve the imaginary url.
113
     *
114
     * @return string
115
     */
116
    public function url()
117
    {
118
        if (!isset($this->config['url'])) {
119 11
            throw InvalidConfigurationException::urlNotDefined();
120
        }
121 11
122
        if (!isset($this->config['client'])) {
123
            throw InvalidConfigurationException::clientNotDefined();
124
        }
125
126
        return implode('/', array_filter([
127
            $this->config['url'],
128
            $this->config['client'],
129
            $this->getResourceKey(),
130 11
            $this->getTypeKey(),
131
            $this->builder->getManipulations(),
132 11
            $this->builder->getKey(),
133
        ]));
134
    }
135
136
    /**
137
     * Get the resource key.
138
     * We currently only support image.
139
     *
140 1
     * @return string
141
     */
142 1
    protected function getResourceKey()
143
    {
144
        return 'images';
145
    }
146
147
    /**
148
     * Get the type key.
149
     * We only support fetch at the moment.
150
     *
151
     * @return string
152
     */
153
    protected function getTypeKey()
154
    {
155
        return 'fetch';
156
    }
157
158
    /**
159
     * Return the url for this image.
160
     *
161
     * @return string
162
     */
163
    public function __toString()
164
    {
165
        return $this->url();
166
    }
167
}