Resources   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 31
c 0
b 0
f 0
dl 0
loc 141
ccs 32
cts 32
cp 1
rs 10
wmc 12

6 Methods

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