AddFile   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 146
Duplicated Lines 23.29 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C execute() 24 51 7
A addTranslation() 10 10 1
A getTranslations() 0 4 1
A getType() 0 4 1
A setType() 0 6 1
A getBranch() 0 4 1
A setBranch() 0 6 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
 * Adds a new file to a Crowdin project.
12
 *
13
 * @author Julien Janvier <[email protected]>
14
 * @see https://crowdin.com/page/api/add-file
15
 */
16
class AddFile extends AbstractApi
17
{
18
    /** @var FileReader */
19
    protected $fileReader;
20
21
    /** @var Translation[] */
22
    protected $translations;
23
24
    /** @var string */
25
    protected $type;
26
27
    /** @var string */
28
    protected $branch;
29
30
    /**
31
     * @param Client     $client
32
     * @param FileReader $fileReader
33
     */
34
    public function __construct(Client $client, FileReader $fileReader)
35
    {
36
        parent::__construct($client);
37
        $this->fileReader = $fileReader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function execute()
44
    {
45
        if (0 === count($this->translations)) {
46
            throw new InvalidArgumentException('There is no files to add.');
47
        }
48
        
49
        $this->addUrlParameter('key', $this->client->getProjectApiKey());
50
51
        $path = sprintf(
52
            "project/%s/add-file?%s",
53
            $this->client->getProjectIdentifier(),
54
            $this->getUrlQueryString()
55
        );
56
57
        $data = $this->parameters;
58
        if (null !== $this->type) {
59
            $data[] = [
60
                'name'      => 'type',
61
                'contents'  => $this->type
62
            ];
63
        }
64 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...
65
            $data[] = [
66
                'name'      => 'branch',
67
                'contents'  => $this->branch
68
            ];
69
        }
70 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...
71
            $data[] = [
72
                'name'      => 'files['.$translation->getCrowdinPath().']',
73
                'contents'  => $this->fileReader->readTranslation($translation)
74
            ];
75
            if ($translation->getTitle()) {
76
                $data[] = [
77
                    'name'      => 'titles['.$translation->getCrowdinPath().']',
78
                    'contents'  => $translation->getTitle()
79
                ];
80
            }
81
            if ($translation->getExportPattern()) {
82
                $data[] = [
83
                    'name'      => 'export_patterns['.$translation->getCrowdinPath().']',
84
                    'contents'  => $translation->getExportPattern()
85
                ];
86
            }
87
        }
88
89
        $data = ['multipart' => $data];
90
        $response = $this->client->getHttpClient()->post($path, $data);
91
92
        return $response->getBody();
93
    }
94
95
    /**
96
     * @param string $localPath
97
     * @param string $crowdinPath
98
     * @param string $exportPattern
99
     * @param string $title
100
     *
101
     * @return $this
102
     */
103 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...
104
    {
105
        $translation = new Translation($localPath, $crowdinPath);
106
        $translation->setExportPattern($exportPattern);
107
        $translation->setTitle($title);
108
109
        $this->translations[] = $translation;
110
111
        return $this;
112
    }
113
114
    /**
115
     * @return Translation[]
116
     */
117
    public function getTranslations()
118
    {
119
        return $this->translations;
120
    }
121
122
    /**
123
     * @return string
124
     */
125
    public function getType()
126
    {
127
        return $this->type;
128
    }
129
130
    /**
131
     * @param string $type
132
     *
133
     * @return AddFile
134
     */
135
    public function setType($type)
136
    {
137
        $this->type = $type;
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return string|null
144
     */
145
    public function getBranch()
146
    {
147
        return $this->branch;
148
    }
149
150
    /**
151
     * @param string $branch
152
     *
153
     * @return AddFile
154
     */
155
    public function setBranch($branch)
156
    {
157
        $this->branch = $branch;
158
159
        return $this;
160
    }
161
}
162