Completed
Push — master ( 9c7829...38271d )
by Nikolai
02:41
created

File::processLanguageFile()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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