Skip to content

Commit

Permalink
♻️ reduce Cognitive Complexity in load()
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Nov 29, 2024
1 parent b465695 commit 67daaa3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
50 changes: 33 additions & 17 deletions classes/SecurityHeaders.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,53 @@ public function csp(): CSPBuilder

public function load(array|string|null $data = null): CSPBuilder
{
// load default if is null
if (is_null($data)) {
$data = $this->option('loader');
}

if (is_string($data) && F::exists($data)) {
$mime = F::mime($data);
$data = F::read($data);
if (in_array($mime, A::get(Mime::types(), 'json'))) {
$data = Json::decode($data);
} elseif (A::get(Mime::types(), 'yaml') && in_array($mime, A::get(Mime::types(), 'yaml'))) {
$data = Yaml::decode($data);
}
if (is_string($data)) {
$data = $this->loadFromFile($data);
}

if (is_array($data)) {
$this->cspBuilder = CSPBuilder::fromArray($data);
} else { // aka null
$this->cspBuilder = new CSPBuilder;
$this->cspBuilder = is_array($data) ? CSPBuilder::fromArray($data) : new CSPBuilder;

$this->addNonceForSelf();
$this->addPanelNonces();

return $this->cspBuilder;
}

public function loadFromFile(string $data): mixed
{
if (! F::exists($data)) {
return null;
}

// add nonce for self
$mime = F::mime($data);
$data = F::read($data);

if (in_array($mime, A::get(Mime::types(), 'json'))) {
$data = Json::decode($data);
} elseif (A::get(Mime::types(), 'yaml') && in_array($mime, A::get(Mime::types(), 'yaml'))) {
$data = Yaml::decode($data);
}

return $data;
}

public function addNonceForSelf(): void
{
if ($seed = strval($this->option('seed'))) {
$nonce = $this->setNonce($seed);
$this->cspBuilder->nonce('script-src', $nonce);
$this->cspBuilder->nonce('style-src', $nonce);
}
}

// add panel nonces
if ($this->option('panel')) {
public function addPanelNonces(): void
{
if (! $this->option('panel')) {
$panelnonces = (array) $this->option('panelnonces');
foreach ($panelnonces as $nonce) {
if (! is_string($nonce)) {
Expand All @@ -208,8 +226,6 @@ public function load(array|string|null $data = null): CSPBuilder
$this->cspBuilder->nonce('style-src', $nonce);
}
}

return $this->cspBuilder;
}

public function applySetter(): void
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/kirby3-security-headers",
"type": "kirby-plugin",
"version": "5.0.0",
"version": "5.0.1",
"license": "MIT",
"description": "Kirby Plugin for easier Security Headers setup",
"authors": [
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/installed.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php return array(
'root' => array(
'name' => 'bnomei/kirby3-security-headers',
'pretty_version' => '5.0.0',
'version' => '5.0.0.0',
'pretty_version' => '5.0.1',
'version' => '5.0.1.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand All @@ -11,8 +11,8 @@
),
'versions' => array(
'bnomei/kirby3-security-headers' => array(
'pretty_version' => '5.0.0',
'version' => '5.0.0.0',
'pretty_version' => '5.0.1',
'version' => '5.0.1.0',
'reference' => null,
'type' => 'kirby-plugin',
'install_path' => __DIR__ . '/../../',
Expand Down

0 comments on commit 67daaa3

Please sign in to comment.