TightModelSelector   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 22
eloc 42
c 3
b 0
f 0
dl 0
loc 97
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A filter_event() 0 8 3
A model() 0 3 1
A filter_with_fields() 0 5 4
A __construct() 0 6 1
A class() 0 3 1
A filter_with_ids() 0 6 2
A statement() 0 3 1
B select() 0 34 9
1
<?php
2
3
namespace HexMakina\TightORM;
4
5
use HexMakina\BlackBox\ORM\ModelInterface;
6
use HexMakina\BlackBox\Database\SelectInterface;
7
8
class TightModelSelector
9
{
10
11
    private \HexMakina\BlackBox\ORM\ModelInterface $model;
12
13
    private string $model_class;
14
15
    private $model_table;
16
17
    private \HexMakina\BlackBox\Database\SelectInterface $statement;
18
19
    public function __construct(ModelInterface $model)
20
    {
21
        $this->model = $model;
22
        $this->model_class = get_class($model);
23
        $this->model_table = $this->model_class::table();
24
        $this->statement = $this->model_table->select();
25
    }
26
27
    public function model(): ModelInterface
28
    {
29
        return $this->model;
30
    }
31
32
    public function class(): string
33
    {
34
        return $this->model_class;
35
    }
36
37
    public function statement(): SelectInterface
38
    {
39
        return $this->statement;
40
    }
41
42
    public function select($filters = [], $options = []): SelectInterface
43
    {
44
        $table_alias = $options['table_alias'] ?? get_class($this->model)::tableAlias();
45
46
        $this->statement = $this->model_table->select(null, $table_alias);
47
48
            
49
        if (isset($options['order_by'])) {
50
            $this->statement()->orderBy($options['order_by']);
51
        }
52
        
53
        if (isset($options['limit'])) {
54
            if(is_array($options['limit'])){
55
                $this->statement()->limit($options['limit'][0], $options['limit'][1]);
56
            }
57
            elseif(is_numeric($options['limit'])){
58
                $this->statement()->limit($options['limit']);
59
            }
60
        }
61
        $this->filter_with_fields($filters);
62
63
        if (is_subclass_of($this->model(), '\HexMakina\kadro\Models\Interfaces\EventInterface')) {
64
            $this->filter_event($filters['date_start'] ?? null, $filters['date_stop'] ?? null);
65
            $this->statement()->orderBy([$this->model()->event_field(), 'DESC']);
0 ignored issues
show
Bug introduced by
The method event_field() does not exist on HexMakina\BlackBox\ORM\ModelInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
            $this->statement()->orderBy([$this->model()->/** @scrutinizer ignore-call */ event_field(), 'DESC']);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
66
        }
67
68
        if (isset($filters['content'])) {
69
            $this->statement()->whereFilterContent($filters['content']);
70
        }
71
72
        if (isset($filters['ids']) && is_array($filters['ids'])) {
73
            $this->filter_with_ids(array_filter($filters['ids'], function($value) { return !is_null($value); }));
74
        }
75
        return $this->statement();
76
    }
77
78
    public function filter_event($date_start = null, $date_stop = null): void
79
    {
80
        if (!empty($date_start)) {
81
            $this->statement()->whereGTE($this->model()->event_field(), $date_start, $this->statement()->tableLabel(), ':filter_date_start');
82
        }
83
84
        if (!empty($date_stop)) {
85
            $this->statement()->whereLTE($this->model()->event_field(), $date_stop, $this->statement()->tableLabel(), ':filter_date_stop');
86
        }
87
      // if(empty($options['order_by']))
88
      //   $this->statement()->orderBy([$this->model()->event_field(), 'DESC']);
89
    }
90
91
    public function filter_with_ids($ids): void
92
    {
93
        if (empty($ids)) {
94
            $this->statement()->where('1=0'); // TODO: this is a new low.. find another way to cancel query
95
        } else {
96
            $this->statement()->whereNumericIn('id', $ids);
97
        }
98
    }
99
100
    public function filter_with_fields($filters, $filter_mode = 'whereEQ'): void
101
    {
102
        foreach ($this->model_table->columns() as $column_name => $column) {
103
            if (isset($filters[$column_name]) && is_scalar($filters[$column_name])) {
104
                $this->statement()->$filter_mode($column_name, $filters[$column_name], $this->statement()->tableLabel());
105
            }
106
        }
107
    }
108
}
109