2019-06-06 06:51:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-06-06 06:51:28 +02:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Repositories;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2019-11-21 09:38:57 +01:00
|
|
|
use App\Models\CompanyUser;
|
|
|
|
use App\Factory\CompanyUserFactory;
|
2019-06-06 06:51:28 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UserRepository
|
|
|
|
*/
|
|
|
|
class UserRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the class name.
|
|
|
|
*
|
|
|
|
* @return string The class name.
|
|
|
|
*/
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return User::class;
|
|
|
|
}
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
/**
|
2019-06-06 06:51:28 +02:00
|
|
|
* Saves the user and its contacts
|
|
|
|
*
|
|
|
|
* @param array $data The data
|
|
|
|
* @param \App\Models\user $user The user
|
|
|
|
*
|
|
|
|
* @return user|\App\Models\user|null user Object
|
|
|
|
*/
|
2019-12-08 11:28:52 +01:00
|
|
|
public function save(array $data, User $user) : ?User
|
2019-12-30 22:59:12 +01:00
|
|
|
{
|
2019-06-06 06:51:28 +02:00
|
|
|
$user->fill($data);
|
|
|
|
$user->save();
|
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
if (isset($data['company_user'])) {
|
2019-11-21 09:38:57 +01:00
|
|
|
$company = auth()->user()->company();
|
|
|
|
$account_id = $company->account->id;
|
|
|
|
|
|
|
|
$cu = CompanyUser::whereUserId($user->id)->whereCompanyId($company->id)->first();
|
|
|
|
|
2019-12-04 02:06:14 +01:00
|
|
|
/*No company user exists - attach the user*/
|
2019-12-30 22:59:12 +01:00
|
|
|
if (!$cu) {
|
|
|
|
$data['company_user']['account_id'] = $account_id;
|
2019-11-22 22:10:53 +01:00
|
|
|
$user->companies()->attach($company->id, $data['company_user']);
|
2019-12-30 22:59:12 +01:00
|
|
|
} else {
|
2019-12-04 02:06:14 +01:00
|
|
|
$cu->fill($data['company_user']);
|
|
|
|
$cu->save();
|
|
|
|
}
|
2019-11-21 09:38:57 +01:00
|
|
|
}
|
|
|
|
|
2019-06-06 06:51:28 +02:00
|
|
|
return $user;
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|
|
|
|
}
|