Upload::upload()   B
last analyzed

Complexity

Conditions 8
Paths 14

Size

Total Lines 38

Duplication

Lines 6
Ratio 15.79 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 6
loc 38
ccs 0
cts 28
cp 0
rs 8.0675
c 0
b 0
f 0
cc 8
nc 14
nop 4
crap 72
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\Client\ClientExceptionInterface;
15
use Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * @author Sascha-Oliver Prolic <[email protected]>
19
 */
20
class Upload extends HttpApi
21
{
22
    /**
23
     * Upload a locale.
24
     *
25
     * @param string $projectKey
26
     * @param string $ext
27
     * @param string $filename
28
     * @param array  $params
29
     *
30
     * @throws Exception\DomainException
31
     * @throws ClientExceptionInterface
32
     *
33
     * @return Uploaded|ResponseInterface
34
     */
35
    public function upload(string $projectKey, string $ext, string $filename, array $params)
36
    {
37
        if (!file_exists($filename)) {
38
            throw new Exception\InvalidArgumentException('file '.$filename.' not found');
39
        }
40
41
        if (!isset($params['locale_id'])) {
42
            throw new Exception\InvalidArgumentException('locale_id is missing in params');
43
        }
44
45
        $postData = [
46
            ['name' => 'file', 'content' => file_get_contents($filename), 'filename' => basename($filename)],
47
            ['name' => 'file_format', 'content' => $ext],
48
            ['name' => 'locale_id', 'content' => $params['locale_id']],
49
        ];
50
51 View Code Duplication
        if (isset($params['update_translations'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
52
            $postData[] = ['name' => 'update_translations', 'content' => $params['update_translations']];
53
        }
54
55 View Code Duplication
        if (isset($params['tags'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
56
            $postData[] = ['name' => 'tags', 'content' => $params['tags']];
57
        }
58
59
        $response = $this->httpPostRaw(sprintf('/api/v2/projects/%s/uploads', $projectKey), $postData, [
60
            'Content-Type' => 'application/x-www-form-urlencoded',
61
        ]);
62
63
        if (!$this->hydrator) {
64
            return $response;
65
        }
66
67
        if ($response->getStatusCode() !== 200 && $response->getStatusCode() !== 201) {
68
            $this->handleErrors($response);
69
        }
70
71
        return $this->hydrator->hydrate($response, Uploaded::class);
72
    }
73
}
74