From f8a7211b95206d9727dfc4459d780e5582d44ef6 Mon Sep 17 00:00:00 2001 From: pierallard Date: Sun, 27 Mar 2016 17:26:45 +0200 Subject: [PATCH] Add Change Directory action --- .../Crowdin/Api/ChangeDirectorySpec.php | 74 ++++++++++ src/Akeneo/Crowdin/Api/ChangeDirectory.php | 134 ++++++++++++++++++ 2 files changed, 208 insertions(+) create mode 100644 spec/Akeneo/Crowdin/Api/ChangeDirectorySpec.php create mode 100644 src/Akeneo/Crowdin/Api/ChangeDirectory.php diff --git a/spec/Akeneo/Crowdin/Api/ChangeDirectorySpec.php b/spec/Akeneo/Crowdin/Api/ChangeDirectorySpec.php new file mode 100644 index 0000000..79f1048 --- /dev/null +++ b/spec/Akeneo/Crowdin/Api/ChangeDirectorySpec.php @@ -0,0 +1,74 @@ +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); + } +} diff --git a/src/Akeneo/Crowdin/Api/ChangeDirectory.php b/src/Akeneo/Crowdin/Api/ChangeDirectory.php new file mode 100644 index 0000000..092050e --- /dev/null +++ b/src/Akeneo/Crowdin/Api/ChangeDirectory.php @@ -0,0 +1,134 @@ + + * @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; + } +}