Total Complexity | 44 |
Total Lines | 345 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Complex classes like Author 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 Author, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Author implements ArrayAccess |
||
12 | { |
||
13 | /** |
||
14 | * An associative array of information relating to this author where |
||
15 | * the keys map directly to the `tbl_authors` columns. |
||
16 | * @var array |
||
17 | */ |
||
18 | private $fields = []; |
||
19 | |||
20 | /** |
||
21 | * Stores a key => value pair into the Author object's `$this->fields` array. |
||
22 | * |
||
23 | * @param string $field |
||
24 | * Maps directly to a column in the `tbl_authors` table. |
||
25 | * @param string $value |
||
26 | * The value for the given $field |
||
27 | */ |
||
28 | public function set($field, $value) |
||
29 | { |
||
30 | $field = trim($field); |
||
31 | if ($value === null) { |
||
|
|||
32 | $this->fields[$field] = null; |
||
33 | } else { |
||
34 | $this->fields[$field] = trim($value); |
||
35 | } |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Retrieves the value from the Author object by field from `$this->fields` |
||
40 | * array. If field is omitted, all fields are returned. |
||
41 | * |
||
42 | * @param string $field |
||
43 | * Maps directly to a column in the `tbl_authors` table. Defaults to null |
||
44 | * @return mixed |
||
45 | * If the field is not set or is empty, returns null. |
||
46 | * If the field is not provided, returns the `$this->fields` array |
||
47 | * Otherwise returns a string. |
||
48 | */ |
||
49 | public function get($field = null) |
||
50 | { |
||
51 | if (is_null($field)) { |
||
52 | return $this->fields; |
||
53 | } |
||
54 | |||
55 | if (!isset($this->fields[$field]) || $this->fields[$field] == '') { |
||
56 | return null; |
||
57 | } |
||
58 | |||
59 | return $this->fields[$field]; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Given a field, remove it from `$this->fields` |
||
64 | * |
||
65 | * @since Symphony 2.2.1 |
||
66 | * @param string $field |
||
67 | * Maps directly to a column in the `tbl_authors` table. Defaults to null |
||
68 | */ |
||
69 | public function remove($field = null) |
||
70 | { |
||
71 | if (!is_null($field)) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | unset($this->fields[$field]); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Implementation of ArrayAccess::offsetExists() |
||
80 | * |
||
81 | * @param mixed $offset |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function offsetExists($offset) |
||
85 | { |
||
86 | return isset($this->fields[$offset]); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Implementation of ArrayAccess::offsetGet() |
||
91 | * |
||
92 | * @param mixed $offset |
||
93 | * @return mixed |
||
94 | */ |
||
95 | public function offsetGet($offset) |
||
96 | { |
||
97 | return $this->fields[$offset]; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Implementation of ArrayAccess::offsetSet() |
||
102 | * |
||
103 | * @param mixed $offset |
||
104 | * @param mixed $value |
||
105 | * @return void |
||
106 | */ |
||
107 | public function offsetSet($offset, $value) |
||
108 | { |
||
109 | $this->fields[$offset] = $value; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Implementation of ArrayAccess::offsetUnset() |
||
114 | * |
||
115 | * @param mixed $offset |
||
116 | * @return void |
||
117 | */ |
||
118 | public function offsetUnset($offset) |
||
119 | { |
||
120 | unset($this->fields[$offset]); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Sets all the fields values from the database for this extension. |
||
125 | * |
||
126 | * @param array $fields |
||
127 | * @return void |
||
128 | */ |
||
129 | public function setFields(array $fields) |
||
130 | { |
||
131 | $this->fields = $fields; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns boolean if the current Author is the original creator |
||
136 | * of this Symphony installation. |
||
137 | * |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function isPrimaryAccount() |
||
141 | { |
||
142 | return ($this->get('primary') === 'yes'); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Returns boolean if the current Author is of the developer |
||
147 | * user type. |
||
148 | * |
||
149 | * @return boolean |
||
150 | */ |
||
151 | public function isDeveloper() |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Returns boolean if the current Author is of the manager |
||
158 | * user type. |
||
159 | * |
||
160 | * @since 2.3.3 |
||
161 | * @return boolean |
||
162 | */ |
||
163 | public function isManager() |
||
164 | { |
||
165 | return ($this->get('user_type') == 'manager'); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns boolean if the current Author is of the author |
||
170 | * user type. |
||
171 | * |
||
172 | * @since 2.4 |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function isAuthor() |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Returns boolean if the current Author's authentication token is valid. |
||
182 | * |
||
183 | * @return boolean |
||
184 | */ |
||
185 | public function isTokenActive() |
||
186 | { |
||
187 | return !empty($this->get('auth_token')); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Returns the current Author's auth token. |
||
192 | * |
||
193 | * @return mixed |
||
194 | * The auth token or null |
||
195 | */ |
||
196 | public function getAuthToken() |
||
197 | { |
||
198 | return $this->get('auth_token'); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * A convenience method that returns an Authors full name |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getFullName() |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * Prior to saving an Author object, the validate function ensures that |
||
213 | * the values in `$this->fields` array are correct. As of Symphony 2.3 |
||
214 | * Authors must have unique username AND email address. This function returns |
||
215 | * boolean, with an `$errors` array provided by reference to the callee |
||
216 | * function. |
||
217 | * |
||
218 | * @param array $errors |
||
219 | * @return boolean |
||
220 | */ |
||
221 | public function validate(&$errors) |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * This is the insert method for the Author. This takes the current |
||
327 | * `$this->fields` values and adds them to the database using either the |
||
328 | * `AuthorManager::edit` or `AuthorManager::add` functions. An |
||
329 | * existing user is determined by if an ID is already set. |
||
330 | * When the database is updated successfully, the id of the author is set. |
||
331 | * |
||
332 | * @uses AuthorManager::add() |
||
333 | * @uses AuthorManager::edit() |
||
334 | * @return integer|boolean |
||
335 | * When a new Author is added or updated, an integer of the Author ID |
||
336 | * will be returned, otherwise false will be returned for a failed update. |
||
337 | */ |
||
338 | public function commit() |
||
359 |