<?php

use KupShop\KupShopBundle\Util\Compat\ServiceContainer;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;

function smarty_function_get_logged_user_provider_ids($params, &$smarty)
{
    /** @var $tokenStorage TokenStorage */
    $tokenStorage = ServiceContainer::getService('security.token_storage');
    $token = $tokenStorage->getToken();

    if ($token && ($user = $token->getUser())) {
        if (method_exists($user, 'getID')) {
            $providers = sqlQueryBuilder()->select('*')->from('users_provider_ids')
                ->where('id_user=:id_user')->setParameter('id_user', $user->getID())
                ->orderBy('provider')->addOrderBy('email')->execute()->fetchAll();

            if (!empty($params['assign'])) {
                $smarty->assign($params['assign'], $providers);
            } else {
                return $providers;
            }
        }
    }
}
