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