Completed
Pull Request — master (#2)
by Denis
01:32
created

File   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 149
Duplicated Lines 11.41 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.92%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 17
loc 149
ccs 47
cts 48
cp 0.9792
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 25 3
A update() 17 17 2
A delete() 0 5 1
A export() 0 15 2
A processLanguageFile() 0 25 3

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
 * Crowdin API implementation in PHP.
4
 *
5
 * @copyright  Copyright (C) 2016 Nikolai Plath (elkuku)
6
 * @license    WTFPL - See license.txt
7
 */
8
9
namespace ElKuKu\Crowdin\Package;
10
11
use ElKuKu\Crowdin\
12
{
13
	Package, Languagefile
14
};
15
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class File
20
 *
21
 * @since  1.0
22
 */
23
Class File extends Package
24
{
25
	/**
26
	 * Add new file to Crowdin project.
27
	 *
28
	 * @param   Languagefile  $languagefile  The translation file object
29
	 * @param   string        $type          The type.
30
	 * @param   string        $branch        The branch.
31
	 *
32
	 * @see https://crowdin.com/page/api/add-file
33
	 * @since  1.0
34
	 *
35
	 * @return ResponseInterface
36
	 */
37 1
	public function add(Languagefile $languagefile, string $type = '', string $branch = '') : ResponseInterface
38
	{
39 1
		$data = [];
40
41 1
		if ($type)
42
		{
43 1
			$data[] = [
44 1
				'name'     => 'type',
45 1
				'contents' => $type
46
			];
47
		}
48
49 1
		if ($branch)
50
		{
51 1
			$data[] = [
52 1
				'name'     => 'branch',
53 1
				'contents' => $branch
54
			];
55
		}
56
57 1
		$data = $this->processLanguageFile($data, $languagefile);
58
59 1
		return $this->getHttpClient()
60 1
			->post($this->getBasePath('add-file'), ['multipart' => $data]);
61
	}
62
63
	/**
64
	 * Upload latest version of your localization file to Crowdin.
65
	 *
66
	 * @param   Languagefile  $languagefile  The translation file object
67
	 * @param   string        $branch        The branch.
68
	 *
69
	 * @see https://crowdin.com/page/api/update-file
70
	 * @since  1.0
71
	 *
72
	 * @return ResponseInterface
73
	 */
74 1 View Code Duplication
	public function update(Languagefile $languagefile, $branch = '') : ResponseInterface
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...
75
	{
76 1
		$data = [];
77
78 1
		if ($branch)
79
		{
80 1
			$data[] = [
81 1
				'name'     => 'branch',
82 1
				'contents' => $branch
83
			];
84
		}
85
86 1
		$data = $this->processLanguageFile($data, $languagefile);
87
88 1
		return $this->getHttpClient()
89 1
			->post($this->getBasePath('update-file'), ['multipart' => $data]);
90
	}
91
92
	/**
93
	 * Delete file from Crowdin project. All the translations will be lost without ability to restore them.
94
	 *
95
	 * @param   Languagefile  $file  The file to delete.
96
	 *
97
	 * @see https://crowdin.com/page/api/delete-file
98
	 * @since  1.0
99
	 *
100
	 * @return ResponseInterface
101
	 */
102 1
	public function delete(Languagefile $file) : ResponseInterface
103
	{
104 1
		return $this->getHttpClient()
105 1
			->post($this->getBasePath('delete-file'), ['form_params' => ['file' => $file->getCrowdinPath()]]);
106
	}
107
108
	/**
109
	 * This method exports single translated files from Crowdin.
110
	 * Additionally, it can be applied to export XLIFF files for offline localization.
111
	 *
112
	 * @param   string  $file      The file name.
113
	 * @param   string  $language  The language tag.
114
	 * @param   string  $toPath    Export to path.
115
	 * @param   bool  	$xliff    Export in xliff ext.
116
	 *
117
	 * @see    https://crowdin.com/page/api/export-file
118
	 * @since  1.0
119
	 *
120
	 * @return ResponseInterface
121
	 */
122 1
	public function export(string $file, string $language, string $toPath, bool $xliff = false) : ResponseInterface
123
	{
124 1
		$path = sprintf(
125 1
			'%s&file=%s&language=%s',
126 1
			$this->getBasePath('export-file'),
127 1
			$file,
128 1
			$language
129
		);
130 1
		if ($xliff) {
131
			$path .= '&format=xliff';
132
		}
133
134 1
		return $this->getHttpClient()
135 1
			->get($path, ['sink' => $toPath]);
136
	}
137
138
	/**
139
	 * Process a language file.
140
	 *
141
	 * @param   array         $data          Data array.
142
	 * @param   Languagefile  $languagefile  The language file object.
143
	 *
144
	 * @return array
145
	 */
146 1
	private function processLanguageFile(array $data, Languagefile $languagefile) : array
147
	{
148 1
		$data[] = [
149 1
			'name'     => 'files[' . $languagefile->getCrowdinPath() . ']',
150 1
			'contents' => fopen($languagefile->getLocalPath(), 'r')
151
		];
152
153 1
		if ($languagefile->getTitle())
154
		{
155 1
			$data[] = [
156 1
				'name'     => 'titles[' . $languagefile->getCrowdinPath() . ']',
157 1
				'contents' => $languagefile->getTitle()
158
			];
159
		}
160
161 1
		if ($languagefile->getExportPattern())
162
		{
163 1
			$data[] = [
164 1
				'name'     => 'export_patterns[' . $languagefile->getCrowdinPath() . ']',
165 1
				'contents' => $languagefile->getExportPattern()
166
			];
167
		}
168
169 1
		return $data;
170
	}
171
}
172