Completed
Push — master ( df8ec4...96358d )
by Nazar
04:25
created

Common_actions   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 94
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A get_common() 0 16 3
A get_all_common() 0 11 2
A add_common() 0 7 2
A set_common() 0 12 2
A del_common() 0 11 2
1
<?php
2
/**
3
 * @package   Shop
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2015, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Shop;
10
use
11
	cs\CRUD,
12
	cs\Language;
13
/**
14
 * @property \cs\Cache\Prefix $cache
15
 * @property string           $table
16
 */
17
trait Common_actions {
18
	use
19
		CRUD;
20
	/**
21
	 * Get item
22
	 *
23
	 * @param int|int[] $id
24
	 *
25
	 * @return array|false
26
	 */
27
	protected function get_common ($id) {
28
		if (is_array($id)) {
29
			foreach ($id as &$i) {
30
				$i = $this->get_common($i);
31
			}
32
			return $id;
33
		}
34
		$L  = Language::instance();
35
		$id = (int)$id;
36
		return $this->cache->get(
37
			"$id/$L->clang",
38
			function () use ($id) {
39
				return $this->read($id);
40
			}
41
		);
42
	}
43
	/**
44
	 * Get array of all items
45
	 *
46
	 * @return int[] Array of attributes ids
47
	 */
48
	protected function get_all_common () {
49
		return $this->cache->get(
50
			'all',
51
			function () {
52
				return $this->db()->qfas(
53
					"SELECT `id`
54
				FROM `$this->table`"
55
				) ?: [];
56
			}
57
		);
58
	}
59
	/**
60
	 * Add new item
61
	 *
62
	 * @param array $arguments
63
	 *
64
	 * @return false|int Id of created item on success of <b>false</> on failure
1 ignored issue
show
Documentation introduced by
Should the return type not be integer|string|false?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
65
	 */
66
	protected function add_common ($arguments) {
67
		$id = $this->create($arguments);
68
		if ($id) {
69
			unset($this->cache->all);
70
		}
71
		return $id;
72
	}
73
	/**
74
	 * Set data of specified item
75
	 *
76
	 * @param array $arguments
77
	 *
78
	 * @return bool
79
	 */
80
	protected function set_common ($arguments) {
81
		$result = $this->update($arguments);
82
		if ($result) {
83
			$L  = Language::instance();
84
			$id = (int)$arguments[0];
85
			unset(
86
				$this->cache->{"$id/$L->clang"},
87
				$this->cache->all
88
			);
89
		}
90
		return $result;
91
	}
92
	/**
93
	 * Delete specified item
94
	 *
95
	 * @param int $id
96
	 *
97
	 * @return bool
98
	 */
99
	protected function del_common ($id) {
100
		$id     = (int)$id;
101
		$result = $this->delete($id);
102
		if ($result) {
103
			unset(
104
				$this->cache->$id,
105
				$this->cache->all
106
			);
107
		}
108
		return $result;
109
	}
110
}
111