From 64c5340a839f1bd49f3c463458d51b8198bb4ea6 Mon Sep 17 00:00:00 2001 From: LaetitiaRiffaud Date: Fri, 19 May 2017 18:17:37 +0200 Subject: [PATCH] Add get potential authors for a project (#97) --- lib/Textmaster/Model/Author.php | 52 +++++++++++++++- lib/Textmaster/Model/AuthorInterface.php | 16 +++++ lib/Textmaster/Model/Project.php | 27 +++++++++ lib/Textmaster/Model/ProjectInterface.php | 13 ++++ test/Textmaster/Unit/Model/ProjectTest.php | 70 +++++++++++++++++++++- 5 files changed, 176 insertions(+), 2 deletions(-) diff --git a/lib/Textmaster/Model/Author.php b/lib/Textmaster/Model/Author.php index ef0db81..34aab17 100644 --- a/lib/Textmaster/Model/Author.php +++ b/lib/Textmaster/Model/Author.php @@ -11,10 +11,60 @@ namespace Textmaster\Model; -class Author implements AuthorInterface +class Author extends AbstractObject implements AuthorInterface { /** * @var string */ protected $id; + + /** + * {@inheritdoc} + */ + public function getAuthorId() + { + return $this->getProperty('author_id'); + } + + /** + * {@inheritdoc} + */ + public function setAuthorId($authorId) + { + return $this->setProperty('author_id', $authorId); + } + + /** + * Get the Author Api object. + * + * @return \Textmaster\Api\Author + */ + protected function getApi() + { + return $this->client->authors(); + } + + /** + * {@inheritdoc} + */ + protected function getEventNamePrefix() + { + return 'textmaster.author'; + } + + /** + * {@inheritdoc} + */ + public function getStatus() + { + return $this->getProperty('status'); + } + + /** + * {@inheritdoc} + */ + protected function isImmutable() + { + return true; + } } diff --git a/lib/Textmaster/Model/AuthorInterface.php b/lib/Textmaster/Model/AuthorInterface.php index f0c9110..8b23376 100644 --- a/lib/Textmaster/Model/AuthorInterface.php +++ b/lib/Textmaster/Model/AuthorInterface.php @@ -19,4 +19,20 @@ interface AuthorInterface extends AbstractObjectInterface * @return AuthorInterface */ public function save(); + + /** + * Get author ID + * + * @return string + */ + public function getAuthorId(); + + /** + * Set author ID + * + * @param string $authorId + * + * @return AuthorInterface + */ + public function setAuthorId($authorId); } diff --git a/lib/Textmaster/Model/Project.php b/lib/Textmaster/Model/Project.php index ec5ca5d..0f96416 100644 --- a/lib/Textmaster/Model/Project.php +++ b/lib/Textmaster/Model/Project.php @@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\GenericEvent; use Textmaster\Events; use Textmaster\Exception\BadMethodCallException; +use Textmaster\Exception\ErrorException; use Textmaster\Exception\InvalidArgumentException; use Textmaster\Exception\UnexpectedTypeException; use Textmaster\Pagination\PagerfantaAdapter; @@ -316,6 +317,32 @@ public function addDocuments(array $documents) return $this; } + /** + * {@inheritdoc} + */ + public function getPotentialAuthors($status = null) + { + if (null === $id = $this->getId()) { + throw new BadMethodCallException( + 'The project must be saved before getting authors who can do it.' + ); + } + + $results = []; + $nextPage = 0; + + while (null !== $nextPage) { + $response = $this->getApi()->authors($id)->setPage($nextPage + 1)->all($status); + + $nextPage = $response['next_page']; + $results = array_merge($results, array_map(function ($author) { + return new Author($this->client, $author); + }, $response['my_authors'])); + } + + return $results; + } + /** * {@inheritdoc} */ diff --git a/lib/Textmaster/Model/ProjectInterface.php b/lib/Textmaster/Model/ProjectInterface.php index 9b21c3b..d1f0365 100644 --- a/lib/Textmaster/Model/ProjectInterface.php +++ b/lib/Textmaster/Model/ProjectInterface.php @@ -13,6 +13,7 @@ use Pagerfanta\Pagerfanta; use Textmaster\Exception\BadMethodCallException; +use Textmaster\Exception\ErrorException; use Textmaster\Exception\InvalidArgumentException; use Textmaster\Exception\ObjectImmutableException; use Textmaster\Exception\UnexpectedTypeException; @@ -269,6 +270,18 @@ public function createDocument(); */ public function addDocuments(array $documents); + /** + * Get all my authors who can do this project + * + * @param string|null $status Possible values: uncategorized, my_textmaster, blacklisted + * + * @return AuthorInterface[] + * + * @throws BadMethodCallException If the project has no id. + * @throws ErrorException If none of my authors can do the project. + */ + public function getPotentialAuthors($status = null); + /** * Launch the project asynchronously. * diff --git a/test/Textmaster/Unit/Model/ProjectTest.php b/test/Textmaster/Unit/Model/ProjectTest.php index 640be8b..ae73d01 100644 --- a/test/Textmaster/Unit/Model/ProjectTest.php +++ b/test/Textmaster/Unit/Model/ProjectTest.php @@ -11,6 +11,7 @@ namespace Textmaster\Unit\Model; +use Textmaster\Model\AuthorInterface; use Textmaster\Model\DocumentInterface; use Textmaster\Model\Project; use Textmaster\Model\ProjectInterface; @@ -49,9 +50,43 @@ public function setUp() 'textmasters' => ['55c3763e656462000b000027'], ]; + $authors = [ + 'total_pages' => 1, + 'count' => 1, + 'page' => 1, + 'per_page' => 20, + 'previous_page' => null, + 'next_page' => null, + 'my_authors' => [ + [ + 'description' => '', + 'tags' => [], + 'status' => 'my_textmaster', + 'id' => '5743286d28cf7f00031eb4c9', + 'author_id' => '55c3763e656462000b000027', + 'author_ref' => 'A-3727-TM', + 'author_name' => 'Test', + 'latest_activity' => '2017-02-06 16:42:03 UTC', + 'created_at' => [ + 'day' => 23, + 'month' => 5, + 'year' => 2016, + 'full' => '2016-05-23 15:57:33 UTC', + ], + 'updated_at' => [ + 'day' => 6, + 'month' => 2, + 'year' => 2017, + 'full' => '2017-02-06 14:06:41 UTC', + ] + ] + ] + ]; + $clientMock = $this->getMockBuilder('Textmaster\Client')->setMethods(['api'])->disableOriginalConstructor()->getMock(); - $projectApiMock = $this->getMock('Textmaster\Api\Project', ['show', 'update', 'launch'], [$clientMock]); + $projectApiMock = $this->getMock('Textmaster\Api\Project', ['show', 'update', 'launch', 'authors'], [$clientMock]); $documentApiMock = $this->getMock('Textmaster\Api\FilterableApiInterface', ['filter', 'getClient']); + $projectAuthorApiMock = $this->getMock('Textmaster\Api\Project\Author', ['all'], [$clientMock, 123456]); $clientMock->method('api') ->willReturn($projectApiMock); @@ -65,6 +100,12 @@ public function setUp() $projectApiMock->method('documents') ->willReturn($documentApiMock); + $projectApiMock->method('authors') + ->willReturn($projectAuthorApiMock); + + $projectAuthorApiMock->method('all') + ->willReturn($authors); + $this->clientMock = $clientMock; $this->projectApiMock = $projectApiMock; } @@ -171,6 +212,33 @@ public function shouldUpdate() $this->assertSame('Project Beta', $project->getName()); } + /** + * @test + */ + public function shouldGetPotentialAuthors() + { + $project = new Project($this->clientMock, '123456'); + $authors = $project->getPotentialAuthors(); + + $this->assertInternalType('array', $authors); + $this->assertCount(1, $authors); + + foreach ($authors as $author) { + $this->assertInstanceOf(AuthorInterface::class, $author); + $this->assertSame('55c3763e656462000b000027', $author->getAuthorId()); + } + } + + /** + * @test + * @expectedException \Textmaster\Exception\BadMethodCallException + */ + public function shouldNotGetPotentialAuthorsOnUnsaved() + { + $project = new Project($this->clientMock); + $project->getPotentialAuthors(); + } + /** * @test */