Upload   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 54
Duplicated Lines 11.11 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 6
loc 54
ccs 0
cts 30
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B upload() 6 38 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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