Passed
Push — master ( 511eb5...e7d893 )
by Henri
03:04 queued 01:47
created

EntityTrait::toEntity()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 16
rs 10
1
<?php
2
3
namespace HnrAzevedo\Datamanager;
4
5
use Exception;
6
7
trait EntityTrait{
8
    public function toEntity()
9
    {
10
        if($this->getCount() === 0){
0 ignored issues
show
Bug introduced by
It seems like getCount() 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

10
        if($this->/** @scrutinizer ignore-call */ getCount() === 0){
Loading history...
11
            return null;
12
        }
13
14
        $entity = $this->setByDatabase($this->result[0]);
0 ignored issues
show
Bug introduced by
It seems like setByDatabase() 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

14
        /** @scrutinizer ignore-call */ 
15
        $entity = $this->setByDatabase($this->result[0]);
Loading history...
15
16
        if(count($this->result) > 1){
17
            $entity = [];
18
            foreach ($this->result as $key => $value) {
19
                $entity[] = $this->setByDatabase($value);
20
            }
21
        }
22
23
        return $entity;
24
    }
25
26
    public function persist()
27
    {
28
        $columns = '';
29
        $values = '';
30
        $data = [];
31
32
        foreach ($this->data as $key => $value) {
33
            if(strstr($this->data[$key]['extra'],'auto_increment')){
34
                continue;
35
            }
36
37
            $this->checkMaxlength($key, $value['value'], $value['maxlength']);
0 ignored issues
show
Bug introduced by
It seems like checkMaxlength() 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

37
            $this->/** @scrutinizer ignore-call */ 
38
                   checkMaxlength($key, $value['value'], $value['maxlength']);
Loading history...
38
39
            $columns .= $key.',';
40
            $values .= ':'.$key.',';
41
            $data[$key] = $value['value'];
42
        }
43
44
        $this->transaction('begin');
0 ignored issues
show
Bug introduced by
It seems like transaction() 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

44
        $this->/** @scrutinizer ignore-call */ 
45
               transaction('begin');
Loading history...
45
        try{
46
           
47
            $id = $this->insert($data);
0 ignored issues
show
Bug introduced by
It seems like insert() 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

47
            /** @scrutinizer ignore-call */ 
48
            $id = $this->insert($data);
Loading history...
48
49
            $this->check_fail();
0 ignored issues
show
Bug introduced by
It seems like check_fail() 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

49
            $this->/** @scrutinizer ignore-call */ 
50
                   check_fail();
Loading history...
50
51
            $this->getData()[$this->primary]['value'] = $id;
0 ignored issues
show
Bug introduced by
It seems like getData() 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

51
            $this->/** @scrutinizer ignore-call */ 
52
                   getData()[$this->primary]['value'] = $id;
Loading history...
52
            
53
            $this->transaction('commit');
54
55
        }catch(Exception $er){
56
            $this->transaction('rollback');
57
            throw $er;
58
        }
59
60
        return $this;
61
    }
62
63
    public function remove(?bool $exec = false)
64
    {
65
        if($exec !== null){
66
            $this->clause = 'remove';    
0 ignored issues
show
Bug Best Practice introduced by
The property clause does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
            return $this;
68
        }
69
70
        $this->clause = null;
71
72
        if(count($this->where) == 1){
73
            $this->removeById();
0 ignored issues
show
Bug introduced by
The method removeById() does not exist on HnrAzevedo\Datamanager\EntityTrait. Did you maybe mean remove()? ( Ignorable by Annotation )

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

73
            $this->/** @scrutinizer ignore-call */ 
74
                   removeById();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            return $this;
75
        }
76
77
        $this->delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
0 ignored issues
show
Bug introduced by
It seems like mountRemove() 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

77
        $this->delete($this->/** @scrutinizer ignore-call */ mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
Loading history...
Bug introduced by
It seems like delete() 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

77
        $this->/** @scrutinizer ignore-call */ 
78
               delete($this->mountRemove()['where'], substr( $this->mountRemove()['data'] ,0,-1) );
Loading history...
78
79
        $this->check_fail();
80
            
81
        return $this;
82
    }
83
84
    public function save()
85
    {
86
        $this->transaction('begin');
87
88
        try{
89
            $this->update(
0 ignored issues
show
Bug introduced by
It seems like update() 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

89
            $this->/** @scrutinizer ignore-call */ 
90
                   update(
Loading history...
90
                $this->mountSave()['data'],
0 ignored issues
show
Bug introduced by
It seems like mountSave() 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

90
                $this->/** @scrutinizer ignore-call */ 
91
                       mountSave()['data'],
Loading history...
91
                "{$this->primary}=:{$this->primary}", 
92
                $this->primary.'='.$this->getData()[$this->primary]['value']
93
            );
94
95
            $this->check_fail();
96
97
            $this->transaction('commit');
98
        }catch(Exception $er){
99
            $this->transaction('rollback');
100
            throw $er;
101
        }
102
103
        return $this;
104
    }
105
}