xref: /plugin/pureldap/vendor/freedsx/ldap/src/FreeDSx/Ldap/Exception/OperationException.php (revision dad993c57a70866aa1db59c43f043769c2eb7ed0)
10b3fd2d3SAndreas Gohr<?php
2*dad993c5SAndreas Gohr
30b3fd2d3SAndreas Gohr/**
40b3fd2d3SAndreas Gohr * This file is part of the FreeDSx LDAP package.
50b3fd2d3SAndreas Gohr *
60b3fd2d3SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com>
70b3fd2d3SAndreas Gohr *
80b3fd2d3SAndreas Gohr * For the full copyright and license information, please view the LICENSE
90b3fd2d3SAndreas Gohr * file that was distributed with this source code.
100b3fd2d3SAndreas Gohr */
110b3fd2d3SAndreas Gohr
120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap\Exception;
130b3fd2d3SAndreas Gohr
14*dad993c5SAndreas Gohruse Exception;
150b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Operation\ResultCode;
16*dad993c5SAndreas Gohruse Throwable;
170b3fd2d3SAndreas Gohr
180b3fd2d3SAndreas Gohr/**
190b3fd2d3SAndreas Gohr * Used in client-side requests to indicate generic issues with non-success request responses for operations. Used to
200b3fd2d3SAndreas Gohr * indicate an error during server-side operation processing. The resulting message and code is used in the
210b3fd2d3SAndreas Gohr * LDAP result sent back to the client (when thrown from the request handler).
220b3fd2d3SAndreas Gohr *
230b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com>
240b3fd2d3SAndreas Gohr */
25*dad993c5SAndreas Gohrclass OperationException extends Exception
260b3fd2d3SAndreas Gohr{
27*dad993c5SAndreas Gohr    public function __construct(
28*dad993c5SAndreas Gohr        string $message = '',
29*dad993c5SAndreas Gohr        int $code = ResultCode::OPERATIONS_ERROR,
30*dad993c5SAndreas Gohr        Throwable $previous = null
31*dad993c5SAndreas Gohr    ) {
32*dad993c5SAndreas Gohr        $message = empty($message)
33*dad993c5SAndreas Gohr            ? $this->generateMessage($code)
34*dad993c5SAndreas Gohr            : $message;
35*dad993c5SAndreas Gohr
36*dad993c5SAndreas Gohr        parent::__construct(
37*dad993c5SAndreas Gohr            $message,
38*dad993c5SAndreas Gohr            $code,
39*dad993c5SAndreas Gohr            $previous
40*dad993c5SAndreas Gohr        );
41*dad993c5SAndreas Gohr    }
42*dad993c5SAndreas Gohr
43*dad993c5SAndreas Gohr    /**
44*dad993c5SAndreas Gohr     * Get the LDAP result code as a short string (as defined in the LDAP RFC).
45*dad993c5SAndreas Gohr     *
46*dad993c5SAndreas Gohr     * @return string|null
47*dad993c5SAndreas Gohr     */
48*dad993c5SAndreas Gohr    public function getCodeShort(): ?string
490b3fd2d3SAndreas Gohr    {
50*dad993c5SAndreas Gohr        return ResultCode::MEANING_SHORT[$this->getCode()] ?? null;
51*dad993c5SAndreas Gohr    }
52*dad993c5SAndreas Gohr
53*dad993c5SAndreas Gohr    /**
54*dad993c5SAndreas Gohr     * Get the LDAP result code meaning description (as defined in the LDAP RFC).
55*dad993c5SAndreas Gohr     *
56*dad993c5SAndreas Gohr     * @return string|null
57*dad993c5SAndreas Gohr     */
58*dad993c5SAndreas Gohr    public function getCodeDescription(): ?string
59*dad993c5SAndreas Gohr    {
60*dad993c5SAndreas Gohr        return ResultCode::MEANING_DESCRIPTION[$this->getCode()] ?? null;
61*dad993c5SAndreas Gohr    }
62*dad993c5SAndreas Gohr
63*dad993c5SAndreas Gohr    private function generateMessage(int $resultCode): string
64*dad993c5SAndreas Gohr    {
65*dad993c5SAndreas Gohr        $message = sprintf('The result code %d was thrown', $resultCode);
66*dad993c5SAndreas Gohr
67*dad993c5SAndreas Gohr        if (isset(ResultCode::MEANING_SHORT[$resultCode])) {
68*dad993c5SAndreas Gohr            $message .= sprintf(' (%s)', ResultCode::MEANING_SHORT[$resultCode]);
69*dad993c5SAndreas Gohr        }
70*dad993c5SAndreas Gohr        if (isset(ResultCode::MEANING_DESCRIPTION[$resultCode])) {
71*dad993c5SAndreas Gohr            $message .= '. ' . ResultCode::MEANING_DESCRIPTION[$resultCode];
72*dad993c5SAndreas Gohr        }
73*dad993c5SAndreas Gohr
74*dad993c5SAndreas Gohr        return $message;
750b3fd2d3SAndreas Gohr    }
760b3fd2d3SAndreas Gohr}
77