summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/Resource.php
blob: 7c88743ee75ca98f3c1e26353417cace3d631a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php

namespace SparkPost;

class Resource
{
    /**
     * SparkPost object used to make requests.
     */
    protected $sparkpost;

    /**
     * The api endpoint that gets prepended to all requests send through this resource.
     */
    protected $endpoint;

    /**
     * Sets up the Resource.
     *
     * @param SparkPost $sparkpost - the sparkpost instance that this resource is attached to
     * @param string    $endpoint  - the endpoint that this resource wraps
     */
    public function __construct(SparkPost $sparkpost, $endpoint)
    {
        $this->sparkpost = $sparkpost;
        $this->endpoint = $endpoint;
    }

    /**
     * Sends get request to API at the set endpoint.
     *
     * @see SparkPost->request()
     */
    public function get($uri = '', $payload = [], $headers = [])
    {
        return $this->request('GET', $uri, $payload, $headers);
    }

    /**
     * Sends put request to API at the set endpoint.
     *
     * @see SparkPost->request()
     */
    public function put($uri = '', $payload = [], $headers = [])
    {
        return $this->request('PUT', $uri, $payload, $headers);
    }

    /**
     * Sends post request to API at the set endpoint.
     *
     * @see SparkPost->request()
     */
    public function post($payload = [], $headers = [])
    {
        return $this->request('POST', '', $payload, $headers);
    }

    /**
     * Sends delete request to API at the set endpoint.
     *
     * @see SparkPost->request()
     */
    public function delete($uri = '', $payload = [], $headers = [])
    {
        return $this->request('DELETE', $uri, $payload, $headers);
    }

    /**
     * Sends requests to SparkPost object to the resource endpoint.
     *
     * @see SparkPost->request()
     *
     * @return SparkPostPromise or SparkPostResponse depending on sync or async request
     */
    public function request($method = 'GET', $uri = '', $payload = [], $headers = [])
    {
        if (is_array($uri)) {
            $headers = $payload;
            $payload = $uri;
            $uri = '';
        }

        $uri = $this->endpoint.'/'.$uri;

        return $this->sparkpost->request($method, $uri, $payload, $headers);
    }
}