Completed
Push — master ( 8c6743...47d307 )
by Nikolai
02:49
created

Memory::upload()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 63
Code Lines 19

Duplication

Lines 60
Ratio 95.24 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 60
loc 63
ccs 0
cts 0
cp 0
rs 8.8945
cc 4
eloc 19
nc 4
nop 1
crap 20

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 View Code Duplication
Class Memory extends Package
0 ignored issues
show
Duplication introduced by
This class 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...
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 1
	public function download($includeAssigned = true)
33
	{
34 1
		return $this->getHttpClient()
35 1
			->get($this->getBasePath('download-tm') . '&include_assigned=' . (int) $includeAssigned);
36
	}
37
38
	/**
39
	 * Upload your Translation Memory for Crowdin Project in TMX file format.
40
	 *
41
	 * @param   string  $file  Full path to file to upload.
42
	 *
43
	 * @since 1.0.5
44
	 * @see   https://crowdin.com/page/api/upload-tm
45
	 *
46
	 * @return string
47
	 */
48
	public function upload($file)
49
	{
50
		if (false === file_exists($file))
51
		{
52
			throw new \UnexpectedValueException('File not found for upload');
53
		}
54
55
		/*
56
		 * @todo Crowdin does not accept Guzzle's POST requests.
57
		 * She just tells me:
58
		 * <code>4</code>
59
		 * <message>No files specified in request</message>
60
		 *
61
		 *  :(
62
63
		return $this->getHttpClient()
64
			->post($this->getBasePath('upload-tm'), ['form_params' => ['file' => $file]]);
65
66
		HELP WANTED !!!
67
68
		 */
69
70
		// @codeCoverageIgnoreStart
71
72
		$post_params = array();
73
		$request_url = 'https://api.crowdin.com/api/' . $this->getBasePath('upload-tm');
74
75
		if (function_exists('curl_file_create'))
76
		{
77
			$post_params['file'] = curl_file_create($file);
78
		}
79
		else
80
		{
81
			/*
82
			 * This does NOT seem to work with Mrs. Crowdin...
83
			 * ... comes from their docs
84
			 * $post_params['file'] = '@/home/crowdin/test.tmx';
85
			 */
86
			throw new \RuntimeException('This version of cURL does not seem to work (with Crowdin...)');
87
		}
88
89
		$ch = curl_init();
90
91
		curl_setopt($ch, CURLOPT_URL, $request_url);
92
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
93
		curl_setopt($ch, CURLOPT_POST, true);
94
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
95
96
		$result = curl_exec($ch);
97
98
		curl_close($ch);
99
100
		if (false === $result)
101
		{
102
			throw new \UnexpectedValueException('File upload failed');
103
104
			// @todo Some more errors pls....
105
		}
106
107
		return 'File has been uploaded (?)';
108
109
		// @codeCoverageIgnoreEnd
110
	}
111
}
112