| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports. |
| 5 | * Copyright (C) 2022-2023 Aleksey Andreev (liuch) |
| 6 | * |
| 7 | * Available at: |
| 8 | * https://github.com/liuch/dmarc-srg |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify it |
| 11 | * under the terms of the GNU General Public License as published by the Free |
| 12 | * Software Foundation, either version 3 of the License. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 17 | * more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along with |
| 20 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | * |
| 22 | * ========================= |
| 23 | * |
| 24 | * This file contains DatabaseExceptionFactory class |
| 25 | * |
| 26 | * @category Common |
| 27 | * @package DmarcSrg |
| 28 | * @author Aleksey Andreev (liuch) |
| 29 | * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3 |
| 30 | */ |
| 31 | |
| 32 | namespace Liuch\DmarcSrg\Exception; |
| 33 | |
| 34 | /** |
| 35 | * Factory class for DatabaseException |
| 36 | */ |
| 37 | class DatabaseExceptionFactory |
| 38 | { |
| 39 | /** |
| 40 | * Creates a DatabaseException instance with an appropriate message based on the passed class's name and error code. |
| 41 | * |
| 42 | * @param \Exception $origin The original exception |
| 43 | * |
| 44 | * @return DatabaseException |
| 45 | */ |
| 46 | public static function fromException(\Throwable $origin) |
| 47 | { |
| 48 | $msg = null; |
| 49 | if (get_class($origin) === 'PDOException') { |
| 50 | switch ($origin->getCode()) { |
| 51 | case 1044: |
| 52 | case 1045: |
| 53 | $msg = 'Database access denied'; |
| 54 | break; |
| 55 | case 2002: |
| 56 | case 2006: |
| 57 | $msg = 'Database connection error'; |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | if (!$msg) { |
| 62 | $msg = 'Database error'; |
| 63 | } |
| 64 | return new DatabaseException($msg, -1, $origin); |
| 65 | } |
| 66 | } |