Translation   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
c 4
b 0
f 0
lcom 1
cbo 3
dl 0
loc 131
ccs 38
cts 38
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B upload() 0 34 1
A export() 0 12 2
A download() 0 17 2
A getStatus() 0 5 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
	 *
39
	 * @see     https://crowdin.com/page/api/upload-translation
40
	 * @since   1.0.1
41
	 *
42
	 * @return ResponseInterface
43
	 */
44 1
	public function upload(
45
		Languagefile $languagefile, string $language, bool $importDuplicates = false,
46
		bool $importEqualSuggestions = false, bool $autoImproveImports = false) : ResponseInterface
47
	{
48 1
		$data = [];
49
50 1
		$data[] = [
51 1
			'name'     => 'import_duplicates',
52 1
			'contents' => (int) $importDuplicates
53
		];
54
55 1
		$data[] = [
56 1
			'name'     => 'import_eq_suggestions',
57 1
			'contents' => (int) $importEqualSuggestions
58
		];
59
60 1
		$data[] = [
61 1
			'name'     => 'auto_approve_imported',
62 1
			'contents' => (int) $autoImproveImports
63
		];
64
65 1
		$data[] = [
66 1
			'name'     => 'language',
67 1
			'contents' => $language
68
		];
69
70 1
		$data[] = [
71 1
			'name'     => 'files[' . $languagefile->getCrowdinPath() . ']',
72 1
			'contents' => fopen($languagefile->getLocalPath(), 'r')
73
		];
74
75 1
		return $this->getHttpClient()
76 1
			->post($this->getBasePath('upload-translation'), ['multipart' => $data]);
77
	}
78
79
	/**
80
	 * Build ZIP archive with the latest translations. Please note that this method can be invoked
81
	 * only once per 30 minutes (there is no such restriction for organization plans). Also API
82
	 * call will be ignored if there were no changes in the project since previous export. You can
83
	 * see whether ZIP archive with latest translations was actually build by status attribute
84
	 * ("built" or "skipped") returned in response.
85
	 *
86
	 * @param   string  $branch  The name of related version branch.
87
	 *
88
	 * @since 1.0.4
89
	 * @see   https://crowdin.com/page/api/export
90
	 *
91
	 * @return ResponseInterface
92
	 */
93 1
	public function export(string $branch = '') : ResponseInterface
94
	{
95 1
		$path = $this->getBasePath('export');
96
97 1
		if ($branch)
98
		{
99 1
			$path .= '&branch=' . $branch;
100
		}
101
102 1
		return $this->getHttpClient()
103 1
			->get($path);
104
	}
105
106
	/**
107
	 * Download ZIP file with translations. You can choose the language of translation
108
	 * you need or download all of them at once.
109
	 * Note: If you would like to download the most recent translations you may want
110
	 * to use export API method before downloading.
111
	 *
112
	 * @param   string  $package  Language code or "all" to download a bundle with translations to all languages.
113
	 * @param   string  $toPath   Local path where to download the translation package.
114
	 * @param   string  $branch   The name of related version branch.
115
	 *
116
	 * @since 1.0.4
117
	 * @see   https://crowdin.com/page/api/download
118
	 *
119
	 * @return \Psr\Http\Message\ResponseInterface
120
	 */
121 1
	public function download(string $package, string $toPath, string $branch = '') : ResponseInterface
122
	{
123 1
		$path = sprintf(
124 1
			'project/%s/download/%s?key=%s',
125 1
			$this->getProjectId(),
126 1
			$package,
127 1
			$this->getApiKey()
128
		);
129
130 1
		if ($branch)
131
		{
132 1
			$path .= '&branch=' . $branch;
133
		}
134
135 1
		return $this->getHttpClient()
136 1
			->get($path, ['sink' => $toPath]);
137
	}
138
139
	/**
140
	 * Track overall translation and proofreading progresses of each target language.
141
	 * Default response format is XML.
142
	 *
143
	 * @see    https://crowdin.com/page/api/status
144
	 * @since  1.0.4
145
	 *
146
	 * @return ResponseInterface
147
	 */
148 1
	public function getStatus() : ResponseInterface
149
	{
150 1
		return $this->getHttpClient()
151 1
			->get($this->getBasePath('status'));
152
	}
153
}
154