Passed
Pull Request — master (#190)
by Arman
02:47
created

Result   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 120
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A paginate() 0 3 1
A setHidden() 0 3 1
A findOneBy() 0 7 1
A count() 0 3 1
A first() 0 7 1
A get() 0 9 1
A findOne() 0 7 1
A asArray() 0 9 4
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\Contracts\PaginatorInterface;
18
use Quantum\Libraries\Database\Exceptions\DatabaseException;
19
use Quantum\Libraries\Database\Adapters\Sleekdb\Paginator;
20
use Quantum\Libraries\Database\Exceptions\ModelException;
21
use Quantum\Libraries\Database\Contracts\DbalInterface;
22
use SleekDB\Exceptions\InvalidConfigurationException;
23
use SleekDB\Exceptions\InvalidArgumentException;
24
use SleekDB\Exceptions\IOException;
25
26
/**
27
 * Trait Result
28
 * @package Quantum\Libraries\Database
29
 */
30
trait Result
31
{
32
    /**
33
     * @inheritDoc
34
     * @return array
35
     * @throws DatabaseException
36
     * @throws ModelException
37
     * @throws IOException
38
     * @throws InvalidArgumentException
39
     * @throws InvalidConfigurationException
40
     */
41
    public function get(): array
42
    {
43
        return array_map(function ($element) {
44
            $item = clone $this;
45
            $item->data = $element;
0 ignored issues
show
Bug Best Practice introduced by
The property data does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
46
            $item->modifiedFields = $element;
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...
47
            $item->isNew = false;
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...
48
            return $item;
49
        }, $this->getBuilder()->getQuery()->fetch());
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

49
        }, $this->/** @scrutinizer ignore-call */ getBuilder()->getQuery()->fetch());
Loading history...
50
    }
51
52
    /**
53
     * @inheritDoc
54
     * @return PaginatorInterface
55
     * @throws DatabaseException
56
     * @throws IOException
57
     * @throws InvalidArgumentException
58
     * @throws InvalidConfigurationException
59
     * @throws ModelException
60
     */
61
    public function paginate(int $perPage, int $currentPage = 1): PaginatorInterface
62
    {
63
        return new Paginator($this, $perPage, $currentPage);
64
    }
65
66
    /**
67
     * @inheritDoc
68
     * @return DbalInterface
69
     * @throws DatabaseException
70
     * @throws IOException
71
     * @throws InvalidArgumentException
72
     * @throws InvalidConfigurationException
73
     */
74
    public function findOne(int $id): DbalInterface
75
    {
76
        $result = $this->getOrmModel()->findById($id);
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

76
        $result = $this->/** @scrutinizer ignore-call */ getOrmModel()->findById($id);
Loading history...
77
78
        $this->updateOrmModel($result);
0 ignored issues
show
Bug introduced by
It seems like updateOrmModel() 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

78
        $this->/** @scrutinizer ignore-call */ 
79
               updateOrmModel($result);
Loading history...
79
80
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
81
    }
82
83
    /**
84
     * @inheritDoc
85
     * @throws DatabaseException
86
     * @throws IOException
87
     * @throws InvalidArgumentException
88
     * @throws InvalidConfigurationException
89
     */
90
    public function findOneBy(string $column, $value): DbalInterface
91
    {
92
        $result = $this->getOrmModel()->findOneBy([$column, '=', $value]);
93
94
        $this->updateOrmModel($result);
95
96
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
97
    }
98
99
    /**
100
     * @inheritDoc
101
     * @throws DatabaseException
102
     * @throws ModelException
103
     * @throws IOException
104
     * @throws InvalidArgumentException
105
     * @throws InvalidConfigurationException
106
     */
107
    public function first(): DbalInterface
108
    {
109
        $result = $this->getBuilder()->getQuery()->first();
110
111
        $this->updateOrmModel($result);
112
113
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Quantum\Libraries\Databa...eekdb\Statements\Result which is incompatible with the type-hinted return Quantum\Libraries\Database\Contracts\DbalInterface.
Loading history...
114
    }
115
116
    /**
117
     * @inheritDoc
118
     * @throws DatabaseException
119
     * @throws ModelException
120
     * @throws IOException
121
     * @throws InvalidArgumentException
122
     * @throws InvalidConfigurationException
123
     */
124
    public function count(): int
125
    {
126
        return count($this->getBuilder()->getQuery()->fetch());
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function asArray(): array
133
    {
134
        $result = $this->data ?: [];
135
136
        if (count($this->hidden) > 0 && count($result)) {
137
            $result = $this->setHidden($result);
138
        }
139
140
        return $result;
141
    }
142
143
    /**
144
     * @param $result
145
     * @return array
146
     */
147
    public function setHidden($result): array
148
    {
149
        return array_diff_key($result, array_flip($this->hidden));
150
    }
151
}