Skip to content

Commit

Permalink
Merge pull request #11 from VPremiss/dev
Browse files Browse the repository at this point in the history
v1.0.12
  • Loading branch information
GoodM4ven authored Apr 26, 2024
2 parents f3a247e + bc1707c commit 277245d
Show file tree
Hide file tree
Showing 18 changed files with 235 additions and 170 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
**The security [Livewire](https://livewire.laravel.com) public methods needed!**

[![Latest Version on Packagist](https://img.shields.io/packagist/v/vpremiss/livewire-nonceable.svg?style=for-the-badge)](https://packagist.org/packages/vpremiss/livewire-nonceable)
[![Total Downloads](https://img.shields.io/packagist/dt/vpremiss/livewire-nonceable.svg?style=for-the-badge&color=darkgoldenrod)](https://packagist.org/packages/vpremiss/livewire-nonceable)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/vpremiss/livewirenonceable/run-tests.yml?branch=main&label=tests&style=for-the-badge&color=darkgreen)](https://github.com/vpremiss/livewirenonceable/actions?query=workflow%3Arun-tests+branch%3Amain)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/vpremiss/livewirenonceable/run-tests.yml?branch=main&label=tests&style=for-the-badge&color=forestgreen)](https://github.com/vpremiss/livewirenonceable/actions?query=workflow%3Arun-tests+branch%3Amain)
[![Total Downloads](https://img.shields.io/packagist/dt/vpremiss/livewire-nonceable.svg?style=for-the-badge&color=b07d00)](https://packagist.org/packages/vpremiss/livewire-nonceable)


## Description
Expand Down Expand Up @@ -55,7 +55,7 @@ Thanks for coming to my -talk. Enjoy the package and the awesome stacking like f
public function getNonces(): array
{
return [
'complex-searching' => 5,
'complex-searching' => 5, // the nonce title, plus 5 seconds lasting in cache
// 'heavy-processing' => 10, as another example
];
}
Expand All @@ -73,13 +73,13 @@ Thanks for coming to my -talk. Enjoy the package and the awesome stacking like f
$nonce = $this->generateNonce('complex-searching');
$this->dispatch(
'searching-began', // receive in the SPA or API
'searching-began', // receive in the front-end (SPA)
query: $validatedQuery,
nonce: $nonce,
);
}
// This is hit back from AlpineJS after axios
// This is hit back from AlpineJS using axios
public function complexSearch($responseFromApi, $nonce)
{
// Or use the opposite ! $this->doesNonceExist($title, $nonce) method
Expand Down
13 changes: 7 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
],
"require": {
"php": "^8.2",
"spatie/laravel-package-tools": "^1.16",
"illuminate/contracts": "^11.0",
"livewire/livewire": "^3.0",
"predis/predis": "^2.0"
"predis/predis": "^2.0",
"spatie/laravel-package-tools": "^1.16",
"vpremiss/crafty": "^3.1"
},
"require-dev": {
"nunomaduro/collision": "^8.1.1",
Expand All @@ -51,7 +52,9 @@
"scripts": {
"analyse": "vendor/bin/phpstan analyse",
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage"
"test-coverage": "vendor/bin/pest --coverage",
"green": "vendor/bin/phpstan analyse && vendor/bin/pest",
"testbench": "vendor/bin/testbench"
},
"config": {
"sort-packages": true,
Expand All @@ -65,9 +68,7 @@
"providers": [
"VPremiss\\LivewireNonceable\\LivewireNonceableServiceProvider"
],
"aliases": {
"LivewireNonceable": "VPremiss\\LivewireNonceable\\Facades\\LivewireNonceable"
}
"aliases": {}
}
},
"minimum-stability": "dev",
Expand Down
18 changes: 10 additions & 8 deletions config/livewire-nonceable.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
<?php

declare(strict_types=1);

return [

/*
|--------------------------------------------------------------------------
| Throw if Nonce attributes are long limit (bool)
| Nonce key attributes character length (int)
|--------------------------------------------------------------------------
|
| Decide whether the package would throw and remind you if it found the
| nonce attributes (title and unique-key) used for caching are a bit
| long or not.
| Determine the length of Nonce attributes' strings.
|
*/

'throw_if_long' => false,
'key_attributes_length' => 40,

/*
|--------------------------------------------------------------------------
| Nonce attributes character length (int)
| Throw if Nonce attributes are long (bool)
|--------------------------------------------------------------------------
|
| Determine the length of Nonce attributes' strings.
| Decide whether the package should throw if it found the nonce key attributes
| (title and unique-key) used for caching are a longer than the set length
| in the previous configuration.
|
*/

'attributes_length' => 40,
'throw_if_key_attributes_are_long' => false,

];
4 changes: 3 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ parameters:
- config
tmpDir: build/phpstan
checkMissingIterableValueType: false

ignoreErrors:
- message: '#Match expression does not handle remaining value: string#'
path: src/LivewireNonceableServiceProvider.php
10 changes: 0 additions & 10 deletions src/Exceptions/NoncenseException.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Facades/LivewireNonceable.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/Interfaces/Noncing.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace VPremiss\LivewireNonceable\Interfaces;

interface Noncing
Expand Down
7 changes: 0 additions & 7 deletions src/LivewireNonceable.php

This file was deleted.

24 changes: 23 additions & 1 deletion src/LivewireNonceableServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
<?php

declare(strict_types=1);

namespace VPremiss\LivewireNonceable;

use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use VPremiss\Crafty\Utilities\Configurated\Interfaces\Configurated;
use VPremiss\LivewireNonceable\Support\Concerns\HasValidatedConfiguration;

class LivewireNonceableServiceProvider extends PackageServiceProvider
class LivewireNonceableServiceProvider extends PackageServiceProvider implements Configurated
{
use HasValidatedConfiguration;

public function configurePackage(Package $package): void
{
/*
Expand All @@ -18,4 +24,20 @@ public function configurePackage(Package $package): void
->name('livewire-nonceable')
->hasConfigFile();
}

public function configValidation(string $configKey): void
{
match ($configKey) {
'livewire-nonceable.key_attributes_length' => $this->validateKeyAttributesLengthConfig(),
'livewire-nonceable.throw_if_key_attributes_are_long' => $this->validateThrowIfLongConfig(),
};
}

public function configDefault(string $configKey): mixed
{
return match ($configKey) {
'livewire-nonceable.key_attributes_length' => 40,
'livewire-nonceable.throw_if_key_attributes_are_long' => false,
};
}
}
107 changes: 107 additions & 0 deletions src/Support/Concerns/HasNoncingValidations.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

namespace VPremiss\LivewireNonceable\Support\Concerns;

use VPremiss\Crafty\Facades\CraftyPackage;
use VPremiss\LivewireNonceable\LivewireNonceableServiceProvider;
use VPremiss\LivewireNonceable\Support\Exceptions\NoncenseException;

trait HasNoncingValidations
{
private function validateTheNoncingInterface(): void
{
if (
!method_exists(self::class, 'getNonces')
|| !method_exists(self::class, 'getNonceUniqueId')
) {
throw new NoncenseException(
'Noncing interface methods were not found! Please implement it on ' . self::class . ' Livewire component.'
);
}
}

private function validateTheNonceUniqueId(): void
{
if (empty($uniqueId = $this->getNonceUniqueId())) {
throw new NoncenseException(
'The Noncing unique ID was found in ' . self::class . ' Livewire component to be an empty string!'
);
}

if (
CraftyPackage::validatedConfig(
'livewire-nonceable.throw_if_key_attributes_are_long',
LivewireNonceableServiceProvider::class,
)
) {
$length = CraftyPackage::validatedConfig(
'livewire-nonceable.key_attributes_length',
LivewireNonceableServiceProvider::class,
);

if (strlen($uniqueId) > $length) {
throw new NoncenseException(
'The Noncing unique ID was found in ' . self::class . " Livewire component to surpass the limit defined in configuration: ({$length})."
);
}
}
}

private function validateTheNonces(): void
{
if (empty($nonces = $this->getNonces())) {
throw new NoncenseException(
'The Noncing nonces were found in ' . self::class . ' Livewire component to be an empty array!'
);
}

foreach ($nonces as $key => $value) {
if (!is_string($key) || empty($key)) {
throw new NoncenseException(
'A key in Noncing nonces was found in ' . self::class . ' Livewire component to NOT be a string or empty! It is meant to be cache key title to lasting cache time in seconds; strings to integers.'
);
}

if (
CraftyPackage::validatedConfig(
'livewire-nonceable.throw_if_key_attributes_are_long',
LivewireNonceableServiceProvider::class,
)
) {
$length = CraftyPackage::validatedConfig(
'livewire-nonceable.key_attributes_length',
LivewireNonceableServiceProvider::class,
);

if (strlen($key) > $length) {
throw new NoncenseException(
'A key in Noncing nonces was found in ' . self::class . " Livewire component to be a long string! Longer than the length defined in configuration: ($length)."
);
}
}

if (!is_int($value)) {
throw new NoncenseException(
'A value in Noncing nonces was found in ' . self::class . ' Livewire component to NOT be an integer! It is meant to be cache key title to lasting cache time in seconds; strings to integers.'
);
}

if ($value <= 0) {
throw new NoncenseException(
'A value in Noncing nonces was found in ' . self::class . ' Livewire component to an integer less than zero! It is meant to be cache key title to lasting cache time in seconds; strings to integers. (MORE than zero, obviously!)'
);
}
}
}

private function validateNonceTitle($title): void
{
if (!array_key_exists($title, $this->getNonces())) {
throw new NoncenseException(
"The given nonce title '$title' doesn't exist in " . self::class . " Livewire component's getNonces() titles-to-seconds array."
);
}
}
}
29 changes: 29 additions & 0 deletions src/Support/Concerns/HasValidatedConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace VPremiss\LivewireNonceable\Support\Concerns;

use VPremiss\Crafty\Facades\CraftyPackage;
use VPremiss\Crafty\Utilities\Configurated\Exceptions\ConfiguratedValidatedConfigurationException;

trait HasValidatedConfiguration
{
protected function validateKeyAttributesLengthConfig()
{
if (!is_int(CraftyPackage::config('livewire-nonceable.key_attributes_length', $this))) {
throw new ConfiguratedValidatedConfigurationException(
'The configuration integer for "key attributes length" is not found!'
);
}
}

protected function validateThrowIfLongConfig()
{
if (!is_bool(CraftyPackage::config('livewire-nonceable.throw_if_key_attributes_are_long', $this))) {
throw new ConfiguratedValidatedConfigurationException(
'The configuration boolean for "throwing if key attributes are long" is not found!'
);
}
}
}
11 changes: 11 additions & 0 deletions src/Support/Exceptions/NoncenseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace VPremiss\LivewireNonceable\Support\Exceptions;

use Exception;

class NoncenseException extends Exception
{
}
Loading

0 comments on commit 277245d

Please sign in to comment.