Exceptions::init()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 37
rs 8.6506
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022-2025
6
 * @package Client
7
 * @subpackage Html
8
 */
9
10
11
namespace Aimeos\Client\Html\Common\Decorator;
12
13
14
/**
15
 * Provides exception handling for HTML clients.
16
 *
17
 * @package Client
18
 * @subpackage Html
19
 */
20
class Exceptions extends Base implements Iface
21
{
22
	/**
23
	 * Returns the HTML code for insertion into the body.
24
	 *
25
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
26
	 * @return string HTML code
27
	 */
28
	public function body( string $uid = '' ) : string
29
	{
30
		$output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
31
		$view = $this->view();
32
		$context = $this->context();
33
34
		try
35
		{
36
			$output = $this->client()->body( $uid );
37
		}
38
		catch( \Aimeos\Client\Html\Exception $e )
39
		{
40
			$error = [$context->translate( 'client', $e->getMessage() )];
41
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
42
		}
43
		catch( \Aimeos\Controller\Frontend\Exception $e )
44
		{
45
			if( $e->getCode() >= 400 ) {
46
				throw $e;
47
			}
48
49
			$error = [$context->translate( 'controller/frontend', $e->getMessage() )];
50
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
51
		}
52
		catch( \Aimeos\MShop\Exception $e )
53
		{
54
			if( $e->getCode() >= 400 ) {
55
				throw $e;
56
			}
57
58
			$error = [$context->translate( 'mshop', $e->getMessage() )];
59
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
60
		}
61
		catch( \Exception $e )
62
		{
63
			$error = [$context->translate( 'client', 'A non-recoverable error occured' )];
64
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
65
			$this->logException( $e );
66
		}
67
68
		return $view->render( 'error' ) . $output;
69
	}
70
71
72
	/**
73
	 * Returns the HTML string for insertion into the header.
74
	 *
75
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
76
	 * @return string|null String including HTML tags for the header on error
77
	 */
78
	public function header( string $uid = '' ) : ?string
79
	{
80
		try
81
		{
82
			return $this->client()->header( $uid );
83
		}
84
		catch( \Exception $e )
85
		{
86
			if( $e->getCode() >= 400 ) {
87
				throw $e;
88
			}
89
90
			$this->logException( $e, \Aimeos\Base\Logger\Iface::NOTICE );
91
		}
92
93
		return null;
94
	}
95
96
97
	/**
98
	 * Processes the input, e.g. store given values.
99
	 */
100
	public function init()
101
	{
102
		$view = $this->view();
103
		$context = $this->context();
104
105
		try
106
		{
107
			$this->client()->init();
108
		}
109
		catch( \Aimeos\Client\Html\Exception $e )
110
		{
111
			$error = array( $context->translate( 'client', $e->getMessage() ) );
112
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
113
		}
114
		catch( \Aimeos\Controller\Frontend\Exception $e )
115
		{
116
			if( $e->getCode() >= 400 ) {
117
				throw $e;
118
			}
119
120
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
121
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
122
		}
123
		catch( \Aimeos\MShop\Exception $e )
124
		{
125
			if( $e->getCode() >= 400 ) {
126
				throw $e;
127
			}
128
129
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
130
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
131
		}
132
		catch( \Exception $e )
133
		{
134
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
135
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
136
			$this->logException( $e );
137
		}
138
	}
139
}
140