public function checkPermission($role, $context)
    {
        if (!is_string($role)) {
            throw new \TypeError('The role parameter must be a string.');
        }
        if (!$role) {
            throw new \InvalidArgumentException('The role parameter cannot be empty.');
        }
        if (!is_array($context)) {
            throw new \TypeError('The context parameter must be an array.');
        }
        if (!isset($context['user'])) {
            throw new \InvalidArgumentException(sprintf('The context parameter must contain a "user" key to be able to evaluate the %s flag.', $this->getName()));
        }

        $user = $context['user'];
        if (is_string($user)) { //Anonymous user
            return false;
        }

        if (!($user instanceof SecurityUserInterface)) {
            throw new \InvalidArgumentException('The user class must implement Symfony\Component\Security\Core\User\UserInterface to be able to evaluate the user role.');
        }

        $roles = $user->getRoles();

        // Use Symfony Security Role class to make roles compatible with RoleHierarchy::getReachableRoles().
        foreach ($roles as $i => $thisRole) {
            if (is_string($thisRole)) {
                $roles[$i] = new SecurityRole($thisRole);
            } elseif (!($thisRole instanceof SecurityRole)) {
                throw new \InvalidArgumentException('One of the roles of this user is neither a string nor an instance of Symfony\Component\Security\Core\Role\Role.');
            }
        }
        $roles = $this->roleHierarchy->getReachableRoles($roles);

        foreach ($roles as $thisRole) {
            $strRole = (string) $thisRole->getRole();
            if ($role === $strRole) {
                return true;
            }
        }

        return false;
    }