Skip to content

Commit

Permalink
added settings trait
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Oct 26, 2023
1 parent f760f14 commit 252ff28
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
28 changes: 28 additions & 0 deletions app/Traits/Settings.php
@@ -0,0 +1,28 @@
<?php

namespace App\Traits;

use App\Models\Setting\Setting;
use App\Traits\Companies;

trait Settings
{
use Companies;

public function getSettingValue(string $key, mixed $default = ''): mixed
{
$settings = setting()->all();

if (! empty($settings)) {
return setting($key);
}

$company_id = $this->getCompanyId();

if (empty($company_id)) {
return $default;
}

return Setting::companyId($company_id)->where('key', $key)->pluck('value')->first();
}
}
4 changes: 2 additions & 2 deletions app/Traits/SiteApi.php
Expand Up @@ -2,8 +2,8 @@

namespace App\Traits;

use App\Utilities\Info;
use Akaunting\Version\Version;
use App\Utilities\Info;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
Expand All @@ -18,7 +18,7 @@ protected static function siteApiRequest($method, $path, $extra_data = [])
$client = new Client(['verify' => false, 'base_uri' => static::$base_uri]);

$headers['headers'] = [
'Authorization' => 'Bearer ' . setting('apps.api_key'),
'Authorization' => 'Bearer ' . Info::getApiKey(),
'Accept' => 'application/json',
'Referer' => app()->runningInConsole() ? config('app.url') : url('/'),
'Akaunting' => Version::short(),
Expand Down
10 changes: 9 additions & 1 deletion app/Utilities/Info.php
Expand Up @@ -6,6 +6,7 @@
use App\Models\Common\Company;
use App\Models\Common\Contact;
use App\Models\Document\Document;
use App\Traits\Settings;
use Composer\InstalledVersions;
use Illuminate\Support\Facades\DB;

Expand All @@ -16,7 +17,7 @@ public static function all()
static $info = [];

$basic = [
'api_key' => setting('apps.api_key'),
'api_key' => static::getApiKey(),
'ip' => static::ip(),
];

Expand Down Expand Up @@ -87,4 +88,11 @@ public static function ip()
? request()->header('CF_CONNECTING_IP')
: request()->ip();
}

public static function getApiKey(): string
{
$setting = new class() { use Settings; };

return $setting->getSettingValue('apps.api_key');
}
}

0 comments on commit 252ff28

Please sign in to comment.