Passed
Pull Request — dev (#47)
by Stone
04:17 queued 01:57
created

ConfigModel::getAllConfigOrdered()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 3
nop 0
dl 0
loc 23
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Core\Model;
6
7
/**
8
 * Class ConfigModel
9
 * @package App\Models
10
 */
11
class ConfigModel extends Model
12
{
13
    /**
14
     * Returns all the configs orderd with their class names
15
     * @return array
16
     * @throws \Exception
17
     */
18
    public function getAllConfigOrdered(): array
19
    {
20
        $returnData = [];
21
        //getting our tables
22
        $configsTbl = $this->getTablePrefix('configs');
23
        $configsClassTbl = $this->getTablePrefix('configs_class');
24
        $sql = "SELECT idconfigs, configs_name, configs_type, configs_value, class FROM  $configsTbl 
25
                INNER JOIN $configsClassTbl ON $configsTbl.configs_class_idconfigsclass = $configsClassTbl.idconfigsclass
26
                ORDER BY class;";
27
28
        $this->query($sql);
29
        $this->execute();
30
        $configClass = $this->fetchAll();
31
        foreach ($configClass as $class) {
32
            //we remove the first 3 characters as they are used for ordering (10_global_site_configuration)
33
            $className = substr($class->class, 3);
34
35
            if (!isset($returnData[$className])) {
36
                $returnData[$className] = [];
37
            }
38
            $returnData[$className][] = $class;
39
        }
40
        return $returnData;
41
    }
42
43
    /**
44
     * gets the entire config table
45
     * @return array
46
     * @throws \ReflectionException
47
     */
48
    public function getAllConfig()
49
    {
50
        return $this->getResultSet();
51
    }
52
53
    /**
54
     * updates the site config table
55
     * @param int $id id of the config
56
     * @param string $param paramter to update
57
     * @return bool update success
58
     * @throws \Exception error
59
     */
60
    public function updateConfig(int $id, string $param):bool
61
    {
62
        $configsTbl = $this->getTablePrefix('configs');
63
        $sql = "UPDATE $configsTbl SET configs_value = :param WHERE idconfigs = :id";
64
        $this->query($sql);
65
        $this->bind(':param', $param);
66
        $this->bind(':id', $id);
67
        return $this->execute();
68
69
    }
70
}