Skip to content

Commit

Permalink
Add get potential authors for a project (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaetitiaRiffaud authored and cdaguerre committed May 19, 2017
1 parent 2f66be0 commit 64c5340
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 2 deletions.
52 changes: 51 additions & 1 deletion lib/Textmaster/Model/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
16 changes: 16 additions & 0 deletions lib/Textmaster/Model/AuthorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
27 changes: 27 additions & 0 deletions lib/Textmaster/Model/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
*/
Expand Down
13 changes: 13 additions & 0 deletions lib/Textmaster/Model/ProjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
70 changes: 69 additions & 1 deletion test/Textmaster/Unit/Model/ProjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Textmaster\Unit\Model;

use Textmaster\Model\AuthorInterface;
use Textmaster\Model\DocumentInterface;
use Textmaster\Model\Project;
use Textmaster\Model\ProjectInterface;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 64c5340

Please sign in to comment.