Completed
Push — master ( a97add...702095 )
by Aimeos
02:48
created

CustomerAddEzpublishTestData   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 11
c 3
b 0
f 1
lcom 0
cbo 1
dl 0
loc 133
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreDependencies() 0 4 1
B migrate() 0 39 3
C getEzContext() 0 69 7
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
		$ctx = new \Aimeos\MShop\Context\Item\Ezpublish();
86
		$ctx->setDatabaseManager( clone $context->getDatabaseManager() );
87
		$ctx->setSession( clone $context->getSession() );
88
		$ctx->setLogger( clone $context->getLogger() );
89
		$ctx->setLocale( clone $context->getLocale() );
90
		$ctx->setConfig( clone $context->getConfig() );
91
		$ctx->setCache( clone $context->getCache() );
92
93
94
		$ctx->setEzUser( function( $id, $code, $email, $password, $status ) use ( $ctx ) {
95
96
			$dbm = $ctx->getDatabaseManager();
97
			$dbname = ( $ctx->getConfig()->get( 'resource/db-customer' ) ? 'db-customer' : 'db' );
98
			$conn = $dbm->acquire( $dbname );
99
100
			try
101
			{
102
				$contentid = ( $id === null ? mt_rand( -0x7fffffff,-1 ) : $id );
103
104
				if( $id === null ) {
105
					$sql = 'INSERT INTO "ezuser" ( "email", "login", "login_normalized", "password_hash", "password_hash_type", "contentobject_id" ) VALUES ( ?, ?, ?, ?, 5, ? )';
106
				} else {
107
					$sql = 'UPDATE "ezuser" SET "email" = ?, "login" = ?, "login_normalized" = ?, "password_hash" = ?, "password_hash_type" = 5 WHERE "contentobject_id" = ?';
108
				}
109
110
				$stmt = $conn->create( $sql );
111
				$stmt->bind( 1, $email );
112
				$stmt->bind( 2, $code );
113
				$stmt->bind( 3, $code );
114
				$stmt->bind( 4, $password );
115
				$stmt->bind( 5, $contentid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
116
117
				$stmt->execute()->finish();
118
119
120
				if( $id === null ) {
121
					$sql = 'INSERT INTO "ezuser_setting" ( "is_enabled", "user_id" ) VALUES ( ?, ? )';
122
				} else {
123
					$sql = 'UPDATE "ezuser_setting" SET "is_enabled" = ? WHERE "user_id" = ?';
124
				}
125
126
				$stmt = $conn->create( $sql );
127
				$stmt->bind( 1, $status, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
128
				$stmt->bind( 2, $contentid, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
129
130
				$stmt->execute()->finish();
131
132
133
				$dbm->release( $conn, $dbname );
134
			}
135
			catch( \Exception $e )
136
			{
137
				$dbm->release( $conn, $dbname );
138
				throw $e;
139
			}
140
141
			return $contentid;
142
		} );
143
144
145
		return $ctx;
146
	}
147
}
148