1<?php
2/**
3 * This file is part of the FreeDSx LDAP package.
4 *
5 * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace FreeDSx\Ldap\Protocol\Factory;
12
13use FreeDSx\Ldap\Exception\OperationException;
14use FreeDSx\Ldap\Operation\Request\AnonBindRequest;
15use FreeDSx\Ldap\Operation\Request\RequestInterface;
16use FreeDSx\Ldap\Operation\Request\SimpleBindRequest;
17use FreeDSx\Ldap\Operation\ResultCode;
18use FreeDSx\Ldap\Protocol\ServerProtocolHandler\BindHandlerInterface;
19use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerAnonBindHandler;
20use FreeDSx\Ldap\Protocol\ServerProtocolHandler\ServerBindHandler;
21
22/**
23 * Determines the correct bind handler for the request.
24 *
25 * @author Chad Sikorra <Chad.Sikorra@gmail.com>
26 */
27class ServerBindHandlerFactory
28{
29    /**
30     * Get the bind handler specific to the request.
31     *
32     * @throws OperationException
33     */
34    public function get(RequestInterface $request): BindHandlerInterface
35    {
36        if ($request instanceof SimpleBindRequest) {
37            return new ServerBindHandler();
38        } elseif ($request instanceof AnonBindRequest) {
39            return new ServerAnonBindHandler();
40        } else {
41            throw new OperationException(
42                'The authentication type requested is not supported.',
43                ResultCode::AUTH_METHOD_UNSUPPORTED
44            );
45        }
46    }
47}
48