Skip to content
Snippets Groups Projects
Select Git revision
  • a32288465b5c931cd1abbe50e19a3bd817ad7872
  • main default protected
  • issue-56
  • testing-systems
4 results

EventController.php

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EventController.php 1.23 KiB
    <?php
    namespace TracToGitlab;
    
    abstract class EventController extends Controller
    {
        private $payload = null;
    
        protected function verifySecret(string $secret): bool
        {
            return $_SERVER['HTTP_X_GITLAB_TOKEN'] === $secret;
        }
    
        protected function verifyEventType(string $type): bool
        {
            return $_SERVER['HTTP_X_GITLAB_EVENT'] === $type;
        }
    
        protected function getFromPayload(...$keys)
        {
            if ($this->payload === null) {
                $this->payload = $this->getPayloadFromRequest() ?? false;
            }
            if ($this->payload === false) {
                throw new \Exception('No payload detected');
            }
    
            $result = $this->payload;
            foreach ($keys as $key) {
                if (!isset($result[$key])) {
                    throw new \Exception("Invalid payload key {$key}");
                }
    
                $result = $result[$key];
            }
    
            return $result;
        }
    
        private function getPayloadFromRequest(): ?array
        {
            $input =file_get_contents('php://input');
            if (!$input) {
                return null;
            }
    
            $payload = json_decode($input, true);
            if (!$payload || !is_array($payload)) {
                return null;
            }
    
            return $payload;
        }
    }