sarga/packages/Webkul/API/Http/Controllers/Shop/CustomerController.php

117 lines
3.2 KiB
PHP
Raw Normal View History

2019-05-07 11:36:21 +00:00
<?php
namespace Webkul\API\Http\Controllers\Shop;
2020-08-07 18:38:14 +00:00
use Illuminate\Support\Facades\Auth;
2019-05-07 11:36:21 +00:00
use Illuminate\Support\Facades\Event;
2021-06-28 05:14:03 +00:00
use Webkul\Customer\Http\Requests\CustomerRegistrationRequest;
use Webkul\Customer\Repositories\CustomerGroupRepository;
2021-06-28 05:14:03 +00:00
use Webkul\Customer\Repositories\CustomerRepository;
2019-05-07 11:36:21 +00:00
class CustomerController extends Controller
{
2020-08-07 18:38:14 +00:00
/**
* Contains current guard
*
* @var array
*/
protected $guard;
2019-05-07 11:36:21 +00:00
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Repository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Customer\Repositories\CustomerRepository
2019-05-07 11:36:21 +00:00
*/
protected $customerRepository;
/**
* Repository object
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\Customer\Repositories\CustomerGroupRepository
2019-05-07 11:36:21 +00:00
*/
protected $customerGroupRepository;
/**
* Create a new controller instance.
*
2020-03-05 14:19:14 +00:00
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
2020-03-05 05:34:57 +00:00
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository
* @return void
*/
public function __construct(
CustomerRepository $customerRepository,
CustomerGroupRepository $customerGroupRepository
) {
2020-08-07 18:38:14 +00:00
$this->guard = request()->has('token') ? 'api' : 'customer';
2019-05-07 11:36:21 +00:00
$this->_config = request('_config');
2020-08-07 18:38:14 +00:00
if (isset($this->_config['authorization_required']) && $this->_config['authorization_required']) {
auth()->setDefaultDriver($this->guard);
$this->middleware('auth:' . $this->guard);
}
2019-05-07 11:36:21 +00:00
$this->customerRepository = $customerRepository;
$this->customerGroupRepository = $customerGroupRepository;
2019-05-07 11:36:21 +00:00
}
/**
* Method to store user's sign up form data to DB.
*
2020-03-05 05:34:57 +00:00
* @return \Illuminate\Http\Response
2019-05-07 11:36:21 +00:00
*/
2021-06-28 05:14:03 +00:00
public function create(CustomerRegistrationRequest $request)
2019-05-07 11:36:21 +00:00
{
2021-06-28 05:14:03 +00:00
$request->validated();
2019-05-07 11:36:21 +00:00
2020-10-05 14:03:57 +00:00
$data = [
'first_name' => $request->get('first_name'),
'last_name' => $request->get('last_name'),
'email' => $request->get('email'),
'password' => $request->get('password'),
'password' => bcrypt($request->get('password')),
2020-10-05 14:03:57 +00:00
'channel_id' => core()->getCurrentChannel()->id,
'is_verified' => 1,
'customer_group_id' => $this->customerGroupRepository->findOneWhere(['code' => 'general'])->id
];
2019-12-24 14:01:13 +00:00
Event::dispatch('customer.registration.before');
2019-05-07 11:36:21 +00:00
$customer = $this->customerRepository->create($data);
2019-12-24 14:01:13 +00:00
Event::dispatch('customer.registration.after', $customer);
2019-05-07 11:36:21 +00:00
return response()->json([
2020-03-04 06:32:53 +00:00
'message' => 'Your account has been created successfully.',
2020-02-27 08:03:03 +00:00
]);
2019-05-07 11:36:21 +00:00
}
2020-08-07 18:38:14 +00:00
/**
* Returns a current user data.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function get($id)
{
if (Auth::user($this->guard)->id === (int) $id) {
return new $this->_config['resource'](
$this->customerRepository->findOrFail($id)
);
}
return response()->json([
'message' => 'Invalid Request.',
], 403);
}
2019-05-07 11:36:21 +00:00
}