Passed
Push — master ( a59ecb...8091ff )
by Aimeos
23:24 queued 18:06
created

Session::getSession()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 25
rs 9.7998
cc 3
nc 5
nop 1
1
<?php
2
3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2023
6
 * @package MShop
7
 * @subpackage Order
8
 */
9
10
11
namespace Aimeos\MShop\Order\Manager;
12
13
14
/**
15
 * Session trait for order managers
16
 *
17
 * @package MShop
18
 * @subpackage Order
19
 */
20
trait Session
21
{
22
	/**
23
	 * Returns the current basket of the customer.
24
	 *
25
	 * @param string $type Basket type if a customer can have more than one basket
26
	 * @return \Aimeos\MShop\Order\Item\Iface Shopping basket
27
	 */
28
	public function getSession( string $type = 'default' ) : \Aimeos\MShop\Order\Item\Iface
29
	{
30
		$context = $this->context();
0 ignored issues
show
Bug introduced by
It seems like context() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

30
		/** @scrutinizer ignore-call */ 
31
  $context = $this->context();
Loading history...
31
		$token = $context->token();
32
		$locale = $context->locale();
33
		$currency = $locale->getCurrencyId();
34
		$language = $locale->getLanguageId();
35
		$sitecode = $locale->getSiteItem()->getCode();
36
37
		$key = $token . '-' . $sitecode . '-' . $language . '-' . $currency . '-' . $type;
38
39
		try
40
		{
41
			if( ( $order = \Aimeos\MShop::create( $context, 'order/basket' )->get( $key )->getItem() ) === null ) {
42
				return $this->object()->create();
0 ignored issues
show
Bug introduced by
It seems like object() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

42
				return $this->/** @scrutinizer ignore-call */ object()->create();
Loading history...
43
			}
44
45
			\Aimeos\MShop::create( $context, 'plugin' )->register( $order, 'order' );
46
		}
47
		catch( \Exception $e )
48
		{
49
			return $this->object()->create();
50
		}
51
52
		return $order;
53
	}
54
55
56
	/**
57
	 * Saves the current shopping basket of the customer.
58
	 *
59
	 * @param \Aimeos\MShop\Order\Item\Iface $order Shopping basket
60
	 * @param string $type Order type if a customer can have more than one order at once
61
	 * @return \Aimeos\MShop\Order\Manager\Iface Manager object for chaining method calls
62
	 */
63
	public function setSession( \Aimeos\MShop\Order\Item\Iface $order, string $type = 'default' ) : \Aimeos\MShop\Order\Manager\Iface
64
	{
65
		$context = $this->context();
66
		$token = $context->token();
67
		$locale = $context->locale();
68
		$currency = $locale->getCurrencyId();
69
		$language = $locale->getLanguageId();
70
		$sitecode = $locale->getSiteItem()->getCode();
71
72
		$key = $token . '-' . $sitecode . '-' . $language . '-' . $currency . '-' . strval( $type );
73
74
		$session = $context->session();
75
76
		$list = $session->get( 'aimeos/basket/list', [] );
77
		$list[$key] = $key;
78
79
		$session->set( 'aimeos/basket/list', $list );
80
81
		$manager = \Aimeos\MShop::create( $context, 'order/basket' );
82
		$manager->save( $manager->create()->setId( $key )->setCustomerId( $context->user() )->setItem( clone $order ) );
83
84
		return $this;
85
	}
86
}
87