<?php

namespace MintyPHP\Service\Auth;

/**
 * Thin wrapper around PHP ldap_* functions for testability.
 *
 * Every public method maps 1:1 to a native ldap_* call.
 * In tests, this gateway is replaced with a mock.
 */
class LdapConnectionGateway
{
    /** @return \LDAP\Connection|false */
    public function connect(string $uri): \LDAP\Connection|false
    {
        return ldap_connect($uri);
    }

    public function setOption(\LDAP\Connection $conn, int $option, mixed $value): bool
    {
        return ldap_set_option($conn, $option, $value);
    }

    public function startTls(\LDAP\Connection $conn): bool
    {
        return ldap_start_tls($conn);
    }

    public function bind(\LDAP\Connection $conn, ?string $dn = null, ?string $password = null): bool
    {
        return @ldap_bind($conn, $dn, $password);
    }

    /**
     * @return mixed LDAP\Result on success, false on failure.
     */
    public function search(\LDAP\Connection $conn, string $baseDn, string $filter, array $attributes = [], int $scope = 0): mixed
    {
        if ($scope === 1) {
            return @ldap_list($conn, $baseDn, $filter, $attributes);
        }
        return @ldap_search($conn, $baseDn, $filter, $attributes);
    }

    public function getEntries(\LDAP\Connection $conn, mixed $result): array|false
    {
        if (!$result instanceof \LDAP\Result) {
            return false;
        }

        return ldap_get_entries($conn, $result);
    }

    public function escape(string $value, string $ignore = '', int $flags = 0): string
    {
        return ldap_escape($value, $ignore, $flags);
    }

    public function errno(\LDAP\Connection $conn): int
    {
        return ldap_errno($conn);
    }

    public function error(\LDAP\Connection $conn): string
    {
        return ldap_error($conn);
    }

    public function unbind(\LDAP\Connection $conn): bool
    {
        return @ldap_unbind($conn);
    }
}
