-
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.
Added support for new recommendation commands.
- Loading branch information
1 parent
7860917
commit c31c4e2
Showing
25 changed files
with
1,176 additions
and
425 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
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
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
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,41 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\Model\Assertion; | ||
|
||
/** | ||
* Deliver personalized recommendations for the given user. | ||
*/ | ||
abstract class AbstractItemRecommendation extends AbstractRecommendation | ||
{ | ||
/** @var string */ | ||
private $itemId; | ||
|
||
protected function __construct(string $itemId, string $scenario) | ||
{ | ||
parent::__construct($scenario); | ||
$this->setItemId($itemId); | ||
} | ||
|
||
public function getItemId(): string | ||
{ | ||
return $this->itemId; | ||
} | ||
|
||
protected function setItemId(string $itemId): void | ||
{ | ||
Assertion::typeIdentifier($itemId); | ||
|
||
$this->itemId = $itemId; | ||
} | ||
|
||
protected function getCommandParameters(): array | ||
{ | ||
$parameters = parent::getCommandParameters(); | ||
|
||
$parameters['item_id'] = $this->itemId; | ||
|
||
return $parameters; | ||
} | ||
} |
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,81 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\Model\Assertion; | ||
|
||
/** | ||
* Deliver personalized recommendations for the given user. | ||
*/ | ||
abstract class AbstractRecommendation extends AbstractCommand | ||
{ | ||
/** @var int */ | ||
private $count; | ||
/** @var string */ | ||
private $scenario; | ||
/** @var string|null */ | ||
private $modelName; | ||
|
||
protected function __construct(string $scenario) | ||
{ | ||
$this->setScenario($scenario); | ||
} | ||
|
||
/** | ||
* Set A/B model name | ||
* | ||
* @return $this | ||
*/ | ||
public function setModelName(string $modelName): self | ||
{ | ||
Assertion::typeIdentifier($modelName); | ||
|
||
$this->modelName = $modelName; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Set number of requested recommendations. The real number of recommended items could be lower or even zero when | ||
* there are no items relevant for the user. | ||
* | ||
* @return $this | ||
*/ | ||
public function setCount(int $count): self | ||
{ | ||
Assertion::greaterThan($count, 0); | ||
|
||
$this->count = $count; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Scenario name. | ||
*/ | ||
protected function setScenario(string $scenario): void | ||
{ | ||
Assertion::typeIdentifier($scenario); | ||
|
||
$this->scenario = $scenario; | ||
} | ||
|
||
abstract protected function getCommandType(): string; | ||
|
||
protected function getCommandParameters(): array | ||
{ | ||
$parameters = [ | ||
'scenario' => $this->scenario, | ||
]; | ||
|
||
if ($this->count !== null) { | ||
$parameters['count'] = $this->count; | ||
} | ||
|
||
if ($this->modelName !== null) { | ||
$parameters['model_name'] = $this->modelName; | ||
} | ||
|
||
return $parameters; | ||
} | ||
} |
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,126 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Lmc\Matej\Model\Command; | ||
|
||
use Lmc\Matej\Model\Assertion; | ||
|
||
/** | ||
* Deliver personalized recommendations for the given user. | ||
*/ | ||
abstract class AbstractUserRecommendation extends AbstractRecommendation implements UserAwareInterface | ||
{ | ||
/** @var string */ | ||
private $userId; | ||
/** @var float */ | ||
private $rotationRate; | ||
/** @var int */ | ||
private $rotationTime; | ||
/** @var bool */ | ||
private $hardRotation; | ||
/** @var bool */ | ||
private $allowSeen = false; | ||
|
||
protected function __construct(string $userId, string $scenario) | ||
{ | ||
parent::__construct($scenario); | ||
$this->setUserId($userId); | ||
} | ||
|
||
/** | ||
* Even with rotation rate 1.0 user could still obtain the same recommendations in some edge cases. | ||
* To prevent this, enable hard rotation - recommended items are then excluded until rotation time is expired. | ||
* By default hard rotation is not enabled. | ||
* @return $this | ||
*/ | ||
public function enableHardRotation(): self | ||
{ | ||
$this->hardRotation = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Allow items, that the user has already "seen" | ||
* | ||
* By default user won't see any items, that it has visited (and we have recorded DetailView interaction.) | ||
* If you want to circumvent this, and get recommendations including the ones, that the user has already visited, | ||
* you can set the "seen" allowance here. | ||
* | ||
* @return $this | ||
*/ | ||
public function setAllowSeen(bool $seen): self | ||
{ | ||
$this->allowSeen = $seen; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get id of user requesting recommendation. | ||
*/ | ||
public function getUserId(): string | ||
{ | ||
return $this->userId; | ||
} | ||
|
||
/** | ||
* Set how much should the item be penalized for being recommended again in the near future. | ||
* | ||
* @return $this | ||
*/ | ||
public function setRotationRate(float $rotationRate): self | ||
{ | ||
Assertion::between($rotationRate, 0, 1); | ||
|
||
$this->rotationRate = $rotationRate; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Specify for how long will the item's rotationRate be taken in account and so the item is penalized for | ||
* recommendations. | ||
* | ||
* @return $this | ||
*/ | ||
public function setRotationTime(int $rotationTime): self | ||
{ | ||
Assertion::greaterOrEqualThan($rotationTime, 0); | ||
|
||
$this->rotationTime = $rotationTime; | ||
|
||
return $this; | ||
} | ||
|
||
protected function setUserId(string $userId): void | ||
{ | ||
Assertion::typeIdentifier($userId); | ||
|
||
$this->userId = $userId; | ||
} | ||
|
||
protected function getCommandParameters(): array | ||
{ | ||
$parameters = parent::getCommandParameters(); | ||
|
||
$parameters['user_id'] = $this->userId; | ||
|
||
if ($this->rotationRate !== null) { | ||
$parameters['rotation_rate'] = $this->rotationRate; | ||
} | ||
|
||
if ($this->rotationRate !== null) { | ||
$parameters['rotation_time'] = $this->rotationTime; | ||
} | ||
|
||
if ($this->allowSeen !== false) { | ||
$parameters['allow_seen'] = $this->allowSeen; | ||
} | ||
|
||
if ($this->hardRotation !== null) { | ||
$parameters['hard_rotation'] = $this->hardRotation; | ||
} | ||
|
||
return $parameters; | ||
} | ||
} |
Oops, something went wrong.