UpdateFile   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 114
Duplicated Lines 29.82 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 34
loc 114
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 24 46 6
A addTranslation() 10 10 1
A getTranslations() 0 4 1
A getBranch() 0 4 1
A setBranch() 0 4 1

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
namespace Akeneo\Crowdin\Api;
4
5
use \InvalidArgumentException;
6
use Akeneo\Crowdin\Client;
7
use Akeneo\Crowdin\FileReader;
8
use Akeneo\Crowdin\Translation;
9
10
/**
11
 * Upload latest version of your localization file to Crowdin.
12
 *
13
 * @author Nicolas Dupont <[email protected]>
14
 * @see https://crowdin.com/page/api/update-file
15
 */
16
class UpdateFile extends AbstractApi
17
{
18
    /** @var FileReader */
19
    protected $fileReader;
20
21
    /** @var Translation[] */
22
    protected $translations;
23
24
    /** @var string|null */
25
    protected $branch;
26
27
    /**
28
     * @param Client     $client
29
     * @param FileReader $fileReader
30
     */
31
    public function __construct(Client $client, FileReader $fileReader)
32
    {
33
        parent::__construct($client);
34
        $this->fileReader = $fileReader;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function execute()
41
    {
42
        if (count($this->translations) === 0) {
43
            throw new InvalidArgumentException('There is no files to update');
44
        }
45
        
46
        $this->addUrlParameter('key', $this->client->getProjectApiKey());
47
        
48
        $path = sprintf(
49
            "project/%s/update-file?%s",
50
            $this->client->getProjectIdentifier(),
51
            $this->getUrlQueryString()
52
        );
53
54
        $data = $this->parameters;
55 View Code Duplication
        if (null !== $this->branch) {
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
            $data[] = [
57
                'name'      => 'branch',
58
                'contents'  => $this->branch
59
            ];
60
        }
61
62 View Code Duplication
        foreach ($this->translations as $translation) {
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...
63
            $data[] = [
64
                'name'      => 'files['.$translation->getCrowdinPath().']',
65
                'contents'  => $this->fileReader->readTranslation($translation)
66
            ];
67
            if ($translation->getTitle()) {
68
                $data[] = [
69
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
70
                    'contents'  => $translation->getTitle()
71
                ];
72
            }
73
            if ($translation->getExportPattern()) {
74
                $data[] = [
75
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
76
                    'contents'  => $translation->getExportPattern()
77
                ];
78
            }
79
        }
80
81
        $data = ['multipart' => $data];
82
        $response = $this->client->getHttpClient()->post($path, $data);
83
84
        return $response->getBody();
85
    }
86
87
    /**
88
     * @param string $localPath
89
     * @param string $crowdinPath
90
     * @param string $exportPattern
91
     * @param string $title
92
     *
93
     * @return $this
94
     */
95 View Code Duplication
    public function addTranslation($localPath, $crowdinPath, $exportPattern = null, $title = null)
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...
96
    {
97
        $translation = new Translation($localPath, $crowdinPath);
98
        $translation->setExportPattern($exportPattern);
99
        $translation->setTitle($title);
100
101
        $this->translations[] = $translation;
102
103
        return $this;
104
    }
105
106
    /**
107
     * @return Translation[]
108
     */
109
    public function getTranslations()
110
    {
111
        return $this->translations;
112
    }
113
114
    /**
115
     * @return null|string
116
     */
117
    public function getBranch()
118
    {
119
        return $this->branch;
120
    }
121
122
    /**
123
     * @param $branch
124
     */
125
    public function setBranch($branch)
126
    {
127
        $this->branch = $branch;
128
    }
129
}
130