Passed
Push — 1.0.x ( 8353d5...3a2c37 )
by Julien
21:28
created

IdentityConditions::defaultIdentityCondition()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 7
nop 0
dl 0
loc 31
ccs 0
cts 20
cp 0
crap 30
rs 9.3222
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Controller\Traits\Query\Conditions;
13
14
use Phalcon\Db\Column;
15
use Phalcon\Filter\Filter;
16
use Phalcon\Support\Collection;
17
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractModel;
18
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams;
19
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractQuery;
20
21
trait IdentityConditions
22
{
23
    use AbstractModel;
24
    use AbstractParams;
25
    use AbstractQuery;
26
    
27
    protected ?Collection $identityConditions;
28
    
29
    public function initializeIdentityConditions(): void
30
    {
31
        $this->setIdentityConditions(new Collection([
32
            'default' => $this->defaultIdentityCondition(),
33
        ], false));
34
    }
35
    
36
    public function setIdentityConditions(?Collection $identityConditions): void
37
    {
38
        $this->identityConditions = $identityConditions;
39
    }
40
    
41
    public function getIdentityConditions(): ?Collection
42
    {
43
        return $this->identityConditions;
44
    }
45
    
46
    /**
47
     * Builds the identity condition based on the current user's identity and role.
48
     *
49
     * @return array|string|null Returns an array with the following elements:
50
     *                         - If identity columns are empty, returns null.
51
     *                         - If no identity is found, returns ['false'].
52
     *                         - If the current user role is a super admin, returns ['true'].
53
     *                         - If identity conditions are found, returns an array with the following elements:
54
     *                           - The condition string formed by joining the columns with 'or' operators.
55
     *                           - An array of bind values for the condition.
56
     *                           - An array of bind types for the condition.
57
     */
58
    public function defaultIdentityCondition(): array|string|null
59
    {
60
        $columns = $this->getIdentityColumns();
61
        
62
        if (empty($columns)) {
63
            return null;
64
        }
65
        
66
        $query = [];
67
        $bind = [];
68
        $bindTypes = [];
69
        
70
        foreach ($columns as $column) {
71
            $rawValue = $this->getParam($column, [Filter::FILTER_STRING, Filter::FILTER_TRIM]);
72
            if (!isset($rawValue)) {
73
                continue;
74
            }
75
            
76
            $field = $this->appendModelName($column);
77
            $value = $this->generateBindKey('identity');
78
            
79
            $bind[$value] = $rawValue;
80
            $bindTypes[$value] = Column::BIND_PARAM_STR;
81
            
82
            $query [] = "{$field} = :{$value}:";
83
        }
84
        
85
        return empty($query)? null : [
86
            implode(' and ', $query),
87
            $bind,
88
            $bindTypes,
89
        ];
90
    }
91
    
92
    /**
93
     * Retrieves the identity columns of the current model.
94
     *
95
     * @return array The identity columns.
96
     */
97
    public function getIdentityColumns(): array
98
    {
99
        // @todo get primary keys from the model
100
        return ['id'];
101
    }
102
}
103