ApiObject::configureApi()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 2
1
<?php
2
3
namespace allejo\Wufoo;
4
5
use GuzzleHttp\Client;
6
7
/**
8
 * @internal
9
 */
10
abstract class ApiObject
11
{
12
    /** @var string */
13
    protected $id;
14
15
    /** @var string */
16
    protected static $apiKey;
17
18
    /** @var string */
19
    protected static $subdomain;
20
21
    /** @var Client */
22
    protected static $client;
23
24
    protected function buildUrl($url)
25
    {
26
        return self::interpolate($url, [
27
            'subdomain' => self::$subdomain,
28
            'identifier' => $this->id,
29
        ]);
30
    }
31
32
    /**
33
     * @param string $subdomain The subdomain for your Wufoo account
34
     * @param string $key       The API key used to access this information
35
     */
36
    public static function configureApi($subdomain, $key)
37
    {
38
        self::$apiKey = $key;
39
        self::$subdomain = $subdomain;
40
        self::$client = new Client([
41
            'auth' => [
42
                self::$apiKey,
43
                'PhpWufoo' // Wufoo doesn't check the password, it just cares about the username being the API key
44
            ]
45
        ]);
46
    }
47
48
    /**
49
     * Sanitize an array of query parameters to prepare the array to be converted into a query string.
50
     *
51
     * @param array $parameters
52
     */
53
    protected static function prepareQueryParameters(array $parameters)
54
    {
55
        foreach ($parameters as $parameter => $value)
56
        {
57
            // Remove any null values so they won't appear in the query
58
            if ($value === null)
59
            {
60
                unset($parameters[$parameter]);
61
            }
62
        }
63
    }
64
65
    /**
66
     * Build a query string from an array.
67
     *
68
     * @param array $parameters
69
     *
70
     * @return string A query string ready for HTTP requests
71
     */
72
    protected static function buildQuery(array $parameters)
73
    {
74
        return urldecode(http_build_query($parameters));
75
    }
76
77
    /**
78
     * Interpolates context values into the message placeholders.
79
     *
80
     * @author PHP Framework Interoperability Group
81
     *
82
     * @param string $message
83
     * @param array  $context
84
     *
85
     * @return string
86
     */
87
    protected static function interpolate($message, array $context)
88
    {
89
        // build a replacement array with braces around the context keys
90
        $replace = array();
91
        foreach ($context as $key => $val) {
92
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
93
                $replace[sprintf('{%s}', $key)] = $val;
94
            }
95
        }
96
        // interpolate replacement values into the message and return
97
        return strtr($message, $replace);
98
    }
99
100
    protected static function setIfNotNull(array &$query, $key, &$checkNull)
101
    {
102
        if ($checkNull != null)
103
        {
104
            $query[$key] = $checkNull;
105
        }
106
    }
107
}
108