Passed
Push — master ( 98fa9f...39cd3b )
by Aimeos
03:45
created

Standard::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2018-2020
6
 * @package Controller
7
 * @subpackage Common
8
 */
9
10
11
namespace Aimeos\Controller\Common\Subscription\Process\Processor\Email;
12
13
14
/**
15
 * Customer group processor for subscriptions
16
 *
17
 * @package Controller
18
 * @subpackage Common
19
 */
20
class Standard
21
	extends \Aimeos\Controller\Common\Subscription\Process\Processor\Base
22
	implements \Aimeos\Controller\Common\Subscription\Process\Processor\Iface
23
{
24
	/** controller/common/subscription/export/csv/processor/email/name
25
	 * Name of the customer group processor implementation
26
	 *
27
	 * Use "Myname" if your class is named "\Aimeos\Controller\Common\Subscription\Process\Processor\Email\Myname".
28
	 * The name is case-sensitive and you should avoid camel case names like "MyName".
29
	 *
30
	 * @param string Last part of the processor class name
31
	 * @since 2018.04
32
	 * @category Developer
33
	 */
34
35
36
	/**
37
	 * Executed after the subscription renewal
38
	 *
39
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item
40
	 * @param \Aimeos\MShop\Order\Item\Iface $order Order invoice item
41
	 */
42
	public function renewAfter( \Aimeos\MShop\Subscription\Item\Iface $subscription, \Aimeos\MShop\Order\Item\Iface $order )
43
	{
44
		if( $subscription->getReason() === \Aimeos\MShop\Subscription\Item\Iface::REASON_PAYMENT ) {
45
			$this->process( $subscription );
46
		}
47
	}
48
49
50
	/**
51
	 * Processes the end of the subscription
52
	 *
53
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item
54
	 */
55
	public function end( \Aimeos\MShop\Subscription\Item\Iface $subscription )
56
	{
57
		$this->process( $subscription );
58
	}
59
60
61
	/**
62
	 * Returns the product notification e-mail client
63
	 *
64
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
65
	 * @return \Aimeos\Client\Html\Iface Product notification e-mail client
66
	 */
67
	protected function getClient( \Aimeos\MShop\Context\Item\Iface $context )
68
	{
69
		if( !isset( $this->client ) ) {
70
			$this->client = \Aimeos\Client\Html\Email\Subscription\Factory::create( $context );
0 ignored issues
show
Bug Best Practice introduced by
The property client does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
71
		}
72
73
		return $this->client;
74
	}
75
76
77
	/**
78
	 * Sends e-mails for the given subscription
79
	 *
80
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item object
81
	 */
82
	protected function process( \Aimeos\MShop\Subscription\Item\Iface $subscription )
83
	{
84
		$context = $this->getContext();
85
86
		$manager = \Aimeos\MShop::create( $context, 'order/base' );
87
		$baseItem = $manager->getItem( $subscription->getOrderBaseId(), ['order/base/address', 'order/base/product'] );
0 ignored issues
show
Bug introduced by
It seems like $subscription->getOrderBaseId() can also be of type null; however, parameter $id of Aimeos\MShop\Common\Manager\Iface::getItem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

87
		$baseItem = $manager->getItem( /** @scrutinizer ignore-type */ $subscription->getOrderBaseId(), ['order/base/address', 'order/base/product'] );
Loading history...
88
89
		$addrItem = $baseItem->getAddress( \Aimeos\MShop\Order\Item\Base\Address\Base::TYPE_PAYMENT, 0 );
90
91
		foreach( $baseItem->getProducts() as $orderProduct )
92
		{
93
			if( $orderProduct->getId() == $subscription->getOrderProductId() ) {
94
				$this->sendMail( $context, $subscription, $addrItem, $orderProduct );
95
			}
96
		}
97
	}
98
99
100
	/**
101
	 * Sends the subscription e-mail for the given customer address and products
102
	 *
103
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context item object
104
	 * @param \Aimeos\MShop\Subscription\Item\Iface $subscription Subscription item
105
	 * @param \Aimeos\MShop\Common\Item\Address\Iface $address Payment address of the customer
106
	 * @param \Aimeos\MShop\Order\Item\Base\Product\Iface $product Subscription product the notification should be sent for
107
	 */
108
	protected function sendMail( \Aimeos\MShop\Context\Item\Iface $context, \Aimeos\MShop\Subscription\Item\Iface $subscription,
109
		\Aimeos\MShop\Common\Item\Address\Iface $address, \Aimeos\MShop\Order\Item\Base\Product\Iface $product )
110
	{
111
		$view = $context->getView();
112
		$view->extAddressItem = $address;
113
		$view->extOrderProductItem = $product;
114
		$view->extSubscriptionItem = $subscription;
115
116
		$params = [
117
			'locale' => $context->getLocale()->getLanguageId(),
118
			'site' => $context->getLocale()->getSiteItem()->getCode(),
119
		];
120
121
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
122
		$view->addHelper( 'param', $helper );
123
124
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $context->getI18n( $address->getLanguageId() ) );
125
		$view->addHelper( 'translate', $helper );
126
127
		$mailer = $context->getMail();
128
		$message = $mailer->createMessage();
129
130
		$helper = new \Aimeos\MW\View\Helper\Mail\Standard( $view, $message );
131
		$view->addHelper( 'mail', $helper );
132
133
		$client = $this->getClient( $context );
134
		$client->setView( $view );
135
		$client->getHeader();
136
		$client->getBody();
137
138
		$mailer->send( $message );
139
	}
140
}
141