<?php
declare(strict_types=1);

namespace App\Validator\Organization\Parameters;

use App\Entity\Organization\Parameters;
use App\Service\Mobyt\MobytService;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * Classe control que les logins/mot de passe mobyt sont OK.
 */
class MobytCredentialsValidator extends ConstraintValidator
{
    public function __construct(private MobytService $mobytService){}

    public function validate($value, Constraint $constraint)
    {
        /** @var Parameters $parameters */
        $parameters = $value;

        $userNameSms = $parameters->getUsernameSMS();
        $passwordSms = $parameters->getPasswordSMS();

        if(empty($userNameSms) || empty($passwordSms))
            return;

        try{
            $this->mobytService->hasCredentialsCorrect($userNameSms, $passwordSms);
        }catch (\Exception $exception){
            $this->context->buildViolation($constraint->message)
                ->atPath('passwordSMS')
                ->addViolation();
        }
    }
}
