| 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/Controllers/ |
Upload File : |
<?php
namespace App\Controllers;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use App\Services\CompanyService;
class CompanyController extends BaseController
{
private CompanyService $companyService;
public function __construct()
{
$this->companyService = $this->getService(CompanyService::class);
}
/**
* Get all companies
*/
public function index(Request $request, Response $response): Response
{
try {
$pagination = $this->getPaginationParams($request);
$filters = $this->getFilterParams($request, [
'domain', 'search'
]);
$companies = $this->companyService->getCompanies(
$pagination['page'],
$pagination['limit'],
$filters
);
return $this->successResponse($response, $this->transformPaginatedResults($companies));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get company by ID
*/
public function show(Request $request, Response $response, array $args): Response
{
try {
$id = (int) $args['id'];
$company = $this->companyService->getCompanyById($id);
if (!$company) {
return $this->notFoundResponse($response, 'Company not found');
}
return $this->successResponse($response, $company->toArray());
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Create a new company
*/
public function store(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
$company = $this->companyService->createCompany($data);
return $this->successResponse($response, $company->toArray(), 'Company created successfully', 201);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Update a company
*/
public function update(Request $request, Response $response, array $args): Response
{
try {
$id = (int) $args['id'];
$data = $request->getParsedBody();
$updated = $this->companyService->updateCompany($id, $data);
if (!$updated) {
return $this->notFoundResponse($response, 'Company not found');
}
return $this->successResponse($response, null, 'Company updated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Delete a company
*/
public function destroy(Request $request, Response $response, array $args): Response
{
try {
$id = (int) $args['id'];
$deleted = $this->companyService->deleteCompany($id);
if (!$deleted) {
return $this->notFoundResponse($response, 'Company not found');
}
return $this->successResponse($response, null, 'Company deleted successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Search companies
*/
public function search(Request $request, Response $response): Response
{
try {
$query = $request->getQueryParams()['q'] ?? '';
$filters = $this->getFilterParams($request, ['domain']);
if (empty($query)) {
return $this->badRequestResponse($response, 'Search query is required');
}
$companies = $this->companyService->searchCompanies($query, $filters);
return $this->successResponse($response, $companies);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Filter companies
*/
public function filter(Request $request, Response $response): Response
{
try {
$filters = $this->getFilterParams($request, ['domain', 'search']);
$pagination = $this->getPaginationParams($request);
$companies = $this->companyService->filterCompanies(
$filters,
$pagination['page'],
$pagination['limit']
);
return $this->successResponse($response, $this->transformPaginatedResults($companies));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Add social link to company
*/
public function addSocialLink(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$data = $request->getParsedBody();
if (!isset($data['platform']) || !isset($data['url'])) {
return $this->validationErrorResponse($response, [
'platform' => 'Platform is required',
'url' => 'URL is required'
]);
}
$socialLink = $this->companyService->addSocialLink($companyId, $data);
return $this->successResponse($response, $socialLink->toArray(), 'Social link added successfully', 201);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Remove social link from company
*/
public function removeSocialLink(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$linkId = (int) $args['link_id'];
$removed = $this->companyService->removeSocialLink($companyId, $linkId);
if (!$removed) {
return $this->notFoundResponse($response, 'Social link not found');
}
return $this->successResponse($response, null, 'Social link removed successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Add product link to company
*/
public function addProductLink(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$data = $request->getParsedBody();
if (!isset($data['platform']) || !isset($data['url'])) {
return $this->validationErrorResponse($response, [
'platform' => 'Platform is required',
'url' => 'URL is required'
]);
}
$productLink = $this->companyService->addProductLink($companyId, $data);
return $this->successResponse($response, $productLink->toArray(), 'Product link added successfully', 201);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Remove product link from company
*/
public function removeProductLink(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$linkId = (int) $args['link_id'];
$removed = $this->companyService->removeProductLink($companyId, $linkId);
if (!$removed) {
return $this->notFoundResponse($response, 'Product link not found');
}
return $this->successResponse($response, null, 'Product link removed successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get company statistics
*/
public function statistics(Request $request, Response $response): Response
{
try {
$stats = $this->companyService->getCompanyStatistics();
return $this->successResponse($response, $stats);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get companies by domain
*/
public function byDomain(Request $request, Response $response, array $args): Response
{
try {
$domain = $args['domain'];
$company = $this->companyService->getCompanyByDomain($domain);
if (!$company) {
return $this->notFoundResponse($response, 'Company not found');
}
return $this->successResponse($response, $company->toArray());
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk activate companies
*/
public function bulkActivate(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['company_ids']) || !is_array($data['company_ids'])) {
return $this->validationErrorResponse($response, ['company_ids' => 'Company IDs array is required']);
}
$count = $this->companyService->bulkActivate($data['company_ids']);
return $this->successResponse($response, [
'activated_count' => $count
], 'Companies activated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk deactivate companies
*/
public function bulkDeactivate(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['company_ids']) || !is_array($data['company_ids'])) {
return $this->validationErrorResponse($response, ['company_ids' => 'Company IDs array is required']);
}
$count = $this->companyService->bulkDeactivate($data['company_ids']);
return $this->successResponse($response, [
'deactivated_count' => $count
], 'Companies deactivated successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Bulk delete companies
*/
public function bulkDelete(Request $request, Response $response): Response
{
try {
$data = $request->getParsedBody();
if (!isset($data['company_ids']) || !is_array($data['company_ids'])) {
return $this->validationErrorResponse($response, ['company_ids' => 'Company IDs array is required']);
}
$count = $this->companyService->bulkDelete($data['company_ids']);
return $this->successResponse($response, [
'deleted_count' => $count
], 'Companies deleted successfully');
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get company users
*/
public function getUsers(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$pagination = $this->getPaginationParams($request);
$users = $this->companyService->getCompanyUsers($companyId, $pagination['page'], $pagination['limit']);
return $this->successResponse($response, $this->transformPaginatedResults($users));
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
/**
* Get company analytics
*/
public function analytics(Request $request, Response $response, array $args): Response
{
try {
$companyId = (int) $args['id'];
$analytics = $this->companyService->getCompanyAnalytics($companyId);
return $this->successResponse($response, $analytics);
} catch (\Exception $e) {
return $this->handleException($response, $e);
}
}
}