Documentation for SettingsSaver trait

This commit is contained in:
David Bomba 2019-10-10 13:24:19 +11:00
parent cb466e4d34
commit fb150cc1b0
5 changed files with 51 additions and 17 deletions

View File

@ -207,7 +207,7 @@ class CompanyController extends BaseController
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account); $company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
$company->saveSettings($request->input('settings')); $company->saveSettings($request->input('settings'), $company);
$this->uploadLogo($request->file('company_logo'), $company, $company); $this->uploadLogo($request->file('company_logo'), $company, $company);
@ -411,7 +411,7 @@ class CompanyController extends BaseController
{ {
$company = $this->company_repo->save($request->all(), $company); $company = $this->company_repo->save($request->all(), $company);
$company->saveSettings($request->input('settings')); $company->saveSettings($request->input('settings'), $company);
$this->uploadLogo($request->file('company_logo'), $company, $company); $this->uploadLogo($request->file('company_logo'), $company, $company);

View File

@ -13,7 +13,7 @@ namespace App\Http\ValidationRules;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Models\User; use App\Models\User;
use App\Utils\Traits\CompanySettingsSaver; use App\Utils\Traits\SettingsSaver;
use Illuminate\Contracts\Validation\Rule; use Illuminate\Contracts\Validation\Rule;
/** /**
@ -22,7 +22,7 @@ use Illuminate\Contracts\Validation\Rule;
*/ */
class ValidSettingsRule implements Rule class ValidSettingsRule implements Rule
{ {
use CompanySettingsSaver; use SettingsSaver;
/** /**
* @param string $attribute * @param string $attribute
* @param mixed $value * @param mixed $value

View File

@ -30,7 +30,7 @@ use App\Models\TaxRate;
use App\Models\Timezone; use App\Models\Timezone;
use App\Models\Traits\AccountTrait; use App\Models\Traits\AccountTrait;
use App\Models\User; use App\Models\User;
use App\Utils\Traits\CompanySettingsSaver; use App\Utils\Traits\SettingsSaver;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -40,7 +40,7 @@ class Company extends BaseModel
{ {
use PresentableTrait; use PresentableTrait;
use MakesHash; use MakesHash;
use CompanySettingsSaver; use SettingsSaver;
protected $presenter = 'App\Models\Presenters\CompanyPresenter'; protected $presenter = 'App\Models\Presenters\CompanyPresenter';

View File

@ -14,19 +14,27 @@ namespace App\Utils\Traits;
use App\DataMapper\CompanySettings; use App\DataMapper\CompanySettings;
/** /**
* Class CompanySettingsSaver * Class SettingsSaver
* @package App\Utils\Traits * @package App\Utils\Traits
*/ */
trait CompanySettingsSaver trait SettingsSaver
{ {
public function saveSettings($settings) /**
* Saves a setting object
*
* Works for groups|clients|companies
* @param array $settings The request input settings array
* @param object $entity The entity which the settings belongs to
* @return void
*/
public function saveSettings($settings, $entity)
{ {
if(!$settings) if(!$settings)
return; return;
$company_settings = $this->settings; $entity_settings = $this->settings;
//unset protected properties. //unset protected properties.
foreach(CompanySettings::$protected_fields as $field) foreach(CompanySettings::$protected_fields as $field)
@ -36,12 +44,21 @@ trait CompanySettingsSaver
//iterate through set properties with new values; //iterate through set properties with new values;
foreach($settings as $key => $value) foreach($settings as $key => $value)
$company_settings->{$key} = $value; $entity_settings->{$key} = $value;
$this->settings = $company_settings; $entity->settings = $entity_settings;
$this->save(); $entity->save();
} }
/**
* Used for custom validation of inbound
* settings request.
*
* Returns an array of errors, or boolean TRUE
* on successful validation
* @param array $settings The request() settings array
* @return array|bool Array on failure, boolean TRUE on success
*/
public function validateSettings($settings) public function validateSettings($settings)
{ {
$settings = (object)$settings; $settings = (object)$settings;
@ -75,7 +92,18 @@ trait CompanySettingsSaver
return true; return true;
} }
private function checkSettingType($settings) /**
* Checks the settings object for
* correct property types.
*
* The method will drop invalid types from
* the object and will also settype() the property
* so that it can be saved cleanly
*
* @param array $settings The settings request() array
* @return object stdClass object
*/
private function checkSettingType($settings) : \stdClass
{ {
$settings = (object)$settings; $settings = (object)$settings;
$casts = CompanySettings::$casts; $casts = CompanySettings::$casts;
@ -114,7 +142,13 @@ trait CompanySettingsSaver
} }
private function checkAttribute($key, $value) /**
* Type checks a object property.
* @param string $key The type
* @param string $value The object property
* @return bool TRUE if the property is the expected type
*/
private function checkAttribute($key, $value) :bool
{ {
switch ($key) switch ($key)
{ {
@ -138,7 +172,7 @@ trait CompanySettingsSaver
json_decode($string); json_decode($string);
return (json_last_error() == JSON_ERROR_NONE); return (json_last_error() == JSON_ERROR_NONE);
default: default:
return $value; return false;
} }
} }

View File

@ -22,7 +22,7 @@ use Tests\TestCase;
/** /**
* @test * @test
* @covers App\Utils\Traits\CompanySettingsSaver * @covers App\Utils\Traits\SettingsSaver
*/ */
class CompanySettingsTest extends TestCase class CompanySettingsTest extends TestCase
{ {