Completed
Push — master ( ac66b3...ab3b6f )
by Aimeos
07:35 queued 01:23
created

CustomerAddEzpublishTestData   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 28.81 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
c 1
b 0
f 1
lcom 0
cbo 1
dl 34
loc 118
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getPreDependencies() 0 4 1
B migrate() 0 39 3
A getEzContext() 34 54 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
		$task = $this;
0 ignored issues
show
Unused Code introduced by
$task is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
94
95 View Code Duplication
		$ezContext->setEzUser( function( $code, $email, $password ) use ( $context ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
97
			$id = mt_rand( -0x7fffffff,-1 );
98
			$dbm = $context->getDatabaseManager();
99
			$dbconf = $context->getConfig()->get( 'resource/db-customer' );
100
			$dbname = ( $dbconf ? 'db-customer' : 'db' );
101
			$conn = $dbm->acquire( $dbname );
102
103
			try
104
			{
105
				$stmt = $conn->create( 'INSERT INTO "ezuser" (
106
					"contentobject_id", "email", "login", "login_normalized", "password_hash", "password_hash_type"
107
				) VALUES (
108
					?, ?, ?, ?, ?, 5
109
				)' );
110
111
				$stmt->bind( 1, $id, \Aimeos\MW\DB\Statement\Base::PARAM_INT );
112
				$stmt->bind( 2, $email );
113
				$stmt->bind( 3, $code );
114
				$stmt->bind( 4, $code );
115
				$stmt->bind( 5, $password );
116
117
				$stmt->execute()->finish();
118
119
				$dbm->release( $conn, $dbname );
120
			}
121
			catch( \Exception $e )
122
			{
123
				$dbm->release( $conn, $dbname );
124
				throw $e;
125
			}
126
127
			return $id;
128
		} );
129
130
		return $ezContext;
131
	}
132
}
133