Completed
Branch master (e3a0da)
by
unknown
15:36
created

DatabaseTransactionsTrait::deleteRecord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
namespace Opeyemiabiodun\PotatoORM\Connections;
4
5
use InvalidArgumentException;
6
7
trait DatabaseTransactionsTrait 
8
{
9
    /**
10
     * Creates a record in the database.
11
     *
12
     * @param string $table  The table where the a new record is made.
13
     * @param array  $record The record to be made in the database.
14
     *
15
     * @return bool
16
     */
17
    public function createRecord($table, $record)
18
    {
19
        if (gettype($table) !== 'string') {
20
            throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead.");
21
        }
22
23
        if (gettype($record) !== 'array') {
24
            throw new InvalidArgumentException("The parameter {$record} is not an array. An array is required instead.");
25
        }
26
27
        $count = count($record);
28
29
        $sql = "INSERT INTO {$table} (";
30 View Code Duplication
        foreach ($record as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
31
            
32
            $count--;
33
34
            if ($key === $this->getPrimaryKey($table)) {
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
35
                continue;
36
            }
37
38
            if ($count > 0) {
39
                $sql = $sql."{$key}, ";
40
            } else {
41
                $sql = $sql."{$key}) ";
42
            }
43
        }
44
45
        $count = count($record);
46
47
        $sql .= 'VALUES (';
48
        foreach ($record as $key => $value) {
49
50
            $count--;
51
52
            if ($key === $this->getPrimaryKey($table)) {
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
                continue;
54
            }
55
56
            if ($count > 0) {
57
                $sql = (empty($value)) ? $sql."NULL, " : $sql."'{$value}', ";
58
            } else {
59
                $sql = (empty($value)) ? $sql."NULL " : $sql."'{$value}') ";
60
            }
61
        }
62
63
        return $this->getPdo()->prepare($sql)->execute();
0 ignored issues
show
Bug introduced by
It seems like getPdo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
64
    }
65
66
    /**
67
     * Remove a record in the database.
68
     *
69
     * @param string $table The table where the record is removed in the database.
70
     * @param string $pk    The primary key value of the record.
71
     *
72
     * @return bool Returns boolean true if the record was successfully deleted or else it returns false.
73
     */
74 View Code Duplication
    public function deleteRecord($table, $pk)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        if (gettype($table) !== 'string') {
77
            throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead.");
78
        }
79
80
        return $this->getPdo()->prepare("DELETE FROM {$table}
0 ignored issues
show
Bug introduced by
It seems like getPdo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
81
                                            WHERE {$this->getPrimaryKey($table)}={$pk}")->execute();
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
    }
83
84
    /**
85
     * Returns a particular record in a table.
86
     *
87
     * @param string $table The table of the record. 
88
     * @param string $pk    The primary key value of the record.
89
     *
90
     * @return array An array containing the particular record.
91
     */
92 View Code Duplication
    public function findRecord($table, $pk)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        if (gettype($table) !== 'string') {
95
            throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead.");
96
        }
97
98
        return $this->getPdo()->query("SELECT * FROM {$table}
0 ignored issues
show
Bug introduced by
It seems like getPdo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
99
                                        WHERE {$this->getPrimaryKey($table)}={$pk}")->fetchAll();
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
100
    }
101
102
    /**
103
     * Returns all the records in a table.
104
     *
105
     * @param string $table The table inspected for all its records.
106
     *
107
     * @return array All the records in the table.        
108
     */
109 View Code Duplication
    public function getAllRecords($table)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        if (gettype($table) !== 'string') {
112
            throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead.");
113
        }
114
115
        return $this->getPdo()->query("SELECT * FROM {$table}")->fetchAll();
0 ignored issues
show
Bug introduced by
It seems like getPdo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
116
    }
117
118
    /**
119
     * Update a record in the database.
120
     *
121
     * @param string $table  The table where the record update is being made.
122
     * @param string $pk     The primary key value of the record to be updated.
123
     * @param array  $record The updates to be made to the record in the database.
124
     *
125
     * @return bool Returns boolean true if the record was successfully updated or else it returns false.
126
     */
127
    public function updateRecord($table, $pk, $record)
128
    {
129
        if (gettype($table) !== 'string') {
130
            throw new InvalidArgumentException("The parameter {$table} is not a string. A string is required instead.");
131
        }
132
133
        if (gettype($pk) !== 'string') {
134
            throw new InvalidArgumentException("The parameter {$pk} is not a string. A string is required instead.");
135
        }
136
137
        if (gettype($record) !== 'array') {
138
            throw new InvalidArgumentException("The parameter {$record} is not an array. An array is required instead.");
139
        }
140
141
        $count = count($record);
142
143
        $sql = "UPDATE {$table} SET ";
144 View Code Duplication
        foreach ($record as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
            if ($count > 1) {
146
                $sql = $sql."{$key}={$value}, ";
147
            } else {
148
                $sql = $sql."{$key}={$value} ";
149
            }
150
            $count--;
151
        }
152
        $sql .= "WHERE {$this->getPrimaryKey($table)}='{$pk}'";
0 ignored issues
show
Bug introduced by
It seems like getPrimaryKey() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
153
154
        return $this->getPdo()->prepare($sql)->execute();
0 ignored issues
show
Bug introduced by
It seems like getPdo() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
155
    }
156
}