Skip to content

Instantly share code, notes, and snippets.

View Usmanzahidcode's full-sized avatar
🎯
Focusing

Usman Zahid Usmanzahidcode

🎯
Focusing
View GitHub Profile
@Usmanzahidcode
Usmanzahidcode / HasRoles.php
Last active February 22, 2025 10:31
This HasRoles trait provides role-based access control (RBAC) functionality for a user model in Laravel. It allows assigning and removing roles, checking role ownership, and retrieving the highest assigned role. The implementation assumes a many-to-many relationship between users and roles, requiring a roles table and a pivot table for associati…
<?php
trait HasRoles {
// Assign a role to the user
public function assignRole(string $roleName): void {
$role = Role::where('name', $roleName)->first();
if ($role && !$this->hasRole($roleName)) {
$this->roles()->attach($role->id);
}
}
@Usmanzahidcode
Usmanzahidcode / ServiceResponse.php
Last active February 22, 2025 10:31
A response object I use for communication between the services.
<?php
class ServiceResponse {
private ?string $message = null;
private ?bool $success = true;
private array $errors = [];
private ?ServiceResponse $previous = null;
private mixed $data = null;
// Getters
<?php
class PricingOptionTaxHelper {
/**
* Resolve the total amount for a pricing option.
*/
public static function resolveTotal(PricingOption $pricingOption, bool $format = true): float {
$taxPlan = $pricingOption->taxPlan;
if ($taxPlan && $taxPlan->type===TaxPlanType::INCLUSIVE) {
$total = $pricingOption->price;
<?php
class Node {
public string $tag;
public array $children = [];
public function __construct(string $tag) {
$this->tag = $tag;
}
public function addChild(Node $child): void {
@Usmanzahidcode
Usmanzahidcode / FakeLogMaker.php
Created February 24, 2025 17:48
A PHPprogram to output fake log messages with fake information such as PIDs, timestamps and data.
<?php
function randomHex(): string {
return bin2hex(random_bytes(random_int(10, 20)));
}
$logTypes = ["INFO", "DEBUG", "WARNING", "ERROR", "CRITICAL"];
$processes = ["sshd", "cron", "init", "systemd", "bash", "php", "nginx", "mysql", "firewalld", "python3", "node"];
while (true) {
$timestamp = date("Y-m-d H:i:s");
@Usmanzahidcode
Usmanzahidcode / setup_laravel_app.sh
Created March 19, 2025 15:22
A shell script to setup the Laravel application for non technicals.
#!/bin/bash
echo "Installing dependencies..."
composer install --no-dev --optimize-autoloader
echo "Setting up .env..."
cp .env.example .env
echo "Generating application key..."
php artisan key:generate