-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added PromotionRule and PromotionRuleTypes with registry
- Added CartQuantity rule - Test
- Loading branch information
Showing
20 changed files
with
629 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Contracts; | ||
|
||
use Vanilo\Contracts\Configurable; | ||
|
||
interface PromotionRule extends Configurable | ||
{ | ||
public function getRuleType(): PromotionRuleType; | ||
|
||
public function isRuleTypPassing(object $subject): bool; | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Contracts; | ||
|
||
use Nette\Schema\Schema; | ||
|
||
interface PromotionRuleType | ||
{ | ||
public static function getName(): string; | ||
|
||
public static function getID(): string; | ||
|
||
public function isPassing(object $subject): bool; | ||
|
||
public function getSchema(): ?Schema; | ||
|
||
public function setConfiguration(array $configuration): self; | ||
|
||
public function getConfiguration(): ?array; | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Exceptions; | ||
|
||
use RuntimeException; | ||
|
||
class InexistentRuleException extends RuntimeException | ||
{ | ||
} |
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,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Vanilo\Promotion\Contracts\Promotion; | ||
use Vanilo\Promotion\Contracts\PromotionRule as PromotionRuleContract; | ||
use Vanilo\Promotion\Contracts\PromotionRuleType; | ||
use Vanilo\Promotion\PromotionRuleTypes; | ||
use Vanilo\Support\Traits\ConfigurableModel; | ||
use Vanilo\Support\Traits\ConfigurationHasNoSchema; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $promotion_id | ||
* @property string $type | ||
* @property ?array $configuration | ||
* | ||
* @property Promotion $promotion | ||
*/ | ||
class PromotionRule extends Model implements PromotionRuleContract | ||
{ | ||
use ConfigurableModel; | ||
use ConfigurationHasNoSchema; | ||
|
||
protected $guarded = ['id', 'created_at', 'updated_at']; | ||
|
||
protected $casts = [ | ||
'configuration' => 'array', | ||
]; | ||
|
||
public function promotion(): BelongsTo | ||
{ | ||
return $this->belongsTo(PromotionProxy::modelClass()); | ||
} | ||
|
||
public function getRuleType(): PromotionRuleType | ||
{ | ||
return PromotionRuleTypes::make($this->type)->setConfiguration($this->configuration); | ||
} | ||
|
||
public function isRuleTypPassing(object $subject): bool | ||
{ | ||
return $this->getRuleType()->isPassing($subject); | ||
} | ||
} |
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,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Models; | ||
|
||
use Konekt\Concord\Proxies\ModelProxy; | ||
|
||
class PromotionRuleProxy extends ModelProxy | ||
{ | ||
} |
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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion; | ||
|
||
use Vanilo\Promotion\Contracts\PromotionRuleType; | ||
use Vanilo\Promotion\Exceptions\InexistentRuleException; | ||
|
||
final class PromotionRuleTypes | ||
{ | ||
private static array $registry = []; | ||
|
||
public static function register(string $id, string $class) | ||
{ | ||
if (array_key_exists($id, self::$registry)) { | ||
return; | ||
} | ||
|
||
if (!in_array(PromotionRuleType::class, class_implements($class))) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'The class you are trying to register (%s) as promotion rule, '. | ||
'must implement the %s interface.', | ||
$class, | ||
PromotionRuleType::class | ||
) | ||
); | ||
} | ||
|
||
self::$registry[$id] = $class; | ||
} | ||
|
||
public static function make(string $id): PromotionRuleType | ||
{ | ||
$gwClass = self::getClass($id); | ||
|
||
if (null === $gwClass) { | ||
throw new InexistentRuleException( | ||
"No rule is registered with the id `$id`." | ||
); | ||
} | ||
|
||
return app()->make($gwClass); | ||
} | ||
|
||
public static function reset(): void | ||
{ | ||
self::$registry = []; | ||
} | ||
|
||
public static function getClass(string $id): ?string | ||
{ | ||
return self::$registry[$id] ?? null; | ||
} | ||
|
||
public static function ids(): array | ||
{ | ||
return array_keys(self::$registry); | ||
} | ||
|
||
public static function choices(): array | ||
{ | ||
$result = []; | ||
|
||
foreach (self::$registry as $type => $class) { | ||
$result[$type] = $class::getName(); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Vanilo\Promotion\Rules; | ||
|
||
use Nette\Schema\Expect; | ||
use Nette\Schema\Processor; | ||
use Nette\Schema\Schema; | ||
use Vanilo\Cart\Contracts\Cart; | ||
use Vanilo\Promotion\Contracts\PromotionRuleType; | ||
|
||
class CartQuantity implements PromotionRuleType | ||
{ | ||
private ?array $configuration = null; | ||
|
||
public static function getName(): string | ||
{ | ||
return __('Cart quantity'); | ||
} | ||
|
||
public static function getID(): string | ||
{ | ||
return 'cart_quantity'; | ||
} | ||
|
||
public function setConfiguration(array $configuration): self | ||
{ | ||
if ($this->getSchema()) { | ||
$configuration = (new Processor())->process($this->getSchema(), $configuration); | ||
} | ||
|
||
$this->configuration = (array)$configuration; | ||
|
||
return $this; | ||
} | ||
|
||
public function getConfiguration(): ?array | ||
{ | ||
$configuration = $this->configuration; | ||
|
||
if ($this->getSchema()) { | ||
$configuration = (new Processor())->process($this->getSchema(), $configuration); | ||
} | ||
|
||
return (array)$configuration; | ||
} | ||
|
||
public function getSchema(): ?Schema | ||
{ | ||
return Expect::structure(['count' => Expect::int(0)->required()]); | ||
} | ||
|
||
public function isPassing(object $subject): bool | ||
{ | ||
if (!$subject instanceof Cart) { | ||
throw new \InvalidArgumentException('Subject must be an instance of Vanilo\Cart\Contracts\Cart'); | ||
} | ||
|
||
if (!$this->getConfiguration()) { | ||
return false; | ||
} | ||
|
||
return $subject->itemCount() <= $this->configuration['count']; | ||
} | ||
} |
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 Vanilo\Promotion\Tests\Examples; | ||
|
||
use Nette\Schema\Schema; | ||
use Vanilo\Promotion\Contracts\PromotionRuleType; | ||
|
||
class CartTotalRule implements PromotionRuleType | ||
{ | ||
public static function getName(): string | ||
{ | ||
return 'Cart Total'; | ||
} | ||
|
||
public static function getID(): string | ||
{ | ||
// TODO: Implement getID() method. | ||
} | ||
|
||
public function isPassing(object $subject): bool | ||
{ | ||
// TODO: Implement isPassing() method. | ||
} | ||
|
||
public function getSchema(): ?Schema | ||
{ | ||
// TODO: Implement getSchema() method. | ||
} | ||
|
||
public function setConfiguration(array $configuration): PromotionRuleType | ||
{ | ||
// TODO: Implement setConfiguration() method. | ||
} | ||
|
||
public function getConfiguration(): ?array | ||
{ | ||
// TODO: Implement getConfiguration() method. | ||
} | ||
} |
Oops, something went wrong.