CompanySummaryController.php 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Controllers;
  3. use Libs\ResponseLib;
  4. use Models\CompanySummaryModel;
  5. use Psr\Http\Message\ServerRequestInterface;
  6. class CompanySummaryController
  7. {
  8. private CompanySummaryModel $summaryModel;
  9. public function __construct()
  10. {
  11. $this->summaryModel = new CompanySummaryModel();
  12. }
  13. public function __invoke(ServerRequestInterface $request)
  14. {
  15. $companyId = (int)($request->getAttribute('api_company_id') ?? 0);
  16. if ($companyId <= 0) {
  17. return ResponseLib::sendFail('Authenticated company not found', [], 'E_VALIDATE')->withStatus(401);
  18. }
  19. try {
  20. $summary = $this->summaryModel->getSummary($companyId);
  21. } catch (\Throwable $e) {
  22. return ResponseLib::sendFail('Failed to fetch company summary: ' . $e->getMessage(), [], 'E_DATABASE')->withStatus(500);
  23. }
  24. return ResponseLib::sendOk([
  25. 'company_id' => $companyId,
  26. 'summary' => $summary,
  27. ], 'S_COMPANY_SUMMARY');
  28. }
  29. }