Completed
Push — master ( 7e484e...487549 )
by Michael
02:01
created

Projects::updateProject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 13
Bugs 0 Features 1
Metric Value
c 13
b 0
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * BabDev Transifex Package
5
 *
6
 * (c) Michael Babker <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BabDev\Transifex;
13
14
/**
15
 * Transifex API Projects class.
16
 *
17
 * @link http://docs.transifex.com/developer/api/projects
18
 */
19
class Projects extends TransifexObject
20
{
21
    /**
22
     * Build the data array to send with create and update requests.
23
     *
24
     * @param array $options Optional additional params to send with the request
25
     *
26
     * @return array
27
     */
28 7
    private function buildProjectRequest(array $options)
29
    {
30 7
        $data = [];
31
32
        // Valid options to check
33
        $validOptions = [
34 7
            'long_description',
35 7
            'private',
36 7
            'homepage',
37 7
            'trans_instructions',
38 7
            'tags',
39 7
            'maintainers',
40 7
            'team',
41 7
            'auto_join',
42 7
            'license',
43 7
            'fill_up_resources',
44 7
            'repository_url',
45 7
            'organization',
46 7
            'archived',
47 7
        ];
48
49
        // Loop through the valid options and if we have them, add them to the request data
50 7
        foreach ($validOptions as $option) {
51 7
            if (isset($options[$option])) {
52 5
                $data[$option] = $options[$option];
53 5
            }
54 7
        }
55
56
        // Set the license if present
57 7
        if (isset($options['license'])) {
58 3
            $this->checkLicense($options['license']);
59 1
            $data['license'] = $options['license'];
60 1
        }
61
62 5
        return $data;
63
    }
64
65
    /**
66
     * Checks that a license is an accepted value.
67
     *
68
     * @param string $license The license to check
69
     *
70
     * @throws \InvalidArgumentException
71
     */
72 4
    private function checkLicense($license)
73
    {
74 4
        $accepted = ['proprietary', 'permissive_open_source', 'other_open_source'];
75
76
        // Ensure the license option is an allowed value
77 4
        if (!in_array($license, $accepted)) {
78 2
            throw new \InvalidArgumentException(
79 2
                sprintf(
80 2
                    'The license %s is not valid, accepted license values are %s',
81 2
                    $license,
82 2
                    implode(', ', $accepted)
83 2
                )
84 2
            );
85
        }
86 2
    }
87
88
    /**
89
     * Method to create a project.
90
     *
91
     * @param string $name           The name of the project
92
     * @param string $slug           The slug for the project
93
     * @param string $description    A description of the project
94
     * @param string $sourceLanguage The source language code for the project
95
     * @param array  $options        Optional additional params to send with the request
96
     *
97
     * @return \Joomla\Http\Response
98
     *
99
     * @throws \InvalidArgumentException
100
     */
101 4
    public function createProject($name, $slug, $description, $sourceLanguage, array $options = [])
102
    {
103
        // Build the request data.
104 4
        $data = array_merge(
105
            [
106 4
                'name'                 => $name,
107 4
                'slug'                 => $slug,
108 4
                'description'          => $description,
109 4
                'source_language_code' => $sourceLanguage,
110 4
            ],
111 4
            $this->buildProjectRequest($options)
112 3
        );
113
114
        // Check mandatory fields.
115 3
        if (!isset($data['license']) || in_array($data['license'], ['permissive_open_source', 'other_open_source'])) {
116 3
            if (!isset($data['repository_url'])) {
117 1
                throw new \InvalidArgumentException(
118
                    'If a project is denoted either as permissive_open_source or other_open_source, the field repository_url is mandatory and should contain a link to the public repository of the project to be created.'
119 1
                );
120
            }
121 2
        }
122
123
        // Send the request.
124 2
        return $this->processResponse(
125 2
            $this->client->post(
126 2
                $this->fetchUrl('/projects/'),
127 2
                json_encode($data),
128 2
                ['Content-Type' => 'application/json']
129 2
            ),
130
            201
131 2
        );
132
    }
133
134
    /**
135
     * Method to delete a project.
136
     *
137
     * @param string $slug The slug for the resource.
138
     *
139
     * @return \Joomla\Http\Response
140
     */
141 2
    public function deleteProject($slug)
142
    {
143 2
        return $this->processResponse($this->client->delete($this->fetchUrl('/project/' . $slug)), 204);
144
    }
145
146
    /**
147
     * Method to get information about a project.
148
     *
149
     * @param string $project The project to retrieve details for
150
     * @param bool   $details True to retrieve additional project details
151
     *
152
     * @return \Joomla\Http\Response
153
     */
154 2 View Code Duplication
    public function getProject($project, $details = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        // Build the request path.
157 2
        $path = '/project/' . $project . '/';
158
159 2
        if ($details) {
160 2
            $path .= '?details';
161 2
        }
162
163
        // Send the request.
164 2
        return $this->processResponse($this->client->get($this->fetchUrl($path)));
165
    }
166
167
    /**
168
     * Method to get a list of projects the user is part of.
169
     *
170
     * @return \Joomla\Http\Response
171
     */
172 2
    public function getProjects()
173
    {
174 2
        return $this->processResponse($this->client->get($this->fetchUrl('/projects/')));
175
    }
176
177
    /**
178
     * Method to update a project.
179
     *
180
     * @param string $slug    The slug for the project
181
     * @param array  $options Optional additional params to send with the request
182
     *
183
     * @return \Joomla\Http\Response
184
     *
185
     * @throws \RuntimeException
186
     */
187 4
    public function updateProject($slug, array $options = [])
188
    {
189
        // Build the request data.
190 4
        $data = $this->buildProjectRequest($options);
191
192
        // Make sure we actually have data to send
193 3
        if (empty($data)) {
194 1
            throw new \RuntimeException('There is no data to send to Transifex.');
195
        }
196
197
        // Send the request.
198 2
        return $this->processResponse(
199 2
            $this->client->put(
200 2
                $this->fetchUrl('/project/' . $slug . '/'),
201 2
                json_encode($data),
202 2
                ['Content-Type' => 'application/json']
203 2
            )
204 2
        );
205
    }
206
}
207