<?php

namespace Modules\Index\Service;

use Entity\Model\ParamDico;

class MagicSaml2Service
{
    private \PDO $pdo;
    private ParamDico $paramDico;

    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
        $this->paramDico = new ParamDico($this->pdo);
    }

    public function getStringForMagic(string $magicLoginUIID, string $login) : string
    {
        /**
         * Construct array for cond
         */
        $arrayCond =  [
            'TYPEDICO' => 'SAML_MAGIC',
            'ID1' => $magicLoginUIID
        ];

        /**
         * GetToken
         */
        $tokenMagicConnect = $this->paramDico->find(
            $arrayCond,
            ['LIBELLE']
        )['LIBELLE'];

        /**
         * Delete tuple in bdd
         */
        $this->paramDico->delete(
            $arrayCond
        );

        $this->paramDico->insert(
            [
                'TYPEDICO' => 'SAML_PHP',
                'ID1' => $tokenMagicConnect,
                'LIBELLE' => $login
            ]
        );

        /**
         * Return string for send
         */
        return $tokenMagicConnect;
    }
}
