Skip to content

Commit

Permalink
Fix: refatorando funções com complexidades acima de 5
Browse files Browse the repository at this point in the history
  • Loading branch information
lucastgama committed Aug 12, 2024
1 parent cb00a4c commit c402dbd
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 59 deletions.
64 changes: 5 additions & 59 deletions src/includes/checkout/CheckoutGateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace VindiPaymentGateways;

use Exception;
use VindiPaymentGateways\GenerateUser;


class CheckoutGateways
{
Expand All @@ -13,54 +15,6 @@ public function __construct()
add_action('woocommerce_before_pay_action', [$this, 'save_billing_fields'], 10, 1);
}

private function auto_create_user_for_order($order)
{
$billing_email = $order->get_billing_email();
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();

$user_id = $this->get_or_create_user($billing_email, $billing_first_name, $billing_last_name);

if ($user_id) {
$order->set_customer_id($user_id);
$order->save();
}
}

private function get_or_create_user($billing_email, $billing_first_name, $billing_last_name)
{
$user_id = $this->set_user_id($billing_email);
if ($user_id) {
return $user_id;
}

$username = $this->generate_unique_username($billing_first_name, $billing_last_name, $billing_email);
$password = wp_generate_password(12, false);
$user_id = wp_create_user($username, $password, $billing_email);
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);

return $user_id;
}

private function generate_unique_username($billing_first_name, $billing_last_name, $billing_email)
{
$username = strtolower($billing_first_name . '_' . $billing_last_name);
$username = str_replace(' ', '_', $username);
$suffix = 1;

while (username_exists($username)) {
$username = strtolower($billing_first_name . '_' . $billing_last_name . '_' . $suffix);
$suffix++;
}

if (!isset($username) || empty($username)) {
$username = current(explode('@', $billing_email));
}

return $username;
}

public function filter_checkout_gateways($gateways)
{
$available = [
Expand All @@ -86,7 +40,7 @@ public function filter_checkout_gateways($gateways)
return $gateways;
}

private function clear_session($hasSessionParams,$gateways)
private function clear_session($hasSessionParams, $gateways)
{
if ($hasSessionParams && !$this->is_subscription_context()) {
WC()->session->set('vindi-payment-link', '');
Expand Down Expand Up @@ -171,7 +125,8 @@ public function save_billing_fields($order)
$this->validate_required_fields();
$this->update_billing_fields($order, $fields);
$order->save();
$this->auto_create_user_for_order($order);
$generate_user = new GenerateUser();
$generate_user->auto_create_user_for_order($order);
} catch (Exception $err) {
wc_add_notice($err->getMessage(), 'error');
}
Expand Down Expand Up @@ -272,13 +227,4 @@ public function validate_person_type()
throw new Exception((__('CNPJ', 'vindi-payment-gateway')));
}
}

private function set_user_id($userEmail)
{
$user = get_user_by('email', $userEmail);
if ($user) {
return $user->ID;
}
return null;
}
}
63 changes: 63 additions & 0 deletions src/utils/GenerateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace VindiPaymentGateways;

class GenerateUser
{
public function auto_create_user_for_order($order)
{
$billing_email = $order->get_billing_email();
$billing_first_name = $order->get_billing_first_name();
$billing_last_name = $order->get_billing_last_name();

$user_id = $this->get_or_create_user($billing_email, $billing_first_name, $billing_last_name);

if ($user_id) {
$order->set_customer_id($user_id);
$order->save();
}
}

private function get_or_create_user($billing_email, $billing_first_name, $billing_last_name)
{
$user_id = $this->set_user_id($billing_email);
if ($user_id) {
return $user_id;
}

$username = $this->generate_unique_username($billing_first_name, $billing_last_name, $billing_email);
$password = wp_generate_password(12, false);
$user_id = wp_create_user($username, $password, $billing_email);
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);

return $user_id;
}

private function set_user_id($userEmail)
{
$user = get_user_by('email', $userEmail);
if ($user) {
return $user->ID;
}
return null;
}

private function generate_unique_username($billing_first_name, $billing_last_name, $billing_email)
{
$username = strtolower($billing_first_name . '_' . $billing_last_name);
$username = str_replace(' ', '_', $username);
$suffix = 1;

while (username_exists($username)) {
$username = strtolower($billing_first_name . '_' . $billing_last_name . '_' . $suffix);
$suffix++;
}

if (!isset($username) || empty($username)) {
$username = current(explode('@', $billing_email));
}

return $username;
}
}

0 comments on commit c402dbd

Please sign in to comment.