-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from akeneo/GIT-5
Add Change Directory action
- Loading branch information
Showing
2 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace spec\Akeneo\Crowdin\Api; | ||
|
||
use Akeneo\Crowdin\Client; | ||
use Guzzle\Http\Client as HttpClient; | ||
use Guzzle\Http\EntityBodyInterface; | ||
use Guzzle\Http\Message\Request; | ||
use Guzzle\Http\Message\Response; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class ChangeDirectorySpec extends ObjectBehavior | ||
{ | ||
public function let(Client $client, HttpClient $http) | ||
{ | ||
$client->getHttpClient()->willReturn($http); | ||
$client->getProjectIdentifier()->willReturn('sylius'); | ||
$client->getProjectApiKey()->willReturn('1234'); | ||
$this->beConstructedWith($client); | ||
} | ||
|
||
public function it_should_be_an_api() | ||
{ | ||
$this->shouldBeAnInstanceOf('Akeneo\Crowdin\Api\AbstractApi'); | ||
} | ||
|
||
public function it_should_have_a_name() | ||
{ | ||
$this->shouldThrow('\InvalidArgumentException')->during('execute', []); | ||
} | ||
|
||
public function it_should_set_name( | ||
$http, | ||
Request $request, | ||
Response $response, | ||
EntityBodyInterface $body | ||
) { | ||
$this->setName('myname'); | ||
$path = 'project/sylius/change-directory?key=1234'; | ||
$data = ['name' => 'myname']; | ||
$http->post($path, [], $data)->willReturn($request); | ||
$request->send()->willReturn($response); | ||
$response->getBody(Argument::any())->willReturn($body); | ||
|
||
$this->execute()->shouldReturn($body); | ||
} | ||
|
||
public function it_should_set_data( | ||
$http, | ||
Request $request, | ||
Response $response, | ||
EntityBodyInterface $body | ||
) { | ||
$this->setName('myName'); | ||
$this->setBranch('myBranch'); | ||
$this->setExportPattern('myExportPattern'); | ||
$this->setTitle('myTitle'); | ||
$this->setNewName('myNewName'); | ||
$path = 'project/sylius/change-directory?key=1234'; | ||
$data = [ | ||
'name' => 'myName', | ||
'branch' => 'myBranch', | ||
'export_pattern' => 'myExportPattern', | ||
'title' => 'myTitle', | ||
'new_name' => 'myNewName' | ||
]; | ||
$http->post($path, [], $data)->willReturn($request); | ||
$request->send()->willReturn($response); | ||
$response->getBody(Argument::any())->willReturn($body); | ||
|
||
$this->execute()->shouldReturn($body); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
namespace Akeneo\Crowdin\Api; | ||
|
||
use \InvalidArgumentException; | ||
|
||
/** | ||
* Rename directory or modify its attributes. When renaming directory the path can not be changed (it means new_name | ||
* parameter can not contain path, name only). | ||
* | ||
* @author Pierre Allard <[email protected]> | ||
* @see https://crowdin.com/page/api/change-directory | ||
*/ | ||
class ChangeDirectory extends AbstractApi | ||
{ | ||
/** @var string */ | ||
protected $name; | ||
|
||
/** @var string */ | ||
protected $newName; | ||
|
||
/** @var string */ | ||
protected $title; | ||
|
||
/** @var string */ | ||
protected $exportPattern; | ||
|
||
/** @var string */ | ||
protected $branch; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function execute() | ||
{ | ||
if (null == $this->name) { | ||
throw new InvalidArgumentException('Argument name is required.'); | ||
} | ||
|
||
$path = sprintf( | ||
"project/%s/change-directory?key=%s", | ||
$this->client->getProjectIdentifier(), | ||
$this->client->getProjectApiKey() | ||
); | ||
|
||
$data = array_merge($this->parameters, ['name' => $this->name]); | ||
|
||
if (null !== $this->newName) { | ||
$data['new_name'] = $this->newName; | ||
} | ||
if (null !== $this->title) { | ||
$data['title'] = $this->title; | ||
} | ||
if (null !== $this->exportPattern) { | ||
$data['export_pattern'] = $this->exportPattern; | ||
} | ||
if (null !== $this->branch) { | ||
$data['branch'] = $this->branch; | ||
} | ||
|
||
$request = $this->client->getHttpClient()->post($path, [], $data); | ||
$response = $request->send(); | ||
|
||
return $response->getBody(true); | ||
} | ||
|
||
/** | ||
* @param string $branch | ||
* | ||
* @return ChangeDirectory | ||
*/ | ||
public function setBranch($branch) | ||
{ | ||
$this->branch = $branch; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set new directory export pattern. Is used to create directory name and path in resulted translations bundle. | ||
* | ||
* @param string $exportPattern | ||
* | ||
* @return ChangeDirectory | ||
*/ | ||
public function setExportPattern($exportPattern) | ||
{ | ||
$this->exportPattern = $exportPattern; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set new directory title to be displayed in Crowdin UI. | ||
* | ||
* @param string $title | ||
* | ||
* @return ChangeDirectory | ||
*/ | ||
public function setTitle($title) | ||
{ | ||
$this->title = $title; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set new directory name. | ||
* | ||
* @param string $newName | ||
* | ||
* @return ChangeDirectory | ||
*/ | ||
public function setNewName($newName) | ||
{ | ||
$this->newName = $newName; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set full directory path that should be modified (e.g. /MainPage/AboutUs). | ||
* | ||
* @param string $name | ||
* | ||
* @return ChangeDirectory | ||
*/ | ||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
|
||
return $this; | ||
} | ||
} |