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

Translation::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
	Languagefile, Package
14
};
15
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class Translation
20
 *
21
 * @since  1.0
22
 */
23
Class Translation extends Package
24
{
25
	/**
26
	 * Upload existing translations to your Crowdin project.
27
	 *
28
	 * @param   Languagefile  $languagefile            The translation object.
29
	 * @param   string        $language                The language tag.
30
	 * @param   boolean       $importDuplicates        Defines whether to add translation if there is
31
	 *                                                 the same translation previously added.
32
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
33
	 * @param   boolean       $importEqualSuggestions  Defines whether to add translation if it is equal to
34
	 *                                                 source string at Crowdin.
35
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
36
	 * @param   boolean       $autoImproveImports      Mark uploaded translations as approved.
37
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
38
	 * @param   boolean       $xliff       						 Specify this parameter if translations are uploaded to the project in XLIFF file format.
39
	 *                                                 Acceptable values are: 0 or 1. Default is 0.
40
	 *
41
	 * @see     https://crowdin.com/page/api/upload-translation
42
	 * @since   1.0.1
43
	 *
44
	 * @return ResponseInterface
45
	 */
46 1
	public function upload(
47
		Languagefile $languagefile, string $language, bool $importDuplicates = false,
48
		bool $importEqualSuggestions = false, bool $autoImproveImports = false, bool $xliff = false) : ResponseInterface
49
	{
50 1
		$data = [];
51
52 1
		$data[] = [
53 1
			'name'     => 'import_duplicates',
54 1
			'contents' => (int) $importDuplicates
55
		];
56
57 1
		$data[] = [
58 1
			'name'     => 'import_eq_suggestions',
59 1
			'contents' => (int) $importEqualSuggestions
60
		];
61
62 1
		$data[] = [
63 1
			'name'     => 'auto_approve_imported',
64 1
			'contents' => (int) $autoImproveImports
65
		];
66
67 1
		$data[] = [
68 1
			'name'     => 'language',
69 1
			'contents' => $language
70
		];
71
72 1
		if ($xliff) {
73
			$data[] = [
74
				'name'     => 'format',
75
				'contents' => 'xliff'
76
			];
77
		}
78
79 1
		$data[] = [
80 1
			'name'     => 'files[' . $languagefile->getCrowdinPath() . ']',
81 1
			'contents' => fopen($languagefile->getLocalPath(), 'r')
82
		];
83
84 1
		return $this->getHttpClient()
85 1
			->post($this->getBasePath('upload-translation'), ['multipart' => $data]);
86
	}
87
88
	/**
89
	 * Build ZIP archive with the latest translations. Please note that this method can be invoked
90
	 * only once per 30 minutes (there is no such restriction for organization plans). Also API
91
	 * call will be ignored if there were no changes in the project since previous export. You can
92
	 * see whether ZIP archive with latest translations was actually build by status attribute
93
	 * ("built" or "skipped") returned in response.
94
	 *
95
	 * @param   string  $branch  The name of related version branch.
96
	 *
97
	 * @since 1.0.4
98
	 * @see   https://crowdin.com/page/api/export
99
	 *
100
	 * @return ResponseInterface
101
	 */
102 1
	public function export(string $branch = '') : ResponseInterface
103
	{
104 1
		$path = $this->getBasePath('export');
105
106 1
		if ($branch)
107
		{
108 1
			$path .= '&branch=' . $branch;
109
		}
110
111 1
		return $this->getHttpClient()
112 1
			->get($path);
113
	}
114
115
	/**
116
	 * Download ZIP file with translations. You can choose the language of translation
117
	 * you need or download all of them at once.
118
	 * Note: If you would like to download the most recent translations you may want
119
	 * to use export API method before downloading.
120
	 *
121
	 * @param   string  $package  Language code or "all" to download a bundle with translations to all languages.
122
	 * @param   string  $toPath   Local path where to download the translation package.
123
	 * @param   string  $branch   The name of related version branch.
124
	 *
125
	 * @since 1.0.4
126
	 * @see   https://crowdin.com/page/api/download
127
	 *
128
	 * @return \Psr\Http\Message\ResponseInterface
129
	 */
130 1
	public function download(string $package, string $toPath, string $branch = '') : ResponseInterface
131
	{
132 1
		$path = sprintf(
133 1
			'project/%s/download/%s?key=%s',
134 1
			$this->getProjectId(),
135 1
			$package,
136 1
			$this->getApiKey()
137
		);
138
139 1
		if ($branch)
140
		{
141 1
			$path .= '&branch=' . $branch;
142
		}
143
144 1
		return $this->getHttpClient()
145 1
			->get($path, ['sink' => $toPath]);
146
	}
147
148
	/**
149
	 * Track overall translation and proofreading progresses of each target language.
150
	 * Default response format is XML.
151
	 *
152
	 * @see    https://crowdin.com/page/api/status
153
	 * @since  1.0.4
154
	 *
155
	 * @return ResponseInterface
156
	 */
157 1
	public function getStatus() : ResponseInterface
158
	{
159 1
		return $this->getHttpClient()
160 1
			->get($this->getBasePath('status'));
161
	}
162
}
163