Skip to content

Commit 64c5340

Browse files
LaetitiaRiffaudcdaguerre
authored andcommitted
Add get potential authors for a project (#97)
1 parent 2f66be0 commit 64c5340

File tree

5 files changed

+176
-2
lines changed

5 files changed

+176
-2
lines changed

lib/Textmaster/Model/Author.php

+51-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,60 @@
1111

1212
namespace Textmaster\Model;
1313

14-
class Author implements AuthorInterface
14+
class Author extends AbstractObject implements AuthorInterface
1515
{
1616
/**
1717
* @var string
1818
*/
1919
protected $id;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getAuthorId()
25+
{
26+
return $this->getProperty('author_id');
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function setAuthorId($authorId)
33+
{
34+
return $this->setProperty('author_id', $authorId);
35+
}
36+
37+
/**
38+
* Get the Author Api object.
39+
*
40+
* @return \Textmaster\Api\Author
41+
*/
42+
protected function getApi()
43+
{
44+
return $this->client->authors();
45+
}
46+
47+
/**
48+
* {@inheritdoc}
49+
*/
50+
protected function getEventNamePrefix()
51+
{
52+
return 'textmaster.author';
53+
}
54+
55+
/**
56+
* {@inheritdoc}
57+
*/
58+
public function getStatus()
59+
{
60+
return $this->getProperty('status');
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
protected function isImmutable()
67+
{
68+
return true;
69+
}
2070
}

lib/Textmaster/Model/AuthorInterface.php

+16
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,20 @@ interface AuthorInterface extends AbstractObjectInterface
1919
* @return AuthorInterface
2020
*/
2121
public function save();
22+
23+
/**
24+
* Get author ID
25+
*
26+
* @return string
27+
*/
28+
public function getAuthorId();
29+
30+
/**
31+
* Set author ID
32+
*
33+
* @param string $authorId
34+
*
35+
* @return AuthorInterface
36+
*/
37+
public function setAuthorId($authorId);
2238
}

lib/Textmaster/Model/Project.php

+27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\EventDispatcher\GenericEvent;
1616
use Textmaster\Events;
1717
use Textmaster\Exception\BadMethodCallException;
18+
use Textmaster\Exception\ErrorException;
1819
use Textmaster\Exception\InvalidArgumentException;
1920
use Textmaster\Exception\UnexpectedTypeException;
2021
use Textmaster\Pagination\PagerfantaAdapter;
@@ -316,6 +317,32 @@ public function addDocuments(array $documents)
316317
return $this;
317318
}
318319

320+
/**
321+
* {@inheritdoc}
322+
*/
323+
public function getPotentialAuthors($status = null)
324+
{
325+
if (null === $id = $this->getId()) {
326+
throw new BadMethodCallException(
327+
'The project must be saved before getting authors who can do it.'
328+
);
329+
}
330+
331+
$results = [];
332+
$nextPage = 0;
333+
334+
while (null !== $nextPage) {
335+
$response = $this->getApi()->authors($id)->setPage($nextPage + 1)->all($status);
336+
337+
$nextPage = $response['next_page'];
338+
$results = array_merge($results, array_map(function ($author) {
339+
return new Author($this->client, $author);
340+
}, $response['my_authors']));
341+
}
342+
343+
return $results;
344+
}
345+
319346
/**
320347
* {@inheritdoc}
321348
*/

lib/Textmaster/Model/ProjectInterface.php

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Pagerfanta\Pagerfanta;
1515
use Textmaster\Exception\BadMethodCallException;
16+
use Textmaster\Exception\ErrorException;
1617
use Textmaster\Exception\InvalidArgumentException;
1718
use Textmaster\Exception\ObjectImmutableException;
1819
use Textmaster\Exception\UnexpectedTypeException;
@@ -269,6 +270,18 @@ public function createDocument();
269270
*/
270271
public function addDocuments(array $documents);
271272

273+
/**
274+
* Get all my authors who can do this project
275+
*
276+
* @param string|null $status Possible values: uncategorized, my_textmaster, blacklisted
277+
*
278+
* @return AuthorInterface[]
279+
*
280+
* @throws BadMethodCallException If the project has no id.
281+
* @throws ErrorException If none of my authors can do the project.
282+
*/
283+
public function getPotentialAuthors($status = null);
284+
272285
/**
273286
* Launch the project asynchronously.
274287
*

test/Textmaster/Unit/Model/ProjectTest.php

+69-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Textmaster\Unit\Model;
1313

14+
use Textmaster\Model\AuthorInterface;
1415
use Textmaster\Model\DocumentInterface;
1516
use Textmaster\Model\Project;
1617
use Textmaster\Model\ProjectInterface;
@@ -49,9 +50,43 @@ public function setUp()
4950
'textmasters' => ['55c3763e656462000b000027'],
5051
];
5152

53+
$authors = [
54+
'total_pages' => 1,
55+
'count' => 1,
56+
'page' => 1,
57+
'per_page' => 20,
58+
'previous_page' => null,
59+
'next_page' => null,
60+
'my_authors' => [
61+
[
62+
'description' => '',
63+
'tags' => [],
64+
'status' => 'my_textmaster',
65+
'id' => '5743286d28cf7f00031eb4c9',
66+
'author_id' => '55c3763e656462000b000027',
67+
'author_ref' => 'A-3727-TM',
68+
'author_name' => 'Test',
69+
'latest_activity' => '2017-02-06 16:42:03 UTC',
70+
'created_at' => [
71+
'day' => 23,
72+
'month' => 5,
73+
'year' => 2016,
74+
'full' => '2016-05-23 15:57:33 UTC',
75+
],
76+
'updated_at' => [
77+
'day' => 6,
78+
'month' => 2,
79+
'year' => 2017,
80+
'full' => '2017-02-06 14:06:41 UTC',
81+
]
82+
]
83+
]
84+
];
85+
5286
$clientMock = $this->getMockBuilder('Textmaster\Client')->setMethods(['api'])->disableOriginalConstructor()->getMock();
53-
$projectApiMock = $this->getMock('Textmaster\Api\Project', ['show', 'update', 'launch'], [$clientMock]);
87+
$projectApiMock = $this->getMock('Textmaster\Api\Project', ['show', 'update', 'launch', 'authors'], [$clientMock]);
5488
$documentApiMock = $this->getMock('Textmaster\Api\FilterableApiInterface', ['filter', 'getClient']);
89+
$projectAuthorApiMock = $this->getMock('Textmaster\Api\Project\Author', ['all'], [$clientMock, 123456]);
5590

5691
$clientMock->method('api')
5792
->willReturn($projectApiMock);
@@ -65,6 +100,12 @@ public function setUp()
65100
$projectApiMock->method('documents')
66101
->willReturn($documentApiMock);
67102

103+
$projectApiMock->method('authors')
104+
->willReturn($projectAuthorApiMock);
105+
106+
$projectAuthorApiMock->method('all')
107+
->willReturn($authors);
108+
68109
$this->clientMock = $clientMock;
69110
$this->projectApiMock = $projectApiMock;
70111
}
@@ -171,6 +212,33 @@ public function shouldUpdate()
171212
$this->assertSame('Project Beta', $project->getName());
172213
}
173214

215+
/**
216+
* @test
217+
*/
218+
public function shouldGetPotentialAuthors()
219+
{
220+
$project = new Project($this->clientMock, '123456');
221+
$authors = $project->getPotentialAuthors();
222+
223+
$this->assertInternalType('array', $authors);
224+
$this->assertCount(1, $authors);
225+
226+
foreach ($authors as $author) {
227+
$this->assertInstanceOf(AuthorInterface::class, $author);
228+
$this->assertSame('55c3763e656462000b000027', $author->getAuthorId());
229+
}
230+
}
231+
232+
/**
233+
* @test
234+
* @expectedException \Textmaster\Exception\BadMethodCallException
235+
*/
236+
public function shouldNotGetPotentialAuthorsOnUnsaved()
237+
{
238+
$project = new Project($this->clientMock);
239+
$project->getPotentialAuthors();
240+
}
241+
174242
/**
175243
* @test
176244
*/

0 commit comments

Comments
 (0)