Issues (1358)

modules/Polls/Polls.php (6 issues)

1
<?php
2
/**
3
 * @package  Polls
4
 * @category modules
5
 * @author   Nazar Mokrynskyi <[email protected]>
6
 * @license  0BSD
7
 */
8
namespace cs\modules\Polls;
9
use
10
	cs\Cache\Prefix,
11
	cs\Config,
12
	cs\CRUD_helpers,
13
	cs\Singleton;
14
15
/**
16
 * @method static $this instance($check = false)
17
 */
18
class Polls {
19
	use
20
		Common_actions,
21
		CRUD_helpers,
22
		Singleton;
23
	/**
24
	 * @var Prefix
25
	 */
26
	protected $cache;
27
	protected $data_model          = [
28
		'id'    => 'int',
29
		'title' => 'ml:string'
30
	];
31
	protected $table               = '[prefix]polls';
32
	protected $data_model_ml_group = 'Polls/polls';
33
34
	protected function construct () {
35
		$this->cache = new Prefix('Polls');
36
	}
37
	protected function cdb () {
38
		return Config::instance()->module('Polls')->db('polls');
39
	}
40
	/**
41
	 * Add new poll
42
	 *
43
	 * @param string $title
44
	 *
45
	 * @return false|int
46
	 */
47
	public function add ($title) {
48
		$id = $this->create($title);
0 ignored issues
show
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Polls\Polls::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
		$id = $this->create(/** @scrutinizer ignore-type */ $title);
Loading history...
49
		if ($id) {
50
			unset($this->cache->all);
0 ignored issues
show
Bug Best Practice introduced by
The property all does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
51
			return $id;
52
		}
53
		return false;
54
	}
55
	/**
56
	 * Get poll
57
	 *
58
	 * @param int|int[] $id
59
	 *
60
	 * @return array|array[]|false
61
	 */
62
	public function get ($id) {
63
		return $this->get_common($id);
64
	}
65
	/**
66
	 * Get ids of add polls
67
	 *
68
	 * @return false|int[]
69
	 */
70
	public function get_all () {
71
		return $this->cache->get(
72
			'all',
73
			function () {
74
				return $this->search([], 1, PHP_INT_MAX, 'id', false);
75
			}
76
		);
77
	}
78
	/**
79
	 * Set poll
80
	 *
81
	 * @param int    $id
82
	 * @param string $title
83
	 *
84
	 * @return bool
85
	 */
86
	public function set ($id, $title) {
87
		$id     = (int)$id;
88
		$result = $this->update($id, $title);
0 ignored issues
show
$id of type integer is incompatible with the type array expected by parameter $arguments of cs\modules\Polls\Polls::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$result = $this->update(/** @scrutinizer ignore-type */ $id, $title);
Loading history...
$title of type string is incompatible with the type array expected by parameter $arguments of cs\modules\Polls\Polls::update(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
		$result = $this->update($id, /** @scrutinizer ignore-type */ $title);
Loading history...
89
		if ($result) {
90
			unset(
91
				$this->cache->$id,
92
				$this->cache->all
0 ignored issues
show
Bug Best Practice introduced by
The property all does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
93
			);
94
			return true;
95
		}
96
		return false;
97
	}
98
	/**
99
	 * Del poll
100
	 *
101
	 * @param int $id
102
	 *
103
	 * @return bool
104
	 */
105
	public function del ($id) {
106
		$id = (int)$id;
107
		if (!$this->delete($id)) {
108
			return false;
109
		}
110
		$Options = Options::instance();
111
		$Options->del(
112
			$Options->get_all_for_poll($id) ?: []
113
		);
114
		if (!$this->db_prime()->q(
115
			"DELETE FROM `[prefix]polls_options_answers`
116
			WHERE `id` = $id"
117
		)
118
		) {
119
			return false;
120
		}
121
		unset(
122
			$this->cache->all,
0 ignored issues
show
Bug Best Practice introduced by
The property all does not exist on cs\Cache\Prefix. Since you implemented __get, consider adding a @property annotation.
Loading history...
123
			$this->cache->$id,
124
			$this->cache->{"options/poll/$id"}
125
		);
126
		return true;
127
	}
128
}
129