Completed
Push — master ( 08753a...42665f )
by Sascha-Oliver
05:38
created

Upload::upload()   C

Complexity

Conditions 7
Paths 8

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 34
ccs 0
cts 27
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 8
nop 4
crap 56
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This software may be modified and distributed under the terms
7
 * of the MIT license.  See the LICENSE file for details.
8
 */
9
10
namespace FAPI\PhraseApp\Api;
11
12
use FAPI\PhraseApp\Exception;
13
use FAPI\PhraseApp\Model\Locale\Uploaded;
14
use Psr\Http\Message\ResponseInterface;
15
16
/**
17
 * @author Sascha-Oliver Prolic <[email protected]>
18
 */
19
class Upload extends HttpApi
20
{
21
    /**
22
     * Upload a locale.
23
     *
24
     * @param string $projectKey
25
     * @param string $ext
26
     * @param string $filename
27
     * @param array  $params
28
     *
29
     * @throws Exception
30
     *
31
     * @return string|ResponseInterface
32
     */
33
    public function upload(string $projectKey, string $ext, string $filename, array $params)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
34
    {
35
        if (!file_exists($filename)) {
36
            throw new Exception\InvalidArgumentException('file '.$filename.' not found');
37
        }
38
39
        if (!isset($params['locale_id'])) {
40
            throw new Exception\InvalidArgumentException('locale_id is missing in params');
41
        }
42
43
        $postData = [
44
            ['name' => 'file', 'content' => file_get_contents($filename), 'filename' => basename($filename)],
45
            ['name' => 'file_format', 'content' => $ext],
46
            ['name' => 'locale_id', 'content' => $params['locale_id']],
47
        ];
48
49
        if (isset($params['tags'])) {
50
            $postData[] = ['name' => 'tags', 'content' => $params['tags']];
51
        }
52
53
        $response = $this->httpPostRaw(sprintf('/api/v2/projects/%s/uploads', $projectKey), $postData, [
54
            'Content-Type' => 'application/x-www-form-urlencoded',
55
        ]);
56
57
        if (!$this->hydrator) {
58
            return $response;
59
        }
60
61
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
62
            $this->handleErrors($response);
63
        }
64
65
        return $this->hydrator->hydrate($response, Uploaded::class);
66
    }
67
}
68