Passed
Push — master ( 7c3548...f0ec78 )
by Aimeos
12:31
created

Exceptions::header()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 26
rs 9.6111
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
		$output = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $output is dead and can be removed.
Loading history...
81
82
		try
83
		{
84
			$output = $this->client()->header( $uid );
85
		}
86
		catch( \Aimeos\Client\Html\Exception $e )
87
		{
88
			// no output in header
89
		}
90
		catch( \Aimeos\Controller\Frontend\Exception $e )
91
		{
92
			// no output in header
93
		}
94
		catch( \Aimeos\MShop\Exception $e )
95
		{
96
			// no output in header
97
		}
98
		catch( \Exception $e )
99
		{
100
			$this->logException( $e );
101
		}
102
103
		return $output;
104
	}
105
106
107
	/**
108
	 * Processes the input, e.g. store given values.
109
	 */
110
	public function init()
111
	{
112
		$view = $this->view();
113
		$context = $this->context();
114
115
		try
116
		{
117
			$this->client()->init();
118
		}
119
		catch( \Aimeos\Client\Html\Exception $e )
120
		{
121
			$error = array( $context->translate( 'client', $e->getMessage() ) );
122
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
123
		}
124
		catch( \Aimeos\Controller\Frontend\Exception $e )
125
		{
126
			if( $e->getCode() >= 400 ) {
127
				throw $e;
128
			}
129
130
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
131
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
132
		}
133
		catch( \Aimeos\MShop\Exception $e )
134
		{
135
			if( $e->getCode() >= 400 ) {
136
				throw $e;
137
			}
138
139
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
140
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
141
		}
142
		catch( \Exception $e )
143
		{
144
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
145
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
146
			$this->logException( $e );
147
		}
148
	}
149
}
150