Completed
Push — master ( 4fc937...686022 )
by Luca
03:20
created

src/Service.php (2 issues)

1
<?php
2
3
/**
4
 * WebhookManager
5
 *
6
 * @author: Luca Agnello <[email protected]>
7
 */
8
9
namespace Gnello\WebhookManager;
10
11
use Gnello\WebhookManager\Services\ServiceInterface;
12
13
/**
14
 * Class Service
15
 *
16
 * @package Gnello\WebhookManager
17
 */
18
class Service implements ServiceInterface
19
{
20
    /**
21
     * @var array
22
     */
23
    private $options;
24
25
    /**
26
     * @var array
27
     */
28
    private $headers = [];
29
30
    /**
31
     * @var mixed
32
     */
33
    private $payload;
34
35
    private $serviceConfig;
36
37
    /**
38
     * BitbucketService constructor.
39
     *
40
     * @param array $options
41
     */
42
    public function __construct(array $options)
43
    {
44
        $this->options = $options;
45
    }
46
47
    /**
48
     * @return array
49
     */
50
    public function getHeaders(): array
51
    {
52
        $this->headers['X-Event-Key'] = $_SERVER['HTTP_X_EVENT_KEY'];
53
        $this->headers['X-Hook-UUID'] = $_SERVER['HTTP_X_HOOK_UUID'];
54
        $this->headers['X-Request-UUID'] = $_SERVER['HTTP_X_REQUEST_UUID'];
55
        $this->headers['X-Attempt-Number'] = $_SERVER['X_ATTEMPT_NUMBER'];
56
57
        foreach ($this->serviceConfig->getHeaders() as $header) {
58
            $this->headers[$header] = $_SERVER[$header];
59
        }
60
61
        return $this->headers;
62
    }
63
64
    /**
65
     * @return string
66
     * @throws WebhookManagerException
67
     */
68 View Code Duplication
    public function getEvent(): string
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69
    {
70
        if (empty($this->headers)) {
71
            $this->getHeaders();
72
        }
73
74
        if (!isset($this->headers['X-Event-Key'])) {
75
            throw new WebhookManagerException("No event specified.");
76
        }
77
78
        return $this->headers['X-Event-Key'];
79
    }
80
81
    /**
82
     * @return mixed
83
     */
84 View Code Duplication
    public function getPayload()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        if (!isset($this->payload)) {
87
            $this->payload = json_decode(file_get_contents('php://input'), (bool) $this->options['json_decode_assoc']);
88
        }
89
90
        return $this->payload;
91
    }
92
}