Completed
Push — master ( f1cec2...e52e43 )
by Nikolai
02:28
created

Memory::download()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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
13
/**
14
 * Class Memory
15
 *
16
 * @since  1.0.5
17
 */
18
Class Memory extends Package
19
{
20
	/**
21
	 * Download Crowdin project Translation Memory as TMX file.
22
	 *
23
	 * @param   boolean  $includeAssigned  Defines whether the assigned TMs should be included in downloaded TMX file.
24
	 *                                     Acceptable values are: 0, 1.
25
	 *                                     Default is 1.
26
	 *
27
	 * @since 1.0.5
28
	 * @see   https://crowdin.com/page/api/download-tm
29
	 *
30
	 * @return \Psr\Http\Message\ResponseInterface
31
	 */
32
	public function download($includeAssigned = true)
33
	{
34
		$path = $this->getBasePath('download-tm');
35
36
		if (!$includeAssigned)
37
		{
38
			$path .= '&include_assigned=0';
39
		}
40
41
		return $this->getHttpClient()
42
			->get($path);
43
	}
44
45
	/**
46
	 * Upload your Translation Memory for Crowdin Project in TMX file format.
47
	 *
48
	 * @param   string  $file  Full path to file to upload.
49
	 *
50
	 * @since 1.0.5
51
	 * @see   https://crowdin.com/page/api/upload-tm
52
	 *
53
	 * @return string
54
	 */
55
	public function upload($file)
56
	{
57
		if (false === file_exists($file))
58
		{
59
			throw new \UnexpectedValueException('File not found for upload');
60
		}
61
62
		/*
63
		 * @todo Crowdin does not accept Guzzle's POST requests.
64
		 * She just tells me:
65
		 * <code>4</code>
66
		 * <message>No files specified in request</message>
67
		 *
68
		 *  :(
69
70
		return $this->getHttpClient()
71
			->post($this->getBasePath('upload-tm'), ['form_params' => ['file' => $file]]);
72
73
		HELP WANTED !!!
74
75
		 */
76
77
		$post_params = array();
78
		$request_url = 'https://api.crowdin.com/api/' . $this->getBasePath('upload-tm');
79
80
		if (function_exists('curl_file_create'))
81
		{
82
			$post_params['file'] = curl_file_create($file);
83
		}
84
		else
85
		{
86
			/*
87
			 * This does NOT seem to work with Mrs. Crowdin...
88
			 * ... comes from their docs
89
			 * $post_params['file'] = '@/home/crowdin/test.tmx';
90
			 */
91
			throw new \RuntimeException('This version of cURL does not seem to work (with Crowdin...)');
92
		}
93
94
		$ch = curl_init();
95
96
		curl_setopt($ch, CURLOPT_URL, $request_url);
97
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
98
		curl_setopt($ch, CURLOPT_POST, true);
99
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
100
101
		$result = curl_exec($ch);
102
103
		curl_close($ch);
104
105
		if (false === $result)
106
		{
107
			throw new \UnexpectedValueException('File upload failed');
108
		}
109
110
		return 'File has been uploaded (?)';
111
	}
112
}
113