403Webshell
Server IP : 65.108.144.40  /  Your IP : 216.73.217.165
Web Server : Apache/2.4.52 (Ubuntu)
System : Linux ubuntu-8gb-hel1-1 5.15.0-173-generic #183-Ubuntu SMP Fri Mar 6 13:29:34 UTC 2026 x86_64
User : dev ( 1000)
PHP Version : 8.2.30
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON  |  Sudo : ON  |  Pkexec : ON
Directory :  /var/www/html/project-slim/app/Services/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/html/project-slim/app/Services/CompanyService.php
<?php

namespace App\Services;

use App\Models\Company;
use App\Repositories\Interfaces\CompanyRepositoryInterface;
use App\Validators\CompanyValidator;
use App\Exceptions\ValidationException;
use Illuminate\Pagination\LengthAwarePaginator;

class CompanyService
{
    private CompanyRepositoryInterface $companyRepository;
    private CompanyValidator $validator;

    public function __construct(CompanyRepositoryInterface $companyRepository, CompanyValidator $validator)
    {
        $this->companyRepository = $companyRepository;
        $this->validator = $validator;
    }

    /**
     * Get company by ID
     */
    public function getCompanyById(int $id): ?Company
    {
        return $this->companyRepository->findById($id);
    }

    /**
     * Get company by domain
     */
    public function getCompanyByDomain(string $domain): ?Company
    {
        return $this->companyRepository->findByDomain($domain);
    }

    /**
     * Create a new company
     */
    public function createCompany(array $data): Company
    {
        // Validate input data
        $validation = $this->validator->validateCreate($data);
        if (!$validation->isValid()) {
            throw new ValidationException('Validation failed', $validation->getErrors());
        }

        return $this->companyRepository->create($data);
    }

    /**
     * Update company
     */
    public function updateCompany(int $id, array $data): ?Company
    {
        $company = $this->companyRepository->findById($id);
        if (!$company) {
            return null;
        }

        // Validate input data
        $validation = $this->validator->validateUpdate($data, $id);
        if (!$validation->isValid()) {
            throw new ValidationException('Validation failed', $validation->getErrors());
        }

        return $this->companyRepository->update($id, $data);
    }

    /**
     * Delete company
     */
    public function deleteCompany(int $id): bool
    {
        return $this->companyRepository->delete($id);
    }

    /**
     * Get paginated companies
     */
    public function getCompanies(int $page = 1, int $limit = 20, array $filters = []): LengthAwarePaginator
    {
        return $this->companyRepository->getAll($page, $limit, $filters);
    }

    /**
     * Search companies
     */
    public function searchCompanies(string $term, int $page = 1, int $limit = 20): LengthAwarePaginator
    {
        return $this->companyRepository->search($term, $page, $limit);
    }

    /**
     * Get company users
     */
    public function getCompanyUsers(int $companyId, int $page = 1, int $limit = 20): LengthAwarePaginator
    {
        return $this->companyRepository->getUsers($companyId, $page, $limit);
    }

    /**
     * Add social link to company
     */
    public function addSocialLink(int $companyId, string $platform, ?string $handle = null, ?string $url = null): bool
    {
        // Validate platform
        $allowedPlatforms = ['linkedin', 'twitter', 'x', 'instagram', 'youtube', 'facebook'];
        if (!in_array($platform, $allowedPlatforms)) {
            throw new ValidationException('Invalid social platform');
        }

        $result = $this->companyRepository->addSocialLink($companyId, $platform, $handle, $url);
        return $result !== null;
    }

    /**
     * Update social link
     */
    public function updateSocialLink(int $companyId, string $platform, ?string $handle = null, ?string $url = null): bool
    {
        $result = $this->companyRepository->updateSocialLink($companyId, $platform, $handle, $url);
        return $result !== null;
    }

    /**
     * Remove social link
     */
    public function removeSocialLink(int $companyId, string $platform): bool
    {
        return $this->companyRepository->removeSocialLink($companyId, $platform);
    }

    /**
     * Add product link to company
     */
    public function addProductLink(int $companyId, string $platform, string $url): bool
    {
        // Validate platform
        $allowedPlatforms = ['clutch', 'g2', 'capterra'];
        if (!in_array($platform, $allowedPlatforms)) {
            throw new ValidationException('Invalid product platform');
        }

        // Validate URL
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            throw new ValidationException('Invalid URL format');
        }

        $result = $this->companyRepository->addProductLink($companyId, $platform, $url);
        return $result !== null;
    }

    /**
     * Update product link
     */
    public function updateProductLink(int $companyId, string $platform, string $url): bool
    {
        // Validate URL
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            throw new ValidationException('Invalid URL format');
        }

        $result = $this->companyRepository->updateProductLink($companyId, $platform, $url);
        return $result !== null;
    }

    /**
     * Remove product link
     */
    public function removeProductLink(int $companyId, string $platform): bool
    {
        return $this->companyRepository->removeProductLink($companyId, $platform);
    }

    /**
     * Get company analytics
     */
    public function getCompanyAnalytics(int $companyId): array
    {
        $analytics = $this->companyRepository->getAnalytics($companyId);
        
        if (empty($analytics)) {
            return [];
        }

        // Add additional calculated metrics
        $company = $this->companyRepository->findById($companyId);
        if ($company) {
            $analytics['verification_rate'] = $analytics['verified_users'] > 0 
                ? round(($analytics['verified_users'] / $analytics['total_users']) * 100, 2)
                : 0;
            
            $analytics['activity_rate'] = $analytics['active_users'] > 0 
                ? round(($analytics['active_users'] / $analytics['total_users']) * 100, 2)
                : 0;

            // Get recent activity
            $analytics['recent_signups'] = $company->users()
                ->where('created_at', '>=', now()->subDays(30))
                ->count();
        }

        return $analytics;
    }

    /**
     * Get user analytics for company
     */
    public function getUserAnalytics(int $companyId): array
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return [];
        }

        return [
            'total_users' => $company->users()->count(),
            'active_users' => $company->activeUsers()->count(),
            'verified_users' => $company->verifiedUsers()->count(),
            'user_growth' => $this->getUserGrowthData($companyId),
            'user_roles' => $this->getUserRoleDistribution($companyId),
            'recent_activity' => $this->getRecentUserActivity($companyId)
        ];
    }

    /**
     * Get performance metrics for company
     */
    public function getPerformanceMetrics(int $companyId): array
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return [];
        }

        return [
            'user_engagement' => $this->calculateUserEngagement($companyId),
            'growth_rate' => $this->calculateGrowthRate($companyId),
            'retention_rate' => $this->calculateRetentionRate($companyId),
            'verification_rate' => $this->calculateVerificationRate($companyId)
        ];
    }

    /**
     * Bulk delete companies
     */
    public function bulkDelete(array $companyIds): int
    {
        return $this->companyRepository->bulkDelete($companyIds);
    }

    /**
     * Get company statistics
     */
    public function getStatistics(): array
    {
        return $this->companyRepository->getStatistics();
    }

    /**
     * Export companies data
     */
    public function exportCompanies(array $filters = []): array
    {
        $companies = $this->companyRepository->export($filters);
        
        return $companies->map(function ($company) {
            return [
                'id' => $company->company_id,
                'name' => $company->name,
                'domain' => $company->domain,
                'user_count' => $company->users_count,
                'active_users' => $company->active_users_count,
                'created_at' => $company->created_at->format('Y-m-d H:i:s'),
                'social_links' => $company->socialLinks->map(function ($link) {
                    return [
                        'platform' => $link->platform,
                        'url' => $link->generated_url ?? $link->url
                    ];
                })->toArray(),
                'product_links' => $company->productLinks->map(function ($link) {
                    return [
                        'platform' => $link->platform,
                        'url' => $link->url
                    ];
                })->toArray()
            ];
        })->toArray();
    }

    /**
     * Get user growth data for company
     */
    private function getUserGrowthData(int $companyId): array
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return [];
        }

        // Get user registration data for the last 12 months
        $growthData = [];
        for ($i = 11; $i >= 0; $i--) {
            $month = now()->subMonths($i);
            $count = $company->users()
                ->whereYear('created_at', $month->year)
                ->whereMonth('created_at', $month->month)
                ->count();
            
            $growthData[] = [
                'month' => $month->format('Y-m'),
                'users' => $count
            ];
        }

        return $growthData;
    }

    /**
     * Get user role distribution for company
     */
    private function getUserRoleDistribution(int $companyId): array
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return [];
        }

        return $company->users()
            ->with('roles')
            ->get()
            ->flatMap(function ($user) {
                return $user->roles->pluck('role_name');
            })
            ->countBy()
            ->toArray();
    }

    /**
     * Get recent user activity for company
     */
    private function getRecentUserActivity(int $companyId): array
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return [];
        }

        return $company->users()
            ->where('created_at', '>=', now()->subDays(30))
            ->orderBy('created_at', 'desc')
            ->limit(10)
            ->get(['user_id', 'first_name', 'last_name', 'email', 'created_at'])
            ->toArray();
    }

    /**
     * Calculate user engagement for company
     */
    private function calculateUserEngagement(int $companyId): float
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return 0.0;
        }

        $totalUsers = $company->users()->count();
        $activeUsers = $company->activeUsers()->count();

        return $totalUsers > 0 ? round(($activeUsers / $totalUsers) * 100, 2) : 0.0;
    }

    /**
     * Calculate growth rate for company
     */
    private function calculateGrowthRate(int $companyId): float
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return 0.0;
        }

        $currentMonth = $company->users()
            ->whereYear('created_at', now()->year)
            ->whereMonth('created_at', now()->month)
            ->count();

        $lastMonth = $company->users()
            ->whereYear('created_at', now()->subMonth()->year)
            ->whereMonth('created_at', now()->subMonth()->month)
            ->count();

        return $lastMonth > 0 ? round((($currentMonth - $lastMonth) / $lastMonth) * 100, 2) : 0.0;
    }

    /**
     * Calculate retention rate for company
     */
    private function calculateRetentionRate(int $companyId): float
    {
        $company = $this->companyRepository->findById($companyId);
        if (!$company) {
            return 0.0;
        }

        $totalUsers = $company->users()->count();
        $verifiedUsers = $company->verifiedUsers()->count();

        return $totalUsers > 0 ? round(($verifiedUsers / $totalUsers) * 100, 2) : 0.0;
    }

    /**
     * Calculate verification rate for company
     */
    private function calculateVerificationRate(int $companyId): float
    {
        return $this->calculateRetentionRate($companyId); // Same as retention rate in this context
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit