Completed
Push — master ( cd36e7...80f49c )
by Nikolai
02:45
created

Directory::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 11
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 11
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
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 Directory
15
 *
16
 * @since  1.0.3
17
 */
18
Class Directory extends Package
19
{
20
	/**
21
	 * Add directory to Crowdin project.
22
	 *
23
	 * @param   string   $name      The name of the directory to add.
24
	 * @param   boolean  $isBranch  If set to 1 the directory will be marked as a version branch.
25
	 *                              Acceptable values are: 1 or 0.
26
	 * @param   string   $branch    The name of related version branch.
27
	 *
28
	 * @see https://crowdin.com/page/api/add-directory
29
	 * @since  1.0.3
30
	 *
31
	 * @return \Psr\Http\Message\ResponseInterface
32
	 */
33
	public function add($name, $isBranch = false, $branch = '')
34
	{
35
		$path = sprintf(
36
			'project/%s/add-directory?key=%s',
37
			$this->getProjectId(),
38
			$this->getApiKey()
39
		);
40
41
		$parameters = ['name' => $name];
42
43
		if ($isBranch)
44
		{
45
			$parameters['is_branch'] = '1';
46
		}
47
48
		if ('' !== $branch)
49
		{
50
			$parameters['branch'] = $branch;
51
		}
52
53
		return $this->getHttpClient()
54
			->post($path, ['form_params' => $parameters]);
55
	}
56
57
	/**
58
	 * Rename directory or modify its attributes.
59
	 * When renaming directory the path can not be changed
60
	 * (it means new_name parameter can not contain path, name only).
61
	 *
62
	 * @param   string  $name           Full directory path that should be modified (e.g. /MainPage/AboutUs).
63
	 * @param   string  $newName        New directory name.
64
	 * @param   string  $title          New directory title to be displayed in Crowdin UI.
65
	 * @param   string  $exportPattern  New directory export pattern.
66
	 *                                  Is used to create directory name and path in resulted translations bundle.
67
	 * @param   string  $branch         The name of related version branch.
68
	 *
69
	 * @see   https://crowdin.com/page/api/change-directory
70
	 * @since 1.0.3
71
	 *
72
	 * @return \Psr\Http\Message\ResponseInterface
73
	 */
74
	public function update($name, $newName = '', $title = '', $exportPattern = '', $branch = '')
75
	{
76
		$path = sprintf(
77
			'project/%s/change-directory?key=%s',
78
			$this->getProjectId(),
79
			$this->getApiKey()
80
		);
81
82
		$data = ['name' => $name];
83
84
		if ('' !== $newName)
85
		{
86
			$data['new_name'] = $newName;
87
		}
88
89
		if ('' !== $title)
90
		{
91
			$data['title'] = $title;
92
		}
93
94
		if ('' !== $exportPattern)
95
		{
96
			$data['export_pattern'] = $exportPattern;
97
		}
98
99
		if ('' !== $branch)
100
		{
101
			$data['branch'] = $branch;
102
		}
103
104
		return $this->getHttpClient()
105
			->post($path, ['form_params' => $data]);
106
	}
107
108
	/**
109
	 * Delete Crowdin project directory.
110
	 * All nested files and directories will be deleted too.
111
	 *
112
	 * @param   string  $name  The name of the directory to delete.
113
	 *
114
	 * @see   https://crowdin.com/page/api/delete-directory
115
	 * @since 1.0.3
116
	 *
117
	 * @return \Psr\Http\Message\ResponseInterface
118
	 */
119 View Code Duplication
	public function delete($name)
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...
120
	{
121
		$path = sprintf(
122
			'project/%s/delete-directory?key=%s',
123
			$this->getProjectId(),
124
			$this->getApiKey()
125
		);
126
127
		return $this->getHttpClient()
128
			->post($path, ['form_params' => ['name' => $name]]);
129
	}
130
}
131