ApiResource   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 72
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A buildQueryString() 0 8 2
A classUrl() 0 5 1
A resourceUrl() 0 12 2
A baseUrl() 0 3 1
A endPointUrl() 0 6 1
1
<?php
2
3
namespace Digikraaft\Paystack;
4
5
use Digikraaft\Paystack\Exceptions\InvalidArgumentException;
6
7
class ApiResource
8
{
9
    const OBJECT_NAME = 'apiresource';
10
11
    use ApiOperations\Request;
12
13
    /**
14
     * @return string the base URL for the given class
15
     */
16
    public static function baseUrl()
17
    {
18
        return Paystack::$apiBase;
19
    }
20
21
    /**
22
     * @return string the endpoint URL for the given class
23
     */
24
    public static function classUrl()
25
    {
26
        $base = static::OBJECT_NAME;
27
28
        return "{$base}";
29
    }
30
31
    /**
32
     * @param null|string $id the ID of the resource
33
     *
34
     * @throws InvalidArgumentException if $id is null
35
     *
36
     * @return string the endpoint URL for the given class
37
     */
38
    public static function resourceUrl($id)
39
    {
40
        if (null === $id) {
41
            $message = 'Invalid ID supplied. ID cannot be null';
42
43
            throw new InvalidArgumentException($message);
44
        }
45
        $id = Util\Util::utf8($id);
46
        $base = static::classUrl();
47
        $extn = urlencode($id);
48
49
        return "{$base}/{$extn}";
50
    }
51
52
    /**
53
     * @param string $slug
54
     *
55
     * @return string the endpoint URL for the given class
56
     */
57
    public static function endPointUrl($slug)
58
    {
59
        $slug = Util\Util::utf8($slug);
60
        $base = static::classUrl();
61
62
        return "{$base}/{$slug}";
63
    }
64
65
    /**
66
     * @param string $slug
67
     * @param $params array of query parameters
68
     *
69
     * @return string the endpoint URL for the given class
70
     */
71
    public static function buildQueryString($slug, $params = null)
72
    {
73
        $url = self::endPointUrl($slug);
74
        if (! empty($params)) {
75
            $url .= '?'.http_build_query($params);
76
        }
77
78
        return $url;
79
    }
80
}
81