Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 22 | class User_Class extends Object_Class { |
||
| 23 | /** |
||
| 24 | * Le nom du modèle |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $model_name = '\eoxia\User_Model'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * La clé principale pour post_meta |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $meta_key = '_wpeo_user'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Utiles pour récupérer la clé unique |
||
| 39 | * |
||
| 40 | * @todo Rien à faire ici |
||
| 41 | * @todo Expliquer la documentation |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $identifier_helper = 'user'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * User element type |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $type = 'user'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Utiles pour DigiRisk |
||
| 56 | * |
||
| 57 | * @todo Rien à faire ici |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | public $element_prefix = 'U'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * La liste des droits a avoir pour accèder aux différentes méthodes |
||
| 64 | * |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $capabilities = array( |
||
| 68 | 'get' => 'list_users', |
||
| 69 | 'put' => 'edit_users', |
||
| 70 | 'post' => 'edit_users', |
||
| 71 | 'delete' => 'delete_users', |
||
| 72 | ); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Slug de base pour la route dans l'api rest |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $base = 'user'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Récupères les données selon le modèle définis. |
||
| 83 | * |
||
| 84 | * @since 0.1.0 |
||
| 85 | * @version 1.0.0 |
||
| 86 | * |
||
| 87 | * @param array $args Les paramètres de WP_User_Query @see https://codex.wordpress.org/Class_Reference/WP_User_Query. |
||
| 88 | * @param boolean $single Si on veut récupérer un tableau, ou qu'une seule entrée. |
||
| 89 | * |
||
| 90 | * @return Comment_Model |
||
| 91 | */ |
||
| 92 | public function get( $args = array(), $single = false ) { |
||
| 93 | $array_users = array(); |
||
| 94 | |||
| 95 | if ( ! empty( $args['id'] ) ) { |
||
| 96 | if ( ! isset( $args['include'] ) ) { |
||
| 97 | $args['include'] = array(); |
||
| 98 | } |
||
| 99 | $args['include'] = array_merge( (array) $args['id'], $args['include'] ); |
||
| 100 | unset( $args['id'] ); |
||
| 101 | } elseif ( isset( $args['id'] ) ) { |
||
| 102 | $args['schema'] = true; |
||
| 103 | } |
||
| 104 | |||
| 105 | $args = apply_filters( 'eo_model_user_before_get', $args ); |
||
| 106 | |||
| 107 | if ( isset( $args['schema'] ) ) { |
||
| 108 | $array_users[] = $args; |
||
| 109 | } else { |
||
| 110 | $array_users = get_users( $args ); |
||
| 111 | } |
||
| 112 | |||
| 113 | // Traitement de la liste des résultats pour le retour. |
||
| 114 | $array_users = $this->prepare_items_for_response( $array_users, 'user', $this->meta_key, 'ID' ); |
||
| 115 | |||
| 116 | if ( true === $single && 1 === count( $array_users ) ) { |
||
| 117 | $array_users = $array_users[0]; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $array_users; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Insère ou met à jour les données dans la base de donnée. |
||
| 125 | * |
||
| 126 | * @since 0.1.0 |
||
| 127 | * @version 1.0.0 |
||
| 128 | * |
||
| 129 | * @param Array $data Les données a insérer ou à mêttre à jour. |
||
| 130 | * @return Object L'objet construit grâce au modèle. |
||
| 131 | */ |
||
| 132 | public function update( $data ) { |
||
| 133 | $model_name = $this->model_name; |
||
| 134 | $data = (array) $data; |
||
| 135 | $req_method = ( ! empty( $data['id'] ) ) ? 'put' : 'post'; |
||
| 136 | $args_cb = array( |
||
| 137 | 'model_name' => $model_name, |
||
| 138 | 'req_method' => $req_method, |
||
| 139 | 'meta_key' => $this->meta_key, |
||
| 140 | ); |
||
| 141 | |||
| 142 | if ( 'post' === $req_method ) { |
||
| 143 | while ( username_exists( $data['login'] ) ) { |
||
| 144 | $data['login'] .= wp_rand( 1000, 9999 ); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if ( ! empty( $data['id'] ) ) { |
||
| 149 | $current_data = $this->get( array( |
||
| 150 | 'id' => $data['id'], |
||
| 151 | ), true ); |
||
| 152 | $data = Array_Util::g()->recursive_wp_parse_args( $data, $current_data->data ); |
||
| 153 | } |
||
| 154 | |||
| 155 | $data = apply_filters( 'eo_model_user_before_' . $req_method, $data, $args_cb ); |
||
| 156 | |||
| 157 | $args_cb['data'] = $data; |
||
| 158 | |||
| 159 | $object = new $model_name( $data, $req_method ); |
||
| 160 | |||
| 161 | if ( empty( $object->data['id'] ) ) { |
||
| 162 | $inserted_user = wp_insert_user( $object->convert_to_wordpress() ); |
||
| 163 | if ( is_wp_error( $inserted_user ) ) { |
||
| 164 | return $inserted_user; |
||
| 165 | } |
||
| 166 | |||
| 167 | $object->data['id'] = $inserted_user; |
||
| 168 | } else { |
||
| 169 | |||
| 170 | $updated_user = wp_update_user( $object->convert_to_wordpress() ); |
||
| 171 | if ( is_wp_error( $updated_user ) ) { |
||
| 172 | return $updated_user; |
||
| 173 | } |
||
| 174 | |||
| 175 | $object->data['id'] = $updated_user; |
||
| 176 | } |
||
| 177 | |||
| 178 | $object = apply_filters( 'eo_model_user_after_' . $req_method, $object, $args_cb ); |
||
| 179 | |||
| 180 | return $object; |
||
| 181 | } |
||
| 182 | |||
| 183 | } |
||
| 184 | } // End if(). |
||
| 185 |