Completed
Push — master ( d5a5f8...a97add )
by Aimeos
02:53
created

CustomerAddEzpublishTestData::getEzContext()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 63
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 63
rs 8.8945
cc 4
eloc 37
nc 2
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 */
7
8
9
namespace Aimeos\MW\Setup\Task;
10
11
12
/**
13
 * Adds ezPublish customer test data
14
 */
15
class CustomerAddEzpublishTestData extends \Aimeos\MW\Setup\Task\CustomerAddTestData
16
{
17
	/**
18
	 * Returns the list of task names which this task depends on.
19
	 *
20
	 * @return string[] List of task names
21
	 */
22
	public function getPreDependencies()
23
	{
24
		return array( 'TablesCreateEzpublish', 'EzuserAddAddress', 'MShopSetLocale', 'MediaAddTestData' );
25
	}
26
27
28
	/**
29
	 * Adds attribute test data.
30
	 */
31
	public function migrate()
32
	{
33
		$iface = '\\Aimeos\\MShop\\Context\\Item\\Iface';
34
		if( !( $this->additional instanceof $iface ) ) {
35
			throw new \Aimeos\MW\Setup\Exception( sprintf( 'Additionally provided object is not of type "%1$s"', $iface ) );
36
		}
37
38
		$context = $this->getEzContext( $this->additional );
39
40
		$this->msg( 'Adding ezPublish customer test data', 0 );
41
42
		$parentIds = array();
43
		$ds = DIRECTORY_SEPARATOR;
44
		$context->setEditor( 'ai-ezpublish:unittest' );
45
		$path = __DIR__ . $ds . 'data' . $ds . 'customer.php';
46
47
		if( ( $testdata = include( $path ) ) === false ){
48
			throw new \Aimeos\MShop\Exception( sprintf( 'No file "%1$s" found for customer domain', $path ) );
49
		}
50
51
52
		$customerManager = \Aimeos\MShop\Customer\Manager\Factory::createManager( $context, 'Ezpublish' );
53
		$customerAddressManager = $customerManager->getSubManager( 'address', 'Ezpublish' );
54
55
		$search = $customerManager->createSearch();
56
		$search->setConditions( $search->compare( '==', 'customer.code', array( 'UTC001', 'UTC002', 'UTC003' ) ) );
57
		$items = $customerManager->searchItems( $search );
58
59
		$this->conn->begin();
60
61
		$customerManager->deleteItems( array_keys( $items ) );
62
		$parentIds = $this->addCustomerData( $testdata, $customerManager, $customerAddressManager->createItem() );
63
		$this->addCustomerAddressData( $testdata, $customerAddressManager, $parentIds );
64
65
		$this->conn->commit();
66
67
68
		$this->status( 'done' );
69
	}
70
71
72
	/**
73
	 * Returns the Ezpublish context
74
	 *
75
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
76
	 * @return \Aimeos\MShop\Context\Item\Ezpublish Ezpublish context object
77
	 */
78
	protected function getEzContext( \Aimeos\MShop\Context\Item\Iface $context )
79
	{
80
		$class = '\Aimeos\MShop\Context\Item\Ezpublish';
81
		if( is_a( $context, $class ) ) {
82
			return $context;
83
		}
84
85
		$ezContext = new \Aimeos\MShop\Context\Item\Ezpublish();
86
		$ezContext->setDatabaseManager( clone $context->getDatabaseManager() );
87
		$ezContext->setSession( clone $context->getSession() );
88
		$ezContext->setLogger( clone $context->getLogger() );
89
		$ezContext->setLocale( clone $context->getLocale() );
90
		$ezContext->setConfig( clone $context->getConfig() );
91
		$ezContext->setCache( clone $context->getCache() );
92
93
94
		$ezContext->setEzUser( function( $code, $email, $password ) use ( $context ) {
95
96
			$id = mt_rand( -0x7fffffff,-1 );
97
			$dbm = $context->getDatabaseManager();
98
			$dbconf = $context->getConfig()->get( 'resource/db-customer' );
99
			$dbname = ( $dbconf ? 'db-customer' : 'db' );
100
			$conn = $dbm->acquire( $dbname );
101
102
			try
103
			{
104
				$stmt = $conn->create( 'INSERT INTO "ezuser" (
105
					"contentobject_id", "email", "login", "login_normalized", "password_hash", "password_hash_type"
106
				) VALUES (
107
					?, ?, ?, ?, ?, 5
108
				)' );
109
110
				$stmt->bind( 1, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
111
				$stmt->bind( 2, $email );
112
				$stmt->bind( 3, $code );
113
				$stmt->bind( 4, $code );
114
				$stmt->bind( 5, $password );
115
116
				$stmt->execute()->finish();
117
118
				$sql = 'INSERT INTO "ezuser_setting" ( "user_id", "is_enabled", "max_login" ) VALUES ( ?, ?, ? )';
119
				$stmt = $conn->create( $sql );
120
121
				$stmt->bind( 1, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
122
				$stmt->bind( 2, 0 );
123
				$stmt->bind( 3, 10 );
124
125
				$stmt->execute()->finish();
126
127
				$dbm->release( $conn, $dbname );
128
			}
129
			catch( \Exception $e )
130
			{
131
				$dbm->release( $conn, $dbname );
132
				throw $e;
133
			}
134
135
			return $id;
136
		} );
137
138
139
		return $ezContext;
140
	}
141
}
142