Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ageekdev/geek-credit
Browse files Browse the repository at this point in the history
  • Loading branch information
eidolex committed Jan 24, 2024
2 parents 8d72d99 + 7a5a4ab commit 6e396cd
Show file tree
Hide file tree
Showing 19 changed files with 112 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Security Policy

If you discover any security related issues, please email <:author_email> instead of using the issue tracker.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Please see the documentation for all configuration options:
# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
labels:
- "dependencies"
2 changes: 1 addition & 1 deletion .github/workflows/fix-php-code-style-issues.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- name: Check out repository code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ jobs:
matrix:
os: [ubuntu-latest]
php: [8.1, 8.2]
laravel: [10.*, 9.*]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
larastan: 2.4.*
- laravel: 9.*
testbench: 7.*
larastan: 2.*

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/update-changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
ref: main

Expand All @@ -21,8 +21,8 @@ jobs:
release-notes: ${{ github.event.release.body }}

- name: Commit updated CHANGELOG
uses: stefanzweifel/git-auto-commit-action@v4
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: main
commit_message: Update CHANGELOG
file_pattern: CHANGELOG.md
file_pattern: CHANGELOG.md
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) :author_name <:author_email>
Copyright (c) AgeekDev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
You can find instructions for submitting a new package proposal in [this repo](https://github.com/ageekdev/packages).
<h1 align="center">Geek Credit</h1>

# A skeleton repo for contributions

<h1 align="center">geek-credit</h1>

[![Laravel 9.x](https://img.shields.io/badge/Laravel-9.x-red.svg?style=flat-square)](https://laravel.com/docs/9.x)
[![Laravel 10.x](https://img.shields.io/badge/Laravel-10.x-red.svg?style=flat-square)](http://laravel.com/docs/10.x)
[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/ageekdev/geek-credit/run-tests.yml?label=tests&style=flat-square)](https://github.com/ageekdev/geek-credit/actions/workflows/run-tests.yml)

Note: Replace GeekCredit :author_name :author_username :author_email geek-credit :package_description with their correct values in README.md, CHANGELOG.md, CONTRIBUTING.md, LICENSE.md, composer.json and other files, then delete this line. Tip: Use "Find in Path/Files" in your code editor to find these keywords within the package directory and replace all occurrences with your specified term.

This is where your description should go. Add a little code example so build can understand real quick how the package can be used. Try and limit it to a paragraph or two.
The Geek Credit Package simplifies credit management and in-app purchases in Laravel apps. With transaction history and customizable features, it's ideal for implementing credit-based systems.

## Installation

Expand All @@ -23,12 +16,29 @@ composer require ageekdev/geek-credit
You can publish the config file with:

```bash
php artisan vendor:publish --tag="geek-credit-config"
php artisan vendor:publish --provider="Ageekdev\GeekCredit\GeekCreditServiceProvider" --tag="geek-credit-config"
```

You can publish the migration with:

## Usage
```bash
php artisan vendor:publish --provider="Ageekdev\GeekCredit\GeekCreditServiceProvider" --tag="geek-credit-migrations"
```
## Using Credit

Add the HasCredit trait on App\User model or any model who acts as user in your app.

```php
use Illuminate\Database\Eloquent\Model;
use Ageekdev\GeekCredit\Traits\HasCredit;

class UserModel extends Model
{
use HasCredit;

...
}
```

## Testing

Expand All @@ -50,7 +60,6 @@ Please review [our security policy](../../security/policy) on how to report secu

## Credits

- [:author_name](https://github.com/:author_username)
- [All Contributors](../../contributors)

## License
Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ageekdev/geek-credit",
"description": ":package_description",
"description": "To simplify credit management and in-app purchases in Laravel apps.",
"keywords": [
"laravel",
"geek-credit"
Expand All @@ -9,8 +9,7 @@
"license": "MIT",
"authors": [
{
"name": ":author_name",
"email": ":author_email",
"name": "eidolex",
"role": "Developer"
}
],
Expand All @@ -22,11 +21,11 @@
},
"require-dev": {
"laravel/pint": "^1.10",
"nunomaduro/collision": "^6.0|^7.0",
"nunomaduro/collision": "^7.0",
"nunomaduro/larastan": "^2.0",
"orchestra/testbench": "^7.0|^8.0",
"pestphp/pest": "^1.0|^2.0",
"pestphp/pest-plugin-laravel": "^1.4|^2.0",
"orchestra/testbench": "^8.0",
"pestphp/pest": "^2.0",
"pestphp/pest-plugin-laravel": "^2.0",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
Expand Down Expand Up @@ -67,4 +66,4 @@
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
6 changes: 3 additions & 3 deletions src/Enums/CreditTransactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ enum CreditTransactionType: int

public function text(): string
{
return match ($this->value) {
return match ($this) {
self::In => 'In',
self::Out => 'Out',
};
}

public function isOut(): bool
{
return $this->value === self::Out;
return $this === self::Out;
}

public function isIn(): bool
{
return $this->value === self::In;
return $this === self::In;
}
}
2 changes: 1 addition & 1 deletion src/Facades/GeekCredit.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Support\Facades\Facade;

/**
* @see \Ageekdev\GeekCredit\geek-credit
* @see \Ageekdev\GeekCredit\GeekCredit
*/
class GeekCredit extends Facade
{
Expand Down
40 changes: 28 additions & 12 deletions src/GeekCreditServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,40 @@ class GeekCreditServiceProvider extends ServiceProvider
*/
public function register(): void
{
$this->loadMigrationsFrom([
dirname(__DIR__).'/database',
]);

$this->mergeConfigFrom(__DIR__.'/../config/geek-credit.php', 'geek-credit');

$this->app->singleton('geek-credit', function () {
return new GeekCredit();
});
$this->configure();
}

/**
* Bootstrap the application services
*/
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/geek-credit.php' => config_path('geek-credit.php'),
], 'config');
$this->registerPublishing();
}

/**
* Setup the configuration.
*/
protected function configure(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/geek-credit.php', 'geek-credit'
);
}

/**
* Register the package's publishable resources.
*/
protected function registerPublishing(): void
{
if ($this->app->runningInConsole()) {
$this->publishes([
__DIR__.'/../config/geek-credit.php' => $this->app->configPath('geek-credit.php'),
], 'geek-credit-config');

$this->publishes([
__DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
], 'geek-credit-migrations');
}
}
}
11 changes: 11 additions & 0 deletions src/Models/Credit.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property int $id
* @property string $holder_type
* @property string|int $holder_id
* @property float $initial_balance
* @property float $remaining_balance
* @property array $meta
* @property ?\Illuminate\Support\Carbon $expires_at
* @property-read ?\Illuminate\Support\Carbon $created_at
* @property-read ?\Illuminate\Support\Carbon $updated_at
*/
class Credit extends Model
{
protected $fillable = [
Expand Down
9 changes: 9 additions & 0 deletions src/Models/CreditTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* @property int $id
* @property string $holder_type
* @property string|int $holder_id
* @property float $amount
* @property CreditTransactionType $type
* @property string $name
* @property string $description
* @property array $meta
* @property-read ?\Illuminate\Support\Carbon $created_at
* @property-read ?\Illuminate\Support\Carbon $updated_at
*/
class CreditTransaction extends Model
{
Expand Down
16 changes: 8 additions & 8 deletions src/Models/CreditTransactionDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Ageekdev\GeekCredit\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
* @property int $id
* @property int $credit_transaction_id
* @property int $credit_id
* @property float $amount
* @property-read ?\Illuminate\Support\Carbon $created_at
* @property-read ?\Illuminate\Support\Carbon $updated_at
*/
class CreditTransactionDetail extends Model
{
use HasFactory;

/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;

protected $fillable = [
Expand Down
7 changes: 3 additions & 4 deletions src/Services/CreditService.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function addNonExpiringCredit(
float $amount,
$meta = null,
): Credit {
$credit = $this->creditModel->query()
$credit = $this->creditModel
->whereNull('expires_at')
->isHolder($holder)
->lockForUpdate()
Expand Down Expand Up @@ -200,13 +200,12 @@ public function useCreditFromHolder(
}

/**
* @return \Illuminate\Support\Collection<array-key, Credit>
* @return \Illuminate\Support\Collection<int, Credit>
*/
public function getRemainingCreditFromHolderForUpdate(
Model $holder,
): Collection {
) {
return $this->creditModel
->query()
->isHolder($holder)
->notExpired()
->hasRemainingBalance()
Expand Down

0 comments on commit 6e396cd

Please sign in to comment.