Completed
Push — development ( 86ad30...7b6aa4 )
by Sebastian
05:00
created

News::deleteNews()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 8
Code Lines 7

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
dl 8
loc 8
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 7
nc 3
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 4 and the first side effect is on line 2.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
class News extends Base {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
5
  protected $table = 'news';
6
7
  /**
8
   * We allow changing the database for shared accounts across pools
9
   * Load the config on construct so we can assign the DB name
10
   * @param config array MPOS configuration
11
   * @return none
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
12
   **/
13
  public function __construct($config) {
14
    $this->setConfig($config);
15
    $this->table = $this->config['db']['shared']['news'] . '.' . $this->table;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
16
  }
17
18
  /**
19
   * Get activation status of post
20
   * @param id int News ID
21
   * @return bool true or false
22
   **/
23
  public function getActive($id) {
24
    $this->debug->append("STA " . __METHOD__, 5);
25
    return $this->getSingle($id, 'active', 'id');
26
  }
27
28
  /**
29
   * Switch activation status
30
   * @param id int News ID
31
   * @return bool true or false
32
   **/
33
  public function toggleActive($id) {
34
    $this->debug->append("STA " . __METHOD__, 5);
35
    $field = array('name' => 'active', 'type' => 'i', 'value' => !$this->getActive($id));
36
    return $this->updateSingle($id, $field);
0 ignored issues
show
Documentation introduced by
$field is of type array<string,string|bool...ng","value":"boolean"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
  }
38
39
  /**
40
   * Get all active news
41
   **/
42 View Code Duplication
  public function getAllActive() {
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...
43
    $this->debug->append("STA " . __METHOD__, 4);
44
    $stmt = $this->mysqli->prepare("SELECT n.*, a.username AS author FROM $this->table AS n LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = n.account_id WHERE active = 1 ORDER BY time DESC");
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
    if ($stmt && $stmt->execute() && $result = $stmt->get_result())
46
      return $result->fetch_all(MYSQLI_ASSOC);
47
    return $this->sqlError('E0040');
48
  }
49
50
  /**
51
   * Get all news
52
   **/
53 View Code Duplication
  public function getAll() {
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...
54
    $this->debug->append("STA " . __METHOD__, 4);
55
    $stmt = $this->mysqli->prepare("SELECT n.*, a.username AS author FROM $this->table AS n LEFT JOIN " . $this->user->getTableName() . " AS a ON a.id = n.account_id ORDER BY time DESC");
56
    if ($stmt && $stmt->execute() && $result = $stmt->get_result())
57
      return $result->fetch_all(MYSQLI_ASSOC);
58
    return $this->sqlError('E0039');
59
  }
60
61
  /**
62
   * Get a specific news entry
63
   **/
64
  public function getEntry($id) {
65
    $this->debug->append("STA " . __METHOD__, 4);
66
    $stmt = $this->mysqli->prepare("SELECT * FROM $this->table WHERE id = ?");
67
    if ($stmt && $stmt->bind_param('i', $id) && $stmt->execute() && $result = $stmt->get_result())
68
      return $result->fetch_assoc();
69
    return $this->sqlError('E0038');
70
  }
71
72
  /**
73
   * Update a news entry
74
   **/
75
  public function updateNews($id, $header, $content, $active=0) {
76
    $this->debug->append("STA " . __METHOD__, 4);
77
    $stmt = $this->mysqli->prepare("UPDATE $this->table SET content = ?, header = ?, active = ? WHERE id = ?");
78
    if ($stmt && $stmt->bind_param('ssii', $content, $header, $active, $id) && $stmt->execute() && $stmt->affected_rows == 1)
79
      return true;
80
    return $this->sqlError('E0037');
81
  }
82
83 View Code Duplication
  public function deleteNews($id) {
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...
84
    $this->debug->append("STA " . __METHOD__, 4);
85
    if (!is_int($id)) return false;
86
    $stmt = $this->mysqli->prepare("DELETE FROM $this->table WHERE id = ?");
87
    if ($this->checkStmt($stmt) && $stmt->bind_param('i', $id) && $stmt->execute() && $stmt->affected_rows == 1)
88
      return true;
89
    return $this->sqlError('E0036');
90
  }
91
92
  /**
93
   * Add a new mews entry to the table
94
   * @param type string Type of the notification
95
   * @return bool
96
   **/
97
  public function addNews($account_id, $aData, $active=false) {
98
    $this->debug->append("STA " . __METHOD__, 4);
99
    if (empty($aData['header'])) return false;
100
    if (empty($aData['content'])) return false;
101
    if (!is_int($account_id)) return false;
102
    if (@$aData['active']) $active = true;
103
    $stmt = $this->mysqli->prepare("INSERT INTO $this->table (account_id, header, content, active) VALUES (?,?,?,?)");
104
    if ($stmt && $stmt->bind_param('issi', $account_id, $aData['header'], $aData['content'], $active) && $stmt->execute())
105
      return true;
106
    return $this->sqlError('E0035');
107
  }
108
}
109
110
$news = new News($config);
111
$news->setDebug($debug);
112
$news->setMysql($mysqli);
113
$news->setUser($user);
114
$news->setErrorCodes($aErrorCodes);
115