GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ModelWithObject::create()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 8
Ratio 44.44 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 8
loc 18
ccs 13
cts 15
cp 0.8667
crap 5.0592
rs 9.3554
c 0
b 0
f 0
1
<?php
2
3
namespace PhpBoot\ORM;
4
5
use Doctrine\Common\Cache\Cache;
6
use PhpBoot\DB\DB;
7
8
class ModelWithObject
9
{
10
    /**
11
     * ModelWithObject constructor.
12
     * @param DB $db
13
     * @param object $entity
14
     * @param Cache $cache
15
     */
16 4
    public function __construct(DB $db, $entity, Cache $cache)
0 ignored issues
show
Unused Code introduced by
The parameter $cache 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...
17
    {
18 4
        is_object($entity) or \PhpBoot\abort(new \InvalidArgumentException('object required'));
19 4
        $entityName = get_class($entity);
20 4
        $this->db = $db;
21 4
        $builder = $db->getApp()->get(ModelContainerBuilder::class);
22 4
        $this->entity = $builder->build($entityName);
23 4
        $this->object = $entity;
24 4
    }
25
    /**
26
     * @return void
27
     */
28 1
    public function create()
29
    {
30 1
        $data = [];
31 1
        foreach ($this->getColumns() as $column){
32 1 View Code Duplication
            if(isset($this->object->$column)){
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...
33 1
                if(is_array($this->object->$column) || is_object($this->object->$column)){
34
                    $data[$column] = json_encode($this->object->$column);
35
                }else{
36 1
                    $data[$column] = $this->object->$column;
37
                }
38
39 1
            }
40 1
        }
41 1
        $id = $this->db->insertInto($this->entity->getTable())
42 1
            ->values($data)
43 1
            ->exec()->lastInsertId();
44 1
        $this->object->{$this->entity->getPK()} = $id;
45 1
    }
46
47
    /**
48
     * @param array $columns columns to update. if columns is empty array, update all of the columns
49
     * @return int rows updated
50
     */
51 2
    public function update(array $columns=[])
52
    {
53 2
        $data = [];
54 2
        $pk = $this->entity->getPK();
55 2
        foreach ($this->getColumns() as $column){
56 2
            if(count($columns) && !in_array($column, $columns)){
57 1
                continue;
58
            }
59 2 View Code Duplication
            if($pk != $column && isset($this->object->$column)){
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...
60 2
                if(is_array($this->object->$column) || is_object($this->object->$column)){
61
                    $data[$column] = json_encode($this->object->$column);
62
                }else{
63 2
                    $data[$column] = $this->object->$column;
64
                }
65 2
            }
66 2
        }
67
68 2
        return $this->db->update($this->entity->getTable())
69 2
            ->set($data)
70 2
            ->where("`{$pk}` = ?", $this->object->$pk)
71 2
            ->exec()->rows;
72
    }
73
74
    /**
75
     * @return int rows deleted
76
     */
77 1
    public function delete()
78
    {
79 1
        $pk = $this->entity->getPK();
80 1
        return $this->db->deleteFrom($this->entity->getTable())
81 1
            ->where([$pk => $this->object->$pk])
82 1
            ->exec()->rows;
83
    }
84
85
    /**
86
     * set entity table name
87
     * @param string $tableName
88
     * @return $this
89
     */
90
    public function withTable($tableName)
91
    {
92
        $this->entity->setTable($tableName);
93
        return $this;
94
    }
95
96 3
    protected function getColumns()
97
    {
98 3
        $columns = [];
99 3
        foreach ($this->entity->getProperties() as $p){
100 3
            $columns[] = $p->name;
101 3
        }
102 3
        return $columns;
103
    }
104
105
    /**
106
     * @var object
107
     */
108
    protected $object;
109
110
    /**
111
     * @var ModelContainer
112
     */
113
    protected $entity;
114
    /**
115
     * @var DB
116
     */
117
    protected $db;
118
}