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

Memory::download()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 0
cts 10
cp 0
rs 9.7666
c 0
b 0
f 0
cc 4
nc 8
nop 4
crap 20
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\Package;
12
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class Memory
17
 *
18
 * @since  1.0.5
19
 */
20
Class Memory extends Package
21
{
22
	/**
23
	 * Download Crowdin project Translation Memory as TMX file.
24
	 *
25
	 * @param   string  $toPath    Export to path.
26
	 * @param   boolean  $includeAssigned  Defines whether the assigned TMs should be included in downloaded TMX file.
27
	 *                                     Acceptable values are: 0, 1.
28
	 *                                     Default is 1.
29
	 * @param   boolean  $sourceLanguage Defines a source language for language pair. Сrowdin language code should be used.
30
	 * @param   boolean  $targetLanguage Defines a target language for language pair. Сrowdin language code should be used.
31
	 *
32
	 * @since 1.0.5
33
	 * @see   https://crowdin.com/page/api/download-tm
34
	 *
35
	 * @return ResponseInterface
36
	 */
37
	public function download(string $toPath, bool $includeAssigned = true, string $sourceLanguage = '', string $targetLanguage = '') : ResponseInterface
38
	{
39
		$url = $this->getBasePath('download-tm');
40
		if ($includeAssigned) {
41
			$url .= '&include_assigned=1';
42
		}
43
		if (!empty($sourceLanguage)) {
44
			$url .= '&source_language=' . $sourceLanguage;
45
		}
46
		if (!empty($targetLanguage)) {
47
			$url .= '&target_language=' . $targetLanguage;
48
		}
49
		return $this->getHttpClient()
50
			->get($url, ['sink' => $toPath]);
51
	}
52
53
	/**
54
	 * Upload your Translation Memory for Crowdin Project in TMX file format.
55
	 *
56
	 * @param   string  $file  Full path to file to upload.
57
	 *
58
	 * @since 1.0.5
59
	 * @see   https://crowdin.com/page/api/upload-tm
60
	 *
61
	 * @return ResponseInterface
62
	 */
63 2 View Code Duplication
	public function upload(string $file) : 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...
64
	{
65 2
		if (false === file_exists($file))
66
		{
67 1
			throw new \UnexpectedValueException('File not found for upload');
68
		}
69
70
		$data = [[
71 1
			'name'     => 'file',
72 1
			'contents' => fopen($file, 'r')
73
		]];
74
75 1
		return $this->getHttpClient()
76 1
			->post($this->getBasePath('upload-tm'), ['multipart' => $data]);
77
	}
78
}
79