Completed
Push — master ( 766aca...d9f01f )
by Nicolas
7s
created

UpdateFile   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 111
Duplicated Lines 30.63 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 6
Bugs 2 Features 1
Metric Value
wmc 11
c 6
b 2
f 1
lcom 1
cbo 6
dl 34
loc 111
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B execute() 24 43 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 http://crowdin.net/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
        $path = sprintf(
46
            "project/%s/update-file?key=%s",
47
            $this->client->getProjectIdentifier(),
48
            $this->client->getProjectApiKey()
49
        );
50
51
        $data = $this->parameters;
52 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...
53
            $data[] = [
54
                'name'      => 'branch',
55
                'contents'  => $this->branch
56
            ];
57
        }
58
59 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...
60
            $data[] = [
61
                'name'      => 'files['.$translation->getCrowdinPath().']',
62
                'contents'  => $this->fileReader->readTranslation($translation)
63
            ];
64
            if ($translation->getTitle()) {
65
                $data[] = [
66
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
67
                    'contents'  => $translation->getTitle()
68
                ];
69
            }
70
            if ($translation->getExportPattern()) {
71
                $data[] = [
72
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
73
                    'contents'  => $translation->getExportPattern()
74
                ];
75
            }
76
        }
77
78
        $data = ['multipart' => $data];
79
        $response = $this->client->getHttpClient()->post($path, $data);
80
81
        return $response->getBody();
82
    }
83
84
    /**
85
     * @param string $localPath
86
     * @param string $crowdinPath
87
     * @param string $exportPattern
88
     * @param string $title
89
     *
90
     * @return $this
91
     */
92 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...
93
    {
94
        $translation = new Translation($localPath, $crowdinPath);
95
        $translation->setExportPattern($exportPattern);
96
        $translation->setTitle($title);
97
98
        $this->translations[] = $translation;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return Translation[]
105
     */
106
    public function getTranslations()
107
    {
108
        return $this->translations;
109
    }
110
111
    /**
112
     * @return null|string
113
     */
114
    public function getBranch()
115
    {
116
        return $this->branch;
117
    }
118
119
    /**
120
     * @param $branch
121
     */
122
    public function setBranch($branch)
123
    {
124
        $this->branch = $branch;
125
    }
126
}
127