Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
martinbean committed Nov 26, 2024
0 parents commit e57d232
Show file tree
Hide file tree
Showing 21 changed files with 634 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{yaml,yml}]
indent_size = 2
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @martinbean
22 changes: 22 additions & 0 deletions .github/workflows/composer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Validate Composer

on:
push:
paths:
- composer.json

jobs:
validate:
name: Validate
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4

- name: Validate
run: composer validate --no-check-lock --no-check-version --strict
25 changes: 25 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Lint

on:
push:
paths:
- '**.php'

jobs:
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4

- name: Install dependencies
run: composer install --no-ansi --no-interaction --no-progress --prefer-dist

- name: Lint
run: composer run-script lint
32 changes: 32 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test

on:
push:
paths:
- '.github/workflows/test.yml'
- '**.php'

jobs:
test:
strategy:
matrix:
php_version:
- '8.2'
- '8.3'
- '8.4'
name: Test
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v4

- name: Set up PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php_version }}

- name: Install dependencies
run: composer install --no-ansi --no-interaction --no-progress --prefer-dist

- name: Run tests
run: composer run-script test
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/.phpunit.*
/vendor
composer.lock
phpunit.xml
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Changelog

## [1.0.0] - 2024-11-26

### Added
- Initial release. Includes configuration and webhook handler.
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Martin Bean

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
102 changes: 102 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Mux PHP SDK for Laravel

## Installation
```sh
composer require martinbean/mux-php-laravel
```

## Configuration
By default, the package expects two environment variables to be defined:

- `MUX_CLIENT_ID`
- `MUX_CLIENT_SECRET`

This should be a Mux access token ID and secret.

You can generate a Mux access token by:

1. Logging into your [Mux dashboard][1]
2. Going to “Settings”
3. Then going to “Access Tokens”

### Verifying webhook signatures
To verify webhook signatures from Mux, specify the signing secret for your webhook endpoint as the value of your `MUX_WEBHOOK_SECRET` environment variable. This means you will need to register your webhook endpoint in the [Mux dashboard][1] first.

## Usage

### API clients
The package will register default configuration based on the configured Mux access token. This means you can then type-hint a Mux client in your Laravel application, and it and its dependencies will be automatically resolved:

```php
namespace App\Http\Controllers;

use MuxPhp\Api\DirectUploadsApi;

class UploadController extends Controller
{
protected DirectUploadsApi $uploads;

public function __construct(DirectUploadsApi $uploads)
{
$this->uploads = $uploads;
}
}
```

### Webhook handling
The package will also automatically register a route to listen for webhooks sent by Mux. The URI of this handler is **/mux/webhook**

The webhook handler is heavily based on [Cashier][2]’s webhook handler. When a webhook from Mux is received, an instance of the `MartinBean\Laravel\Mux\Events\WebhookReceived` event is dispatched containing the webhook’s payload.

You can define a listener that listens for this event:

```php
namespace App\Listeners;

use MartinBean\Laravel\Mux\Events\WebhookReceived;

class MuxEventListener
{
public function handle(WebhookReceived $event): void
{
// Handle Mux webhook event...
}
}
```

This is where you would update models, etc in your application based on events from Mux:

```diff
public function handle(WebhookReceived $event): void
{
- // Handle Mux webhook event...
+ match ($event->payload['type']) {
+ 'video.asset.errored' => $this->handleVideoAssetErrored($event->payload),
+ 'video.asset.ready' => $this->handleVideoAssetReady($event->payload),
+ default => null, // unhandled event
+ };
}
+
+ protected function handleVideoAssetErrored(array $payload): void
+ {
+ // TODO: Retrieve associated video
+ // TODO: Update video status to 'errored'
+ // TODO: Notify user processing video errored
+ }
+
+ protected function handleVideoAssetReady(array $payload): void
+ {
+ // TODO: Retrieve associated video
+ // TODO: Update video status to 'ready'
+ // TODO: Notify user processing video succeeded
+ }
```

## Contribution
Contributions are always welcome. Please open a pull request with your proposed changes, with accompanying tests.

## License
Licensed under the [MIT License](LICENSE.md)

[1]: https://dashboard.mux.com/
[2]: https://laravel.com/docs/cashier
70 changes: 70 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"$schema": "https://getcomposer.org/schema.json",
"name": "martinbean/mux-php-laravel",
"description": "Laravel wrapper for the official Mux PHP SDK.",
"version": "1.0.0",
"type": "library",
"keywords": [
"laravel",
"mux",
"php",
"video"
],
"readme": "README.md",
"license": "MIT",
"authors": [
{
"name": "Martin Bean",
"homepage": "https://martinbean.dev"
}
],
"support": {
"issues": "https://github.com/martinbean/mux-php-laravel/issues",
"source": "https://github.com/martinbean/mux-php-laravel"
},
"funding": [
{
"type": "other",
"url": "https://buymeacoffee.com/martinbean"
}
],
"require": {
"illuminate/http": "^11.0",
"illuminate/routing": "^11.0",
"illuminate/support": "^11.0",
"muxinc/mux-php": "^3.0"
},
"require-dev": {
"orchestra/testbench": "^9.6",
"squizlabs/php_codesniffer": "^3.11"
},
"autoload": {
"psr-4": {
"MartinBean\\Laravel\\Mux\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MartinBean\\Laravel\\Mux\\Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"minimum-stability": "stable",
"prefer-stable": true,
"config": {
"sort-packages": true
},
"scripts": {
"lint": "phpcs --standard=PSR12 src",
"test": "phpunit"
},
"extra": {
"laravel": {
"providers": [
"MartinBean\\Laravel\\Mux\\MuxServiceProvider"
]
}
}
}
23 changes: 23 additions & 0 deletions config/mux.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

return [

/*
|--------------------------------------------------------------------------
| Mux access token
|--------------------------------------------------------------------------
|
| Access tokens are used to authenticate requests to the Mux API. You can
| create your own access token by logging in to the Mux Dashboard, and
| going to Settings > Access Tokens.
|
*/

'client_id' => env('MUX_CLIENT_ID'),
'client_secret' => env('MUX_CLIENT_SECRET'),

'webhook' => [
'secret' => env('MUX_WEBHOOK_SECRET'),
],

];
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true">
<testsuites>
<testsuite name="Package Tests">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<source ignoreIndirectDeprecations="true" restrictNotices="true" restrictWarnings="true">
<include>
<directory>src</directory>
</include>
</source>
<php>
<env name="MUX_CLIENT_ID" value="null"/>
<env name="MUX_CLIENT_SECRET" value="null"/>
<env name="MUX_WEBHOOK_SECRET" value="null"/>
</php>
</phpunit>
7 changes: 7 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use Illuminate\Support\Facades\Route;
use MartinBean\Laravel\Mux\Http\Controllers\WebhookController;

// Mux Webhook
Route::post('/mux/webhook', WebhookController::class)->name('mux.webhook');
19 changes: 19 additions & 0 deletions src/Events/WebhookReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace MartinBean\Laravel\Mux\Events;

class WebhookReceived
{
/**
* The webhook payload.
*/
public array $payload;

/**
* Create a new event instance.
*/
public function __construct(array $payload)
{
$this->payload = $payload;
}
}
9 changes: 9 additions & 0 deletions src/Exception/SignatureException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace MartinBean\Laravel\Mux\Exceptions;

use RuntimeException;

class SignatureException extends RuntimeException
{
}
Loading

0 comments on commit e57d232

Please sign in to comment.