Completed
Push — master ( f731c8...255ed4 )
by Arman
14s queued 12s
created

Model   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A prop() 0 6 2
A save() 0 11 2
A create() 0 4 1
A delete() 0 4 1
A deleteMany() 0 3 1
1
<?php
2
3
/**
4
 * Quantum PHP Framework
5
 *
6
 * An open source software development framework for PHP
7
 *
8
 * @package Quantum
9
 * @author Arman Ag. <[email protected]>
10
 * @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
11
 * @link http://quantum.softberg.org/
12
 * @since 2.6.0
13
 */
14
15
namespace Quantum\Libraries\Database\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\DbalInterface;
18
19
/**
20
 * Trait Model
21
 * @package Quantum\Libraries\Database\Sleekdb\Statements
22
 */
23
trait Model
24
{
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function create(): DbalInterface
30
    {
31
        $this->isNew = true;
0 ignored issues
show
Bug Best Practice introduced by
The property isNew does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...leekdb\Statements\Model which is incompatible with the type-hinted return Quantum\Libraries\Database\DbalInterface.
Loading history...
33
    }
34
35
    /**
36
     * @inheritDoc
37
     */
38
    public function prop(string $key, $value = null)
39
    {
40
        if ($value) {
41
            $this->modifiedFields[$key] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property modifiedFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        } else {
43
            return $this->modifiedFields[$key] ?? null;
44
        }
45
    }
46
47
    /**
48
     * @inheritDoc
49
     * @throws \SleekDB\Exceptions\IOException
50
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
51
     * @throws \Quantum\Exceptions\DatabaseException
52
     * @throws \SleekDB\Exceptions\InvalidArgumentException
53
     */
54
    public function save(): bool
55
    {
56
        $ormMode = $this->getOrmModel();
0 ignored issues
show
Bug introduced by
It seems like getOrmModel() 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

56
        /** @scrutinizer ignore-call */ 
57
        $ormMode = $this->getOrmModel();
Loading history...
57
58
        if ($this->isNew) {
59
            $ormMode->insert($this->modifiedFields);
60
        } else {
61
            $ormMode->update($this->modifiedFields);
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * @inheritDoc
69
     * @throws \Quantum\Exceptions\DatabaseException
70
     * @throws \SleekDB\Exceptions\IOException
71
     * @throws \SleekDB\Exceptions\InvalidArgumentException
72
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
73
     */
74
    public function delete(): bool
75
    {
76
        $pk = $this->getOrmModel()->getPrimaryKey();
77
        return $this->getOrmModel()->deleteById($this->data[$pk]);
78
    }
79
80
    /**
81
     * @inheritDoc
82
     * @throws \Quantum\Exceptions\DatabaseException
83
     * @throws \SleekDB\Exceptions\IOException
84
     * @throws \SleekDB\Exceptions\InvalidArgumentException
85
     * @throws \SleekDB\Exceptions\InvalidConfigurationException
86
     */
87
    public function deleteMany(): bool
88
    {
89
        return $this->getBuilder()->getQuery()->delete();
0 ignored issues
show
Bug introduced by
It seems like getBuilder() 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
        return $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->delete();
Loading history...
90
    }
91
}