DataManager::isAltering()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace API\Helpers;
4
5
use MazenTouati\Simple2wayConfig\S2WConfigException;
6
7
class DataManager
8
{
9
    // the query to run
10
    public $query = '';
11
    public $queryResult = null;
12
    //data to return in case of error
13
    private $error = [];
14
15
    public $db = null;
16
    public $config = null;
17
18
    public $paginator = null;
19
    private $paginatorConfig = [
20
    'item_per_page' => 25,
21
    'current_page'  => 1,
22
  ];
23
24
    private $customMessage = false;
25
26
    // query type: fetching, inserting or altering (update, delete)
27
    private $type = '';
28
    const TYPE_FETCHING = 'fetching';
29
    const TYPE_INSERTING = 'inserting';
30
    const TYPE_ALTERING = 'altering';
31
    const TYPE_OTHER = 'other';
32
33
    public function __construct($query, $container, $currentPage = 1)
34
    {
35
        $this->query = $query;
36
        $this->db = $container->db;
37
        $this->config = $container->config;
38
        $this->paginatorConfig['current_page'] = $currentPage;
39
40
        $this->determineQueryType();
41
        $this->runQuery();
42
    }
43
44
    private function determineQueryType()
45
    {
46
        if (starts_with($this->query, 'select ')) {
47
            $this->type = self::TYPE_FETCHING;
48
        } elseif (starts_with($this->query, 'insert ')) {
49
            $this->type = self::TYPE_INSERTING;
50
        } elseif (starts_with($this->query, 'update ') || starts_with($this->query, 'delete ')) {
51
            $this->type = self::TYPE_ALTERING;
52
        } else {
53
            $this->type = self::TYPE_OTHER;
54
        }
55
    }
56
57
    public function isError()
58
    {
59
        return !empty($this->error);
60
    }
61
62
    public function getErrorData()
63
    {
64
        return $this->error;
65
    }
66
67
    public function isFetching()
68
    {
69
        return $this->type === self::TYPE_FETCHING;
70
    }
71
72
    public function isInserting()
73
    {
74
        return $this->type === self::TYPE_INSERTING;
75
    }
76
77
    public function isAltering()
78
    {
79
        return $this->type === self::TYPE_ALTERING;
80
    }
81
82
    public function isOther()
83
    {
84
        return $this->type === self::TYPE_OTHER;
85
    }
86
87
    public function getCustomMessage()
88
    {
89
        return $this->customMessage;
90
    }
91
92
    private function runQuery()
93
    {
94
        try {
95
            if ($this->isFetching()) {
96
                $this->paginator = new Paginator($this->query, $this->paginatorConfig, $this->db);
97
            } else {
98
                if ($this->runSpecialQueries() === false) {
99
                    $this->queryResult = $this->db->query($this->query);
100
                }
101
            }
102
        } catch (\PDOException $e) {
103
            // this entry is used by the front-end part, because we return a 200 status code even when there's a syntax error or logical error in the query.
104
            $this->error['error'] = true;
105
            $this->error['message'] = $e->getMessage();
106
        }
107
    }
108
109
    private function runSpecialQueries()
110
    {
111
        if (starts_with($this->query, 'use')) {
112
            $database = str_replace('use ', '', $this->query);
113
114
            //run query against database to make sure the database name is valid
115
            if ($this->db->query($this->query) !== false) {
116
                $this->config->set('database.drivers.mysql.database', $database);
117
118
                try {
119
                    $this->config->sync('database');
120
                } catch (S2WConfigException $e) {
121
                    $this->customMessage = $e->getMessage();
0 ignored issues
show
Documentation Bug introduced by
The property $customMessage was declared of type boolean, but $e->getMessage() is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
122
123
                    return false;
124
                }
125
                $this->customMessage = '`'.$database.'` is your new database now.';
0 ignored issues
show
Documentation Bug introduced by
The property $customMessage was declared of type boolean, but '`' . $database . '` is your new database now.' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
126
127
                return true;
128
            }
129
        }
130
131
        return false;
132
    }
133
}
134