StandardProvider   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 91
ccs 24
cts 24
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A request() 0 6 1
A supports() 0 15 3
A makeUri() 0 11 1
1
<?php
2
3
namespace Bangpound\oEmbed\Provider;
4
5
use GuzzleHttp\Psr7;
6
7
/**
8
 * Class StandardProvider.
9
 */
10
class StandardProvider implements ProviderInterface
11
{
12
    /**
13
     * @var array
14
     */
15
    private $scheme;
16
17
    /**
18
     * @var string
19
     */
20
    private $endpoint;
21
22
    /**
23
     * @var array
24
     */
25
    private $requirements;
26
27
    /**
28
     * @var array
29
     */
30
    private $defaults;
31
32
    /**
33
     * @param $endpoint
34
     * @param array $scheme
35
     * @param array $requirements
36
     * @param array $defaults
37
     */
38 184
    public function __construct($endpoint, array $scheme = array(), array $requirements = array(), array $defaults = array())
39
    {
40 184
        $this->endpoint = $endpoint;
41 184
        $this->scheme = $scheme;
42 184
        $this->requirements = $requirements;
43 184
        $this->defaults = $defaults;
44 184
    }
45
46
    /**
47
     * @param $url
48
     * @param array $params
49
     *
50
     * @return \GuzzleHttp\Psr7\Request
51
     */
52 168
    public function request($url, array $params = array())
53
    {
54 168
        $uri = $this->makeUri($url, $params);
55
56 168
        return new Psr7\Request('get', $uri);
57
    }
58
59
    /**
60
     * Returns whether this class supports the given resource.
61
     *
62
     * @param string $url    A url
63
     * @param array  $params The resource type or null if unknown
64
     *
65
     * @return bool True if this class supports the given url, false otherwise
66
     */
67 176
    public function supports($url, array $params = array())
68
    {
69 176
        $params = array_merge($this->defaults, $params);
70 176
        if (empty($this->scheme)) {
71 8
            return false;
72
        }
73
74 170
        $patterns = array_map(function ($scheme) {
75 170
            return str_replace('\*', '.*', preg_quote($scheme, '#'));
76 170
        }, $this->scheme);
77
78 170
        $pattern = '#'.implode('|', $patterns).'#i';
79
80 170
        return preg_match($pattern, $url) && !array_diff_key($this->requirements, $params);
81
    }
82
83
    /**
84
     * @param $url
85
     * @param array $params
86
     *
87
     * @return \Psr\Http\Message\UriInterface
88
     */
89 168
    private function makeUri($url, $params = array())
90
    {
91 168
        $uri = \GuzzleHttp\uri_template($this->endpoint, array_merge($this->defaults, $params));
92 168
        $uri = new Psr7\Uri($uri);
93
94
        // All arguments must be urlencoded (as per RFC 1738).
95 168
        $query = Psr7\build_query($params, PHP_QUERY_RFC1738);
96 168
        $uri = $uri->withQuery($query);
97
98 168
        return Psr7\Uri::withQueryValue($uri, 'url', $url);
99
    }
100
}
101