commerce_funds-8.x-1.7/src/Plugin/Validation/Constraint/IssuerEqualsCurrentUserConstraintValidator.php
src/Plugin/Validation/Constraint/IssuerEqualsCurrentUserConstraintValidator.php
<?php
namespace Drupal\commerce_funds\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the IssuerEqualsCurrentUser Constraint.
*
* @package commerce_funds
*/
class IssuerEqualsCurrentUserConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* Class constructor.
*/
public function __construct(AccountProxyInterface $current_user) {
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
$recipient_id = $items->getValue()[0]['target_id'];
// Error if user try to make a transaction to itself.
if ($this->currentUser->id() == $recipient_id) {
$this->context->addViolation($constraint->message);
}
}
}
