Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Exceptions/ResponseException.php

raw symbols SCIP
1 <?php
2 /*
3 * File: ResponseException.php
4 * Category: Exception
5 * Author: M. Goldenbaum
6 * Created: 19.01.17 22:21
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13 namespace Webklex\PHPIMAP\Exceptions;
14
15 use \Exception;
16 use Webklex\PHPIMAP\Connection\Protocols\Response;
17
18 /**
19 * Class ResponseException
20 *
21 * @package Webklex\PHPIMAP\Exceptions
22 */
23 class ResponseException extends Exception {
24
25 /**
26 * Make a new ResponseException instance
27 * @param Response $response
28 * @param false|boolean $debug
29 * @param Exception|null $exception
30 *
31 * @return ResponseException
32 */
33 public static function make(Response $response, bool $debug = false, ?Exception $exception = null): ResponseException {
34 $message = "Command failed to process:\n";
35 $message .= "Causes:\n";
36
37 foreach($response->getErrors() as $error) {
38 $message .= "\t- $error\n";
39 }
40
41 if(!$response->data()) {
42 $message .= "\t- Empty response\n";
43 }
44
45 if ($debug) {
46 $message .= self::debug_message($response);
47 }
48
49 foreach($response->getStack() as $_response) {
50 $exception = self::make($_response, $debug, $exception);
51 }
52
53 return new self($message."Error occurred", 0, $exception);
54 }
55
56 /**
57 * Generate a debug message containing all commands send and responses received
58 * @param Response $response
59 *
60 * @return string
61 */
62 protected static function debug_message(Response $response): string {
63 $commands = $response->getCommands();
64 $message = "Commands send:\n";
65 if ($commands) {
66 foreach($commands as $command) {
67 $message .= "\t".str_replace("\r\n", "\\r\\n", $command)."\n";
68 }
69 }else{
70 $message .= "\tNo command send!\n";
71 }
72
73 $responses = $response->getResponse();
74 $message .= "Responses received:\n";
75 if ($responses) {
76 foreach($responses as $_response) {
77 if (is_array($_response)) {
78 foreach($_response as $value) {
79 $message .= "\t".str_replace("\r\n", "\\r\\n", "$value")."\n";
80 }
81 }else{
82 $message .= "\t".str_replace("\r\n", "\\r\\n", "$_response")."\n";
83 }
84 }
85 }else{
86 $message .= "\tNo responses received!\n";
87 }
88
89 return $message;
90 }
91 }