Passed
Push — master ( d7c3ec...93a960 )
by Aimeos
03:37
created

Base::context()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/**
4
 * @copyright Metaways Infosystems GmbH, 2013
5
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
6
 * @copyright Aimeos (aimeos.org), 2015-2022
7
 * @package Controller
8
 * @subpackage Jobs
9
 */
10
11
12
namespace Aimeos\Controller\Jobs;
13
14
15
/**
16
 * Common methods for Jobs controller classes.
17
 *
18
 * @package Controller
19
 * @subpackage Jobs
20
 */
21
abstract class Base
22
	implements \Aimeos\Macro\Iface
23
{
24
	use \Aimeos\Macro\Macroable;
25
26
27
	private $aimeos;
28
	private $context;
29
30
31
	/**
32
	 * Initializes the object.
33
	 *
34
	 * @param \Aimeos\MShop\Context\Item\Iface $context MShop context object
35
	 * @param \Aimeos\Bootstrap $aimeos \Aimeos\Bootstrap main object
36
	 */
37
	public function __construct( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\Bootstrap $aimeos )
38
	{
39
		$this->context = $context;
40
		$this->aimeos = $aimeos;
41
	}
42
43
44
	/**
45
	 * Catch unknown methods
46
	 *
47
	 * @param string $name Name of the method
48
	 * @param array $param List of method parameter
49
	 * @throws \Aimeos\Controller\Jobs\Exception If method call failed
50
	 */
51
	public function __call( string $name, array $param )
52
	{
53
		throw new \Aimeos\Controller\Jobs\Exception( sprintf( 'Unable to call method "%1$s"', $name ) );
54
	}
55
56
57
	/**
58
	 * Returns the context object.
59
	 *
60
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
61
	 */
62
	protected function context() : \Aimeos\MShop\Context\Item\Iface
63
	{
64
		return $this->context;
65
	}
66
67
68
	/**
69
	 * Returns the \Aimeos\Bootstrap object.
70
	 *
71
	 * @return \Aimeos\Bootstrap \Aimeos\Bootstrap object
72
	 */
73
	protected function getAimeos() : \Aimeos\Bootstrap
74
	{
75
		return $this->aimeos;
76
	}
77
78
79
	/**
80
	 * Returns the value from the list or the default value
81
	 *
82
	 * @param array $list Associative list of key/value pairs
83
	 * @param string $key Key for the value to retrieve
84
	 * @param mixed $default Default value if key isn't found
85
	 * @return mixed Value for the key in the list or the default value
86
	 */
87
	protected function val( array $list, string $key, $default = null )
88
	{
89
		return isset( $list[$key] ) && ( $value = trim( $list[$key] ) ) !== '' ? $value : $default;
90
	}
91
92
93
	/**
94
	 * Sends a mail with the given data to the configured e-mails
95
	 *
96
	 * @param string $subject Subject of the e-mail
97
	 * @param string $body Text body of the e-mail
98
	 * @return \Aimeos\Controller\Jobs\Iface Same object for fluent interface
99
	 */
100
	protected function mail( string $subject, string $body ) : self
101
	{
102
		$config = $this->context->config();
103
104
		/** resource/email/from-name
105
		 * Name of the e-mail sender
106
		 *
107
		 * Should be the company or web site name
108
		 *
109
		 * @param string Sender name
110
		 * @see resource/email/from-email
111
		 */
112
		$name = $config->get( 'resource/email/from-name' );
113
114
		/** resource/email/from-email
115
		 * E-Mail address of the sender
116
		 *
117
		 * Should be the e-mail address of the company or web site
118
		 *
119
		 * @param string E-Mail address
120
		 * @see resource/email/from-name
121
		 */
122
		$email = $config->get( 'resource/email/from-email' );
123
124
		/** controller/jobs/to-email
125
		 * Recipient e-mail address used when sending job e-mails
126
		 *
127
		 * Job controllers can send e-mails when they has finished of if an
128
		 * error occurred. This setting will be used as the recipient e-mail
129
		 * address for these e-mails.
130
		 *
131
		 * @param string E-mail address
132
		 * @since 2020.04
133
		 * @category User
134
		 * @see controller/jobs/from-email
135
		 */
136
		if( ( $to = $config->get( 'controller/jobs/to-email', $email ) ) == null ) {
137
			return $this;
138
		}
139
140
		$message = $this->context->mail()->create();
141
142
		foreach( (array) $to as $addr ) {
143
			$message->to( $addr, $name );
144
		}
145
146
		/** controller/jobs/from-email
147
		 * Sender e-mail address used when sending job e-mails
148
		 *
149
		 * Job controllers can send e-mails when they has finished of if an
150
		 * error occurred. This setting will be used as the sender e-mail
151
		 * address in these e-mails.
152
		 *
153
		 * @param string E-mail address
154
		 * @since 2020.04
155
		 * @category User
156
		 * @see controller/jobs/to-email
157
		 */
158
		if( $from = $config->get( 'controller/jobs/from-email', $email ) ) {
159
			$message->from( $from, $name );
160
		}
161
162
		$message->subject( $subject )->text( $body )->send();
163
164
		return $this;
165
	}
166
}
167