-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 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,42 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
/** | ||
* Sorting items is a way how to use Matej to deliver personalized experience to users. | ||
* It allows to sort given list of items according to the user preference. | ||
*/ | ||
class Sorting extends AbstractCommand | ||
{ | ||
/** @var string */ | ||
private $userId; | ||
/** @var string[] */ | ||
private $itemIds = []; | ||
|
||
private function __construct(string $userId, array $itemIds) | ||
{ | ||
$this->userId = $userId; | ||
$this->itemIds = $itemIds; | ||
} | ||
|
||
/** | ||
* Sort given item ids for user-based recommendations. | ||
*/ | ||
public static function create(string $userId, array $itemIds): self | ||
{ | ||
return new static($userId, $itemIds); | ||
} | ||
|
||
public function getCommandType(): string | ||
{ | ||
return 'sorting'; | ||
} | ||
|
||
public function getCommandParameters(): array | ||
{ | ||
return [ | ||
'user_id' => $this->userId, | ||
'item_ids' => $this->itemIds, | ||
]; | ||
} | ||
} |
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,37 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\TestCase; | ||
|
||
class SortingTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function shouldBeInstantiableViaNamedConstructor(): void | ||
{ | ||
$userId = 'user-id'; | ||
$itemIds = ['item-1', 'item-3', 'item-2']; | ||
|
||
$command = Sorting::create($userId, $itemIds); | ||
$this->assertSortingObject($command, $userId, $itemIds); | ||
} | ||
|
||
/** | ||
* Execute asserts against user merge object | ||
* @param object $object | ||
*/ | ||
private function assertSortingObject($object, string $userId, array $itemIds): void | ||
{ | ||
$this->assertInstanceOf(Sorting::class, $object); | ||
$this->assertSame( | ||
[ | ||
'type' => 'sorting', | ||
'parameters' => [ | ||
'user_id' => $userId, | ||
'item_ids' => $itemIds, | ||
], | ||
], | ||
$object->jsonSerialize() | ||
); | ||
} | ||
} |