Total Complexity | 62 |
Total Lines | 312 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like PluginHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PluginHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class PluginHandler extends \XoopsPersistableObjectHandler |
||
34 | { |
||
35 | /** |
||
36 | * @var \XoopsMySQLDatabase |
||
37 | */ |
||
38 | public $db; |
||
39 | public $dbTable; |
||
40 | public $objClass = Plugin::class; |
||
41 | public $objKey = 'rssf_conf_id'; |
||
42 | public $sortby = 'rssf_order'; |
||
43 | public $order = 'ASC'; |
||
44 | /** |
||
45 | * @var \XoopsModules\Rssfit\Helper |
||
46 | */ |
||
47 | public $helper; |
||
48 | |||
49 | public function __construct(?\XoopsDatabase $db = null, ?Helper $helper = null) |
||
50 | { |
||
51 | if (null === $helper) { |
||
52 | $helper = \XoopsModules\Rssfit\Helper::getInstance(); |
||
53 | } |
||
54 | $this->helper = $helper; |
||
55 | |||
56 | if (null === $db) { |
||
57 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
58 | } |
||
59 | $this->db = $db; |
||
60 | // $this->dbTable = $db->prefix($helper->getDirname() . '_plugins'); |
||
61 | |||
62 | $table = $db->prefix($helper->getDirname() . '_plugins'); |
||
63 | $this->dbTable = $table; |
||
64 | |||
65 | parent::__construct($db, $table, Plugin::class, 'rssf_conf_id', 'rssf_filename'); |
||
66 | } |
||
67 | |||
68 | public function getInstance(?\XoopsDatabase $db = null): \XoopsPersistableObjectHandler |
||
69 | { |
||
70 | static $instance; |
||
71 | if (null === $instance) { |
||
72 | $instance = new static($db); |
||
73 | } |
||
74 | |||
75 | return $instance; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param bool $isNew |
||
80 | */ |
||
81 | public function create($isNew = true): ?\XoopsObject |
||
82 | { |
||
83 | $object = parent::create($isNew); |
||
84 | // if ($isNew) { |
||
85 | // $object->setDefaultPermissions(); |
||
86 | // } |
||
87 | $object->helper = $this->helper; |
||
88 | |||
89 | return $object; |
||
90 | } |
||
91 | |||
92 | // public function get($id, $fields = '*') |
||
93 | |||
94 | /** |
||
95 | * @param null|int $id |
||
96 | * @param null|array $fields |
||
97 | */ |
||
98 | public function get($id = null, $fields = null): ?\XoopsObject |
||
99 | { |
||
100 | $ret = null; |
||
101 | $criteria = new \Criteria($this->objKey, (int)$id); |
||
102 | $objs = $this->getObjects2($criteria); |
||
103 | if (\is_array($objs) && 1 === \count($objs)) { |
||
104 | $ret = &$objs[0]; |
||
105 | } |
||
106 | |||
107 | return $ret; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param bool $force flag to force the query execution despite security settings |
||
112 | * @return array|bool|int|mixed|null |
||
113 | */ |
||
114 | public function insert(\XoopsObject $object, $force = true) |
||
115 | { |
||
116 | if (mb_strtolower(\get_class($object)) != mb_strtolower($this->objClass)) { |
||
117 | return false; |
||
118 | } |
||
119 | if (!$object->isDirty()) { |
||
120 | return true; |
||
121 | } |
||
122 | if (!$object->cleanVars()) { |
||
123 | return false; |
||
124 | } |
||
125 | foreach ($object->cleanVars as $k => $v) { |
||
126 | if (\XOBJ_DTYPE_INT == $object->vars[$k]['data_type']) { |
||
127 | $cleanvars[$k] = (int)$v; |
||
128 | } else { |
||
129 | $cleanvars[$k] = $this->db->quoteString($v); |
||
130 | } |
||
131 | } |
||
132 | if (\count($object->getErrors()) > 0) { |
||
133 | return false; |
||
134 | } |
||
135 | if (empty($cleanvars[$this->objKey]) || $object->isNew()) { |
||
136 | $cleanvars[$this->objKey] = $this->db->genId($this->dbTable . '_' . $this->objKey . '_seq'); |
||
137 | $sql = 'INSERT INTO ' . $this->dbTable . ' (' . \implode(',', \array_keys($cleanvars)) . ') VALUES (' . \implode(',', $cleanvars) . ')'; |
||
|
|||
138 | } else { |
||
139 | unset($cleanvars[$this->objKey]); |
||
140 | $sql = 'UPDATE ' . $this->dbTable . ' SET'; |
||
141 | foreach ($cleanvars as $k => $v) { |
||
142 | $sql .= ' ' . $k . '=' . $v . ','; |
||
143 | } |
||
144 | $sql = mb_substr($sql, 0, -1); |
||
145 | $sql .= ' WHERE ' . $this->objKey . ' = ' . $object->getVar($this->objKey); |
||
146 | } |
||
147 | if ($force) { |
||
148 | $result = $this->db->queryF($sql); |
||
149 | } else { |
||
150 | $result = $this->db->query($sql); |
||
151 | } |
||
152 | if (!$result) { |
||
153 | $object->setErrors('Could not store data in the database.<br>' . $this->db->error() . ' (' . $this->db->errno() . ')<br>' . $sql); |
||
154 | |||
155 | return false; |
||
156 | } |
||
157 | // if (false === $object->getVar($this->objKey)) { |
||
158 | if (0 === (int)$object->getVar($this->objKey)) { |
||
159 | $object->assignVar($this->objKey, $this->db->getInsertId()); |
||
160 | } |
||
161 | |||
162 | return $object->getVar($this->objKey); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param bool $force |
||
167 | */ |
||
168 | public function delete(\XoopsObject $object, $force = false): bool |
||
169 | { |
||
170 | if (mb_strtolower(\get_class($object)) != mb_strtolower($this->objClass)) { |
||
171 | return false; |
||
172 | } |
||
173 | $sql = 'DELETE FROM ' . $this->dbTable . ' WHERE ' . $this->objKey . '=' . $object->getVar($this->objKey); |
||
174 | if ($force) { |
||
175 | $result = $this->db->queryF($sql); |
||
176 | } else { |
||
177 | $result = $this->db->query($sql); |
||
178 | } |
||
179 | if (!$result) { |
||
180 | return false; |
||
181 | } |
||
182 | |||
183 | return true; |
||
184 | } |
||
185 | |||
186 | /** |
||
187 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
188 | */ |
||
189 | public function getObjects2($criteria = null, string $fields = '*', string $key = ''): ?array |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
238 | */ |
||
239 | public function modifyObjects($criteria = null, array $fields = [], bool $force = false): ?string |
||
240 | { |
||
241 | if (\count($fields) > 0) { |
||
242 | $object = new $this->objClass(); |
||
243 | $sql = ''; |
||
244 | foreach ($fields as $k => $v) { |
||
245 | $sql .= $k . ' = '; |
||
246 | $sql .= 3 == $object->vars[$k]['data_type'] ? (int)$v : $this->db->quoteString($v); |
||
247 | $sql .= ', '; |
||
248 | } |
||
249 | $sql = mb_substr($sql, 0, -2); |
||
250 | $sql = 'UPDATE ' . $this->dbTable . ' SET ' . $sql; |
||
251 | if (($criteria instanceof \CriteriaCompo) || ($criteria instanceof \Criteria)) { |
||
252 | $sql .= ' ' . $criteria->renderWhere(); |
||
253 | } |
||
254 | if ($force) { |
||
255 | $result = $this->db->queryF($sql); |
||
256 | } else { |
||
257 | $result = $this->db->query($sql); |
||
258 | } |
||
259 | if (!$result) { |
||
260 | return 'Could not store data in the database.<br>' . $this->db->error() . ' (' . $this->db->errno() . ')<br>' . $sql; |
||
261 | } |
||
262 | } |
||
263 | |||
264 | return null; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * count objects matching a condition |
||
269 | * @param null|\Criteria|\CriteriaCompo $criteria |
||
270 | */ |
||
271 | public function getCount($criteria = null): ?int |
||
284 | } |
||
285 | |||
286 | public function forceDeactivate(\XoopsObject $object, string $type = 'rssf_activated'): bool |
||
293 | } |
||
294 | |||
295 | public function &getPluginFileList(): ?array |
||
307 | } |
||
308 | |||
309 | /** |
||
310 | * @return null|mixed |
||
311 | */ |
||
312 | public function checkPlugin(\XoopsObject $object) |
||
345 | } |
||
346 | } |
||
347 |