Relationships   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 14
c 4
b 1
f 0
lcom 1
cbo 2
dl 0
loc 51
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C joinClause() 0 29 8
B whereClause() 0 18 6
1
<?php
2
/**
3
 * PotatoORM manages the persistence of database CRUD operations.
4
 *
5
 * @package Ibonly\PotatoORM\Model
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use Ibonly\PotatoORM\DatabaseQuery;
13
use Ibonly\PotatoORM\RelationshipsInterface;
14
15
class Relationships extends DatabaseQuery implements RelationshipsInterface
16
{
17
    public function joinClause($con = null)
18
    {
19
        $connection = self::checkConnection($con);
20
21
        $data = self::query('SELECT COLUMN_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME="'.self::getTableName($connection).'"')->all();
22
23
        $output = "";
24
        $i = 0;
25
26
        if (! isset($data[0]) || $data[1]->REFERENCED_TABLE_NAME !== null) {
27
            foreach($data as $key => $value) {
28
                if ( ! empty($value->REFERENCED_TABLE_NAME)) {
29
                    $output .= ' JOIN '.$value->REFERENCED_TABLE_NAME;
30
                }
31
            }
32
            foreach($data as $key => $value) {
33
                $i++;
34
                $whereAnd = $i > 1 ? 'AND' : 'WHERE';
35
                if ( empty($value->REFERENCED_TABLE_NAME)) {
36
                    $value->REFERENCED_TABLE_NAME = self::getTableName($connection);
37
                    $value->REFERENCED_COLUMN_NAME = $value->COLUMN_NAME;
38
                }
39
                $output .= ' '.$whereAnd.' '.self::getTableName($connection).'.'.$value->COLUMN_NAME.'='.$value->REFERENCED_TABLE_NAME.'.'.$value->REFERENCED_COLUMN_NAME.' ';
40
            }
41
         } else {
42
            $output = false;
43
         }
44
        return $output;
45
    }
46
47
    public function whereClause($data = null, $condition = null, $con = null)
48
    {
49
        $joinClause = self::joinClause();
50
        $connection = self::checkConnection($con);
51
        $tableName  = self::getTableName($connection);
52
        $columnName = self::whereAndClause($tableName, $data, $condition);
53
54
        $query = self::selectAllQuery($tableName);
55
56
        if ($joinClause == false && $data === null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $joinClause of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
57
            $query .= $columnName;
58
        } else if ($joinClause == false && $data !== null) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $joinClause of type string|false against false; this is ambiguous if the string can be empty. Consider using a strict comparison === instead.
Loading history...
59
            $query .= ' WHERE '.$columnName;
60
        } else {
61
            $query .= ($data === null) ? $joinClause : $joinClause .' AND '.$columnName;
62
        }
63
        return $query;
64
    }
65
}