Passed
Push — master ( c9c1dd...eb7532 )
by Aimeos
18:11
created

Email::push()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2021-2023
6
 * @package MShop
7
 * @subpackage Service
8
 */
9
10
11
namespace Aimeos\MShop\Service\Provider\Delivery;
12
13
14
/**
15
 * Email delivery provider implementation
16
 *
17
 * @package MShop
18
 * @subpackage Service
19
 */
20
class Email
21
	extends \Aimeos\MShop\Service\Provider\Delivery\Base
22
	implements \Aimeos\MShop\Service\Provider\Delivery\Iface
23
{
24
	private $beConfig = [
25
		'email.from' => [
26
			'code' => 'email.from',
27
			'internalcode' => 'email.from',
28
			'label' => 'Sender e-mail address',
29
			'type' => 'string',
30
			'internaltype' => 'string',
31
			'default' => '',
32
			'required' => true,
33
		],
34
		'email.to' => [
35
			'code' => 'email.to',
36
			'internalcode' => 'email.to',
37
			'label' => 'Recipient e-mail address',
38
			'type' => 'string',
39
			'internaltype' => '',
40
			'default' => '',
41
			'required' => true,
42
		],
43
		'email.subject' => [
44
			'code' => 'email.subject',
45
			'internalcode' => 'email.subject',
46
			'label' => 'E-mail subject',
47
			'type' => 'string',
48
			'internaltype' => 'string',
49
			'default' => '',
50
			'required' => false,
51
		],
52
		'email.template' => [
53
			'code' => 'email.template',
54
			'internalcode' => 'email.template',
55
			'label' => 'E-mail template',
56
			'type' => 'string',
57
			'internaltype' => 'string',
58
			'default' => 'service/provider/delivery/email-body',
59
			'required' => true,
60
		],
61
		'email.order-template' => [
62
			'code' => 'email.order-template',
63
			'internalcode' => 'email.order-template',
64
			'label' => 'Order template',
65
			'type' => 'string',
66
			'internaltype' => 'string',
67
			'default' => 'service/provider/delivery/email-order',
68
			'required' => true,
69
		],
70
	];
71
72
73
	/**
74
	 * Checks the backend configuration attributes for validity
75
	 *
76
	 * @param array $attributes Attributes added by the shop owner in the administraton interface
77
	 * @return array An array with the attribute keys as key and an error message as values for all attributes that are
78
	 * 	known by the provider but aren't valid
79
	 */
80
	public function checkConfigBE( array $attributes ) : array
81
	{
82
		$errors = parent::checkConfigBE( $attributes );
83
84
		return array_merge( $errors, $this->checkConfig( $this->beConfig, $attributes ) );
85
	}
86
87
88
	/**
89
	 * Returns the configuration attribute definitions of the provider to generate a list of available fields and
90
	 * rules for the value of each field in the administration interface.
91
	 *
92
	 * @return array List of attribute definitions implementing \Aimeos\Base\Critera\Attribute\Iface
93
	 */
94
	public function getConfigBE() : array
95
	{
96
		return $this->getConfigItems( $this->beConfig );
97
	}
98
99
100
	/**
101
	 * Sends the email with several orders and updates the delivery status
102
	 *
103
	 * @param \Aimeos\MShop\Order\Item\Iface[] $orders List of order invoice objects
104
	 * @return \Aimeos\MShop\Order\Item\Iface[] Updated order items
105
	 */
106
	public function push( iterable $orders ) : \Aimeos\Map
107
	{
108
		return $this->send( $orders )->setStatusDelivery( \Aimeos\MShop\Order\Item\Base::STAT_PROGRESS );
109
	}
110
111
112
	/**
113
	 * Returns the content for the e-mail body
114
	 *
115
	 * @param iterable $orderItems List of order items to export
116
	 */
117
	protected function getEmailContent( iterable $orderItems )
118
	{
119
		$template = $this->getConfigValue( 'email.template', 'service/provider/delivery/email-body' );
120
121
		return $this->context()->view()
122
			->assign( ['orderItems' => $orderItems] )
123
			->render( $template );
124
	}
125
126
127
	/**
128
	 * Returns the order content for the e-mail attachment
129
	 *
130
	 * @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items to export
131
	 */
132
	protected function getOrderContent( iterable $orderItems )
133
	{
134
		$template = $this->getConfigValue( 'email.order-template', 'service/provider/delivery/email-order' );
135
136
		return $this->context()->view()
137
			->assign( ['orderItems' => $orderItems] )
138
			->render( $template );
139
	}
140
141
142
	/**
143
	 * Sends an e-mail for the given orders
144
	 *
145
	 * @param \Aimeos\MShop\Order\Item\Iface[] $orderItems List of order items to export
146
	 * @return \Aimeos\Map List of order items
147
	 */
148
	protected function send( iterable $orderItems ) : \Aimeos\Map
149
	{
150
		$this->context()->mail()->create()
151
			->to( (string) $this->getConfigValue( 'email.to' ) )
152
			->from( (string) $this->getConfigValue( 'email.from' ) )
153
			->subject( (string) $this->getConfigValue( 'email.subject', 'New orders' ) )
154
			->attach( $this->getOrderContent( $orderItems ), 'orders.csv', 'text/plain' )
155
			->text( $this->getEmailContent( $orderItems ) )
156
			->send();
157
158
		return map( $orderItems );
159
	}
160
}
161