Completed
Push — master ( 50eeb9...9e0ccd )
by Michael
02:32
created

Resources::getResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace BabDev\Transifex\Connector;
4
5
use BabDev\Transifex\ApiConnector;
6
use Psr\Http\Message\ResponseInterface;
7
8
/**
9
 * Transifex API Resources class.
10
 *
11
 * @link http://docs.transifex.com/api/resources/
12
 */
13
final class Resources extends ApiConnector
14
{
15
    /**
16
     * Create a resource.
17
     *
18
     * @param string $project  The slug for the project
19
     * @param string $name     The name of the resource
20
     * @param string $slug     The slug for the resource
21
     * @param string $fileType The file type of the resource
22
     * @param array  $options  Optional additional params to send with the request
23
     *
24
     * @return ResponseInterface
25
     *
26
     * @throws \InvalidArgumentException
27
     */
28 4
    public function createResource(
29
        string $project,
30
        string $name,
31
        string $slug,
32
        string $fileType,
33
        array $options = []
34
    ): ResponseInterface {
35
        // Build the required request data.
36
        $data = [
37 4
            'name'      => $name,
38 4
            'slug'      => $slug,
39 4
            'i18n_type' => $fileType,
40
        ];
41
42
        // Valid options to check
43
        $validOptions = [
44 4
            'accept_translations',
45
            'category',
46
            'priority',
47
        ];
48
49
        // Loop through the valid options and if we have them, add them to the request data
50 4
        foreach ($validOptions as $option) {
51 4
            if (isset($options[$option])) {
52 3
                $data[$option] = $options[$option];
53
            }
54
        }
55
56
        // Attach the resource data - it should be in the content key if this is a string or the file key if it's a file
57 4
        if (isset($options['content'])) {
58 2
            $data['content'] = $options['content'];
59 2
        } elseif (isset($options['file'])) {
60 2
            if (!\file_exists($options['file'])) {
61 1
                throw new \InvalidArgumentException(
62 1
                    \sprintf('The specified file, "%s", does not exist.', $options['file'])
63
                );
64
            }
65
66 1
            $data['content'] = \file_get_contents($options['file']);
67
        }
68
69 3
        $request = $this->createRequest('POST', $this->createUri("/api/2/project/$project/resources/"));
70 3
        $request = $request->withBody($this->streamFactory->createStream(\json_encode($data)));
71 3
        $request = $request->withHeader('Content-Type', 'application/json');
72
73 3
        return $this->client->sendRequest($request);
74
    }
75
76
    /**
77
     * Delete a resource within a project.
78
     *
79
     * @param string $project  The slug for the project the resource is part of
80
     * @param string $resource The resource slug within the project
81
     *
82
     * @return ResponseInterface
83
     */
84 2
    public function deleteResource(string $project, string $resource): ResponseInterface
85
    {
86 2
        return $this->client->sendRequest($this->createRequest('DELETE', $this->createUri("/api/2/project/$project/resource/$resource")));
87
    }
88
89
    /**
90
     * Get information about a resource within a project.
91
     *
92
     * @param string $project  The slug for the project the resource is part of
93
     * @param string $resource The resource slug within the project
94
     * @param bool   $details  True to retrieve additional project details
95
     *
96
     * @return ResponseInterface
97
     */
98 2
    public function getResource(string $project, string $resource, bool $details = false): ResponseInterface
99
    {
100 2
        $uri = $this->createUri("/api/2/project/$project/resource/$resource/");
101
102 2
        if ($details) {
103 2
            $uri = $uri->withQuery('details');
104
        }
105
106 2
        return $this->client->sendRequest($this->createRequest('GET', $uri));
107
    }
108
109
    /**
110
     * Get the content of a resource within a project.
111
     *
112
     * @param string $project  The slug for the project the resource is part of
113
     * @param string $resource The resource slug within the project
114
     *
115
     * @return ResponseInterface
116
     */
117 2
    public function getResourceContent(string $project, string $resource): ResponseInterface
118
    {
119 2
        return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/resource/$resource/content/")));
120
    }
121
122
    /**
123
     * Get information about a project's resources.
124
     *
125
     * @param string $project The slug for the project to retrieve details for
126
     *
127
     * @return ResponseInterface
128
     */
129 2
    public function getResources(string $project): ResponseInterface
130
    {
131 2
        return $this->client->sendRequest($this->createRequest('GET', $this->createUri("/api/2/project/$project/resources")));
132
    }
133
134
    /**
135
     * Update the content of a resource within a project.
136
     *
137
     * @param string $project  The slug for the project the resource is part of
138
     * @param string $resource The resource slug within the project
139
     * @param string $content  The content of the resource, this can either be a string of data or a file path
140
     * @param string $type     The type of content in the $content variable, this should be either string or file
141
     *
142
     * @return ResponseInterface
143
     */
144 5
    public function updateResourceContent(
145
        string $project,
146
        string $resource,
147
        string $content,
148
        string $type = 'string'
149
    ): ResponseInterface {
150 5
        return $this->updateResource($this->createUri("/api/2/project/$project/resource/$resource/content/"), $content, $type);
151
    }
152
}
153