Passed
Push — master ( fd781a...b1eaa4 )
by Aimeos
08:26
created

Exceptions::header()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2022
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
			$error = [$context->translate( 'controller/frontend', $e->getMessage() )];
46
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
47
		}
48
		catch( \Aimeos\MShop\Exception $e )
49
		{
50
			$error = [$context->translate( 'mshop', $e->getMessage() )];
51
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
52
		}
53
		catch( \Exception $e )
54
		{
55
			$error = [$context->translate( 'client', 'A non-recoverable error occured' )];
56
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
57
			$this->logException( $e );
58
		}
59
60
		return $view->render( 'error' ) . $output;
61
	}
62
63
64
	/**
65
	 * Returns the HTML string for insertion into the header.
66
	 *
67
	 * @param string $uid Unique identifier for the output if the content is placed more than once on the same page
68
	 * @return string|null String including HTML tags for the header on error
69
	 */
70
	public function header( string $uid = '' ) : ?string
71
	{
72
		try {
73
			return $this->client()->header( $uid );
74
		} catch( \Exception $e ) {
75
			$this->logException( $e );
76
		}
77
78
		return null;
79
	}
80
81
82
	/**
83
	 * Processes the input, e.g. store given values.
84
	 */
85
	public function init()
86
	{
87
		$view = $this->view();
88
		$context = $this->context();
89
90
		try
91
		{
92
			$this->client()->init();
93
		}
94
		catch( \Aimeos\Client\Html\Exception $e )
95
		{
96
			$error = array( $context->translate( 'client', $e->getMessage() ) );
97
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
98
		}
99
		catch( \Aimeos\Controller\Frontend\Exception $e )
100
		{
101
			$error = array( $context->translate( 'controller/frontend', $e->getMessage() ) );
102
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
103
		}
104
		catch( \Aimeos\MShop\Exception $e )
105
		{
106
			$error = array( $context->translate( 'mshop', $e->getMessage() ) );
107
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
108
		}
109
		catch( \Exception $e )
110
		{
111
			$error = array( $context->translate( 'client', 'A non-recoverable error occured' ) );
112
			$view->errors = array_merge( $view->get( 'errors', [] ), $error );
113
			$this->logException( $e );
114
		}
115
	}
116
}
117