-
Notifications
You must be signed in to change notification settings - Fork 9
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
4 changed files
with
91 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace tpaycom\magento2basic\Model; | ||
|
||
use Magento\Checkout\Model\Session; | ||
|
||
class ConstraintValidator | ||
{ | ||
/** @var Session */ | ||
private $checkoutSession; | ||
|
||
public function __construct(Session $session) | ||
{ | ||
$this->checkoutSession = $session; | ||
} | ||
|
||
public function validate(array $constraints): bool | ||
{ | ||
foreach ($constraints as $constraint) { | ||
switch ($constraint['type']) { | ||
case 'min': | ||
if (!$this->validateMinimalTotal((float) $constraint['value'])) { | ||
return false; | ||
} | ||
|
||
break; | ||
case 'max': | ||
if (!$this->validateMaximalTotal((float) $constraint['value'])) { | ||
return false; | ||
} | ||
|
||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private function validateMinimalTotal(float $minimal): bool | ||
{ | ||
return $this->checkoutSession->getQuote()->getGrandTotal() > $minimal; | ||
} | ||
|
||
private function validateMaximalTotal(float $maximal): bool | ||
{ | ||
return $this->checkoutSession->getQuote()->getGrandTotal() < $maximal; | ||
} | ||
} |
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