<commit_before><?php

/*
 * This file is part of the Toyota Legacy PHP framework package.
 *
 * (c) Toyota Industrial Equipment <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Toyota\Component\Ldap\Core;

use Toyota\Component\Ldap\API\DriverInterface;
use Toyota\Component\Ldap\API\SearchInterface;
use Toyota\Component\Ldap\API\ConnectionInterface;
use Toyota\Component\Ldap\Exception\NodeNotFoundException;
use Toyota\Component\Ldap\Exception\NoResultException;
use Toyota\Component\Ldap\Exception\NotBoundException;
use Toyota\Component\Ldap\Exception\PersistenceException;
use Toyota\Component\Ldap\Exception\DeleteException;
use Toyota\Component\Ldap\Core\Node;

/**
 * Class to handle Ldap operations
 *
 * @author <NAME> <<EMAIL>>
 */
class Manager
{

    protected $connection = null;
    protected $isBound = false;
    protected $configuration = array();
    protected $driver = null;

    /**
     * Default constructor
     *
     * Parameters structure:
     *     Required:
     *         hostname      => ldap.example.com
     *         base_dn       => dc=example,dc=com
     *     Optional:
     *         port          => 389
     *         security      => one of SSL or TLS
     *         bind_dn       => cn=admin,dc=example,dc=com (bind will be anonymous if not given)
     *         bind_password => secret (can be plain or a hash)
     *         page_size     => page size (default 1000, set to 0 for no paging)
     *         options       => array of options according to driver specifications
     *
     * @param array           $params Connection parameters
     * @param DriverInterface $driver Driver to use for execution
     *
     * @return Manager
     *
     * @throws \InvalidArgumentException if parameters are incorrect
     */
    public function __construct(array $params, DriverInterface $driver)
    {
        $this->configure($params);
        $this->driver = $driver;
    }

    /**
     * Connects to the Ldap store
     *
     * @return void
     *
     * @throws Toyota\Component\Ldap\Exception\ConnectionException if connection fails
     * @throws Toyota\Component\Ldap\Exception\OptionException if connection configuration fails
     */
    public function connect()
    {
        $this->isBound = false;
        $this->connection = $this->driver->connect(
                $this->configuration['hostname'], $this->configuration['port'], $this->configuration['withSSL'], $this->configuration['withTLS']
        );

        foreach ($this->configuration['options'] as $key => $value) {
            $this->connection->setOption($key, $value);
        }
    }

    /**
     * Binds to the Ldap connection
     *
     * @param string $name     Bind rdn (Default: null)
     * @param string $password Bind password (Default: null)
     *
     * @return void
     *
     * @throws Toyota\Component\Ldap\Exception\BindException if binding fails
     */
    public function bind($name = null, $password = null)
    {
        if (strlen(trim($name)) > 0) {
            $password = (null === $password) ? '' : $password;
            $this->connection->bind($name, $password);
            $this->isBound = true;
            return;
        }

        if ($this->configuration['bind_anonymous']) {
            $this->connection->bind();
            $this->isBound = true;
            return;
        }

        $this->connection->bind(
                $this->configuration['bind_dn'], $this->configuration['bind_password']
        );
        $this->isBound = true;
    }

    /**
     * Complete and validates given parameters with default settings
     *
     * @param array $params Parameters to be cleaned
     *
     * @return array Cleaned parameters
     *
     * @throws \InvalidArgumentException
     */
    protected function configure(array $params)
    {

        $required = array('hostname', 'base_dn');
        $missing = array();
        foreach ($required as $key) {
            if (!array_key_exists($key, $params)) {<commit_msg>fix style<commit_after><?php

/*
 * This file is part of the Toyota Legacy PHP framework package.
 *
 * (c) Toyota Industrial Equipment <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Toyota\Component\Ldap\Core;

use Toyota\Component\Ldap\API\DriverInterface;
use Toyota\Component\Ldap\API\SearchInterface;
use Toyota\Component\Ldap\API\ConnectionInterface;
use Toyota\Component\Ldap\Exception\NodeNotFoundException;
use Toyota\Component\Ldap\Exception\NoResultException;
use Toyota\Component\Ldap\Exception\NotBoundException;
use Toyota\Component\Ldap\Exception\PersistenceException;
use Toyota\Component\Ldap\Exception\DeleteException;
use Toyota\Component\Ldap\Core\Node;

/**
 * Class to handle Ldap operations
 *
 * @author <NAME> <<EMAIL>>
 */
class Manager
{

    protected $connection = null;
    protected $isBound = false;
    protected $configuration = array();
    protected $driver = null;

    /**
     * Default constructor
     *
     * Parameters structure:
     *     Required:
     *         hostname      => ldap.example.com
     *         base_dn       => dc=example,dc=com
     *     Optional:
     *         port          => 389
     *         security      => one of SSL or TLS
     *         bind_dn       => cn=admin,dc=example,dc=com (bind will be anonymous if not given)
     *         bind_password => <PASSWORD> (can be plain or a hash)
     *         page_size     => page size (default 1000, set to 0 for no paging)
     *         options       => array of options according to driver specifications
     *
     * @param array           $params Connection parameters
     * @param DriverInterface $driver Driver to use for execution
     *
     * @return Manager
     *
     * @throws \InvalidArgumentException if parameters are incorrect
     */
    public function __construct(array $params, DriverInterface $driver)
    {
        $this->configure($params);
        $this->driver = $driver;
    }

    /**
     * Connects to the Ldap store
     *
     * @return void
     *
     * @throws Toyota\Component\Ldap\Exception\ConnectionException if connection fails
     * @throws Toyota\Component\Ldap\Exception\OptionException if connection configuration fails
     */
    public function connect()
    {
        $this->isBound = false;
        $this->connection = $this->driver->connect(
                $this->configuration['hostname'], 
                $this->configuration['port'], 
                $this->configuration['withSSL'], 
                $this->configuration['withTLS']
        );

        foreach ($this->configuration['options'] as $key => $value) {
            $this->connection->setOption($key, $value);
        }
    }

    /**
     * Binds to the Ldap connection
     *
     * @param string $name     Bind rdn (Default: null)
     * @param string $password Bind password (Default: null)
     *
     * @return void
     *
     * @throws Toyota\Component\Ldap\Exception\BindException if binding fails
     */
    public function bind($name = null, $password = null)
    {
        if (strlen(trim($name)) > 0) {
            $password = (null === $password) ? '' : $password;
            $this->connection->bind($name, $password);
            $this->isBound = true;
            return;
        }

        if ($this->configuration['bind_anonymous']) {
            $this->connection->bind();
            $this->isBound = true;
            return;
        }

        $this->connection->bind(
                $this->configuration['bind_dn'], 
                $this->configuration['bind_password']
        );
        $this->isBound = true;
    }

    /**
     * Complete and validates given parameters with default settings
     *
     * @param array $params Parameters to be cleaned
     *
     * @return array Cleaned parameters
     *
     * @throws \InvalidArgumentException
     */
    protected function configure(array $params)
    {

        $required = array('hostname', 'base_dn');
        $missing = array();
        foreach ($required as $key) {
            if (!array_key_exists($key, $params)) {
{'fraction_non_alphanumeric': 0.1149090909090909, 'fraction_numerical': 0.002181818181818182, 'mean_word_length': 2.287250996015936, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 84, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1150519', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2198, 'n_words': 581}