Passed
Push — master ( 41bc94...c383d9 )
by dima
04:18
created

TraitDataMapperEvent   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 3
cbo 0
dl 0
loc 102
ccs 0
cts 44
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A onPrepareData() 0 8 4
A onBeforeDelete() 0 14 4
C onAfterSave() 0 39 8
A onBeforeSave() 0 17 1
1
<?php
2
/*
3
 * To change this license header, choose License Headers in Project Properties.
4
 * To change this template file, choose Tools | Templates
5
 * and open the template in the editor.
6
 */
7
8
namespace SimpleORM;
9
10
/**
11
 * Description of TraitDataMapperEvent
12
 *
13
 * @author d.lanec
14
 */
15
trait TraitDataMapperEvent {
16
17
	/**
18
	 * Перед сохранением извелкаем объект и дополняем массив для записи, недостающими полями
19
	 * @param \Autoprice\Domain\Price\EntityInterface $Entity
20
	 * @param type $data
21
	 */
22
	protected function onPrepareData(\SimpleORM\EntityInterface $Entity, &$data) {
0 ignored issues
show
Unused Code introduced by
The parameter $Entity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
23
		foreach ($this->mapping_fields as $field => $cfg) {
0 ignored issues
show
Bug introduced by
The property mapping_fields does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24
25
			if ($cfg['null'] === false && empty($data[$cfg['field']])) {
26
				$data[$cfg['field']] = $cfg['default'];
27
			}
28
		}
29
	}
30
31
	/**
32
	 * На успешное удаление
33
	 * @param \SimpleORM\EntityInterface $Entity
34
	 */
35
	protected function onBeforeDelete(EntityInterface $Entity) {
36
		foreach ($this->relations as $alias => $cfg) {
0 ignored issues
show
Bug introduced by
The property relations does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
			$mapper = $cfg['mapper'];
38
			//если связь один к одному то удаляем сущность
39
			if ($cgg['reltype'] == 'has_one') {
0 ignored issues
show
Bug introduced by
The variable $cgg does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
40
				$Entity = $Entity->{'get' . $alias}();
41
				if (!$mapper->delete($Entity)) {
42
					throw new \Autoprice\Exceptions\EntityNotDeleteException('Unable to delete Entity!');
43
				}
44
			}
45
		}
46
47
		return true;
48
	}
49
50
	/**
51
	 * Событие перед сохранением
52
	 * @param \SimpleORM\EntityInterface $Entity
53
	 */
54
	protected function onAfterSave(EntityInterface $Entity) {
55
56
		$this->getAdapter()->startTransaction();
0 ignored issues
show
Bug introduced by
It seems like getAdapter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
57
58
		$rel_list = $this->createListRelation();
0 ignored issues
show
Bug introduced by
It seems like createListRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
60
		foreach ($rel_list as $obj_path => $mapper) {
61
62
			$call_obj = '$Entity'.$obj_path.';';
63
64
			$set_path = str_replace(['#', '();'], ['->set', '($o);'], $call_obj);
65
		
66
			$ar_path = explode('()',$obj_path);
67
			
68
			$o = $Entity;
69
		
70
			foreach ($ar_path as $_m){
71
				
72
				$_mc = str_replace('#','get',mb_ucfirst($_m));
73
74
				//Set logic
75
				if(empty($_m)){
76
				
77
					$_mc = ltrim( $ar_path[(count($ar_path)-2)] , '#');
78
					
79
					if (is_object($$_mc) && is_a($$_mc,'SimpleORM\EntityInterface') && $this->DI->get($mapper)->saveWithoutEvents($o)) {
0 ignored issues
show
Bug introduced by
The property DI does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
80
						$o = $$_mc;
81
						eval($set_path);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
82
					}
83
				}
84
				elseif(is_object($o) ){
85
					$$_mc = $o->{$_mc}();
86
					$o = $$_mc;
87
				}
88
				
89
			}
90
			
91
		}
92
	}
93
94
	/**
95
	 * После успешного сохранения
96
	 * @param \SimpleORM\EntityInterface $Entity
97
	 */
98
	protected function onBeforeSave(EntityInterface $Entity) {
0 ignored issues
show
Unused Code introduced by
The parameter $Entity is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
99
100
		$this->getAdapter()->endTransaction();
0 ignored issues
show
Bug introduced by
It seems like getAdapter() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
101
102
//		foreach ($this->relations as $alias => $mapper) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
57% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
103
//		
104
//			$SaveEntity = $Entity->{'get'.$alias}();
105
//			
106
//			if(!$mapper->save($SaveEntity)){
107
//				throw new \Autoprice\Exceptions\EntityNotSaveException('Unable to save Entity!');
108
//			}
109
//			
110
//			unset($SaveEntity);
111
//		}
112
//		
113
//		return true;
114
	}
115
116
}
117