Passed
Pull Request — master (#190)
by Arman
03:14
created

Model::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
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.9.5
13
 */
14
15
namespace Quantum\Libraries\Database\Adapters\Sleekdb\Statements;
16
17
use Quantum\Libraries\Database\Exceptions\DatabaseException;
18
use Quantum\Libraries\Database\Exceptions\ModelException;
19
use Quantum\Libraries\Database\Contracts\DbalInterface;
20
use SleekDB\Exceptions\InvalidConfigurationException;
21
use SleekDB\Exceptions\InvalidArgumentException;
22
use SleekDB\Exceptions\IdNotAllowedException;
23
use SleekDB\Exceptions\JsonException;
24
use SleekDB\Exceptions\IOException;
25
26
/**
27
 * Trait Model
28
 * @package Quantum\Libraries\Database
29
 */
30
trait Model
31
{
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function create(): DbalInterface
37
    {
38
        $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...
39
        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\Contracts\DbalInterface.
Loading history...
40
    }
41
42
    /**
43
     * @inheritDoc
44
     */
45
    public function prop(string $key, $value = null)
46
    {
47
        if (func_num_args() == 2) {
48
            $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...
49
        } else {
50
            return $this->modifiedFields[$key] ?? null;
51
        }
52
    }
53
54
    /**
55
     * @inheritDoc
56
     * @throws DatabaseException
57
     * @throws IOException
58
     * @throws IdNotAllowedException
59
     * @throws InvalidArgumentException
60
     * @throws InvalidConfigurationException
61
     * @throws JsonException
62
     */
63
    public function save(): bool
64
    {
65
        $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

65
        /** @scrutinizer ignore-call */ 
66
        $ormMode = $this->getOrmModel();
Loading history...
66
67
        if ($this->isNew) {
68
            $ormMode->insert($this->modifiedFields);
69
        } else {
70
            $ormMode->update($this->modifiedFields);
71
        }
72
73
        return true;
74
    }
75
76
    /**
77
     * @inheritDoc
78
     * @throws DatabaseException
79
     * @throws IOException
80
     * @throws InvalidArgumentException
81
     * @throws InvalidConfigurationException
82
     */
83
    public function delete(): bool
84
    {
85
        $pk = $this->getOrmModel()->getPrimaryKey();
86
        return $this->getOrmModel()->deleteById($this->data[$pk]);
87
    }
88
89
    /**
90
     * @inheritDoc
91
     * @throws DatabaseException
92
     * @throws IOException
93
     * @throws InvalidArgumentException
94
     * @throws InvalidConfigurationException
95
     * @throws ModelException
96
     */
97
    public function deleteMany(): bool
98
    {
99
        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

99
        return $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->delete();
Loading history...
100
    }
101
}