| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Api; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentCart\Api\Validator\CustomerAddressValidator; |
| 7 |
use FluentCart\App\Helpers\CustomerHelper; |
| 8 |
use FluentCart\App\Models\CustomerAddresses; |
| 9 |
use FluentCart\Framework\Support\Arr; |
| 10 |
use FluentCart\Framework\Validator\ValidationException; |
| 11 |
|
| 12 |
class CustomerAddress |
| 13 |
{ |
| 14 |
/** |
| 15 |
* @var CustomerHelper $customerHelper An instance of the CustomerHelper service |
| 16 |
*/ |
| 17 |
private $customerHelper; |
| 18 |
|
| 19 |
public function __construct() |
| 20 |
{ |
| 21 |
$this->customerHelper = (new CustomerHelper()); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* @return null|ValidationException |
| 27 |
* |
| 28 |
* @param array $args, which contain customers data like |
| 29 |
* first_name, last_name, email, city, state, postcode, country |
| 30 |
*/ |
| 31 |
public function validate($args) |
| 32 |
{ |
| 33 |
try { |
| 34 |
(new CustomerAddressValidator())->validate($args); |
| 35 |
} catch (ValidationException $e) { |
| 36 |
throw $e; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
/** |
| 42 |
* @return array |
| 43 |
* |
| 44 |
* create new customer address |
| 45 |
* |
| 46 |
* @param string $id will be the address id |
| 47 |
* @param array $data will be the customer info |
| 48 |
*/ |
| 49 |
public function create($id, $data) |
| 50 |
{ |
| 51 |
|
| 52 |
$address = Arr::get($data, 'address'); |
| 53 |
|
| 54 |
$this->validate($address); |
| 55 |
|
| 56 |
foreach ($address as $key => $val) { |
| 57 |
$address[$key] = sanitize_text_field($val); |
| 58 |
} |
| 59 |
|
| 60 |
$hasPrimary = CustomerAddresses::query()->where('customer_id', $id) |
| 61 |
->where('type', Arr::get($address, 'type')) |
| 62 |
->where('is_primary', '1') |
| 63 |
->count(); |
| 64 |
|
| 65 |
if (!$hasPrimary) { |
| 66 |
$address['is_primary'] = '1'; |
| 67 |
} |
| 68 |
|
| 69 |
$address['customer_id'] = intval($id); |
| 70 |
|
| 71 |
return CustomerAddresses::create($address); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* @return array |
| 77 |
* |
| 78 |
* Update any existing customer address info |
| 79 |
* |
| 80 |
* @param array $data , valid address details with fillable infos |
| 81 |
*/ |
| 82 |
public function update($data) |
| 83 |
{ |
| 84 |
$this->validate(Arr::get($data, 'address')); |
| 85 |
|
| 86 |
$addressId = Arr::get($data, 'address.id', false); |
| 87 |
|
| 88 |
if (!$addressId) { |
| 89 |
throw new Exception(esc_html__('Please edit a valid address!', 'fluent-cart')); |
| 90 |
} |
| 91 |
|
| 92 |
$address = CustomerAddresses::query()->findOrFail($addressId); |
| 93 |
if (!$address) { |
| 94 |
throw new Exception(esc_html__('Address not found, please reload the page and try again!', 'fluent-cart')); |
| 95 |
} |
| 96 |
|
| 97 |
$data = Arr::get($data, 'address'); |
| 98 |
foreach ($data as $key => $val) { |
| 99 |
$data[$key] = sanitize_text_field($val); |
| 100 |
} |
| 101 |
|
| 102 |
$address->update($data); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* @return Boolean |
| 108 |
* |
| 109 |
* Delete existing customer or customers |
| 110 |
* |
| 111 |
* @param string|Int $id , customer address id |
| 112 |
*/ |
| 113 |
public function delete($id) |
| 114 |
{ |
| 115 |
if (!$id) { |
| 116 |
throw new Exception(esc_html__('Please use a valid address ID!', 'fluent-cart')); |
| 117 |
} |
| 118 |
return CustomerAddresses::query()->where('id', $id)->delete(); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* @return array |
| 124 |
* |
| 125 |
* Will return info of specific customer |
| 126 |
* |
| 127 |
* @param string|Number $id get data by this customer id |
| 128 |
* @param array $type accepts a type of address like 'shipping', 'billing' |
| 129 |
*/ |
| 130 |
public function get($id, $type = 'billing') |
| 131 |
{ |
| 132 |
$addresses = CustomerAddresses::search([ 'customer_id' => [ 'column' => 'customer_id', 'operator' => '=', 'value' => $id ] ]); |
| 133 |
if ($type) { |
| 134 |
$addresses->search([ 'type' => [ 'column' => 'type', 'operator' => '=', 'value' => $type ] ]); |
| 135 |
} |
| 136 |
return $addresses->orderBy('is_primary', 'DESC')->get()->toArray(); |
| 137 |
} |
| 138 |
|
| 139 |
public function getAddress($id, $type = 'billing') |
| 140 |
{ |
| 141 |
$all = $this->get($id, $type); |
| 142 |
|
| 143 |
if (empty($all)) { |
| 144 |
return []; |
| 145 |
} |
| 146 |
|
| 147 |
$firstItem = Arr::first($all); |
| 148 |
return Arr::only($firstItem, [ |
| 149 |
'address_1', |
| 150 |
'address_2', |
| 151 |
'city', |
| 152 |
'state', |
| 153 |
'postcode', |
| 154 |
'country' |
| 155 |
]); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* @return string message |
| 162 |
* |
| 163 |
* @param array $data, accepts 'id' of address and 'type' of address |
| 164 |
* |
| 165 |
* possible type, 'billing', 'shipping' |
| 166 |
*/ |
| 167 |
public function makePrimary($customerId, $data) |
| 168 |
{ |
| 169 |
$addressId = Arr::get($data, 'address.id'); |
| 170 |
$type = Arr::get($data, 'address.type'); |
| 171 |
|
| 172 |
CustomerAddresses::query()->where('customer_id', $customerId) |
| 173 |
->where('type', $type) |
| 174 |
->update(array('is_primary' => '0')); |
| 175 |
|
| 176 |
return CustomerAddresses::query()->where('id', $addressId)->update(array('is_primary' => '1')); |
| 177 |
} |
| 178 |
|
| 179 |
} |
| 180 |
|