Completed
Pull Request — master (#14)
by
unknown
118:26 queued 101:49
created

TriggerWebhooksJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 2
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Mpociot\CaptainHook\Jobs;
3
4
use Carbon\Carbon;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Middleware;
7
use GuzzleHttp\Promise\Promise;
8
use Illuminate\Bus\Queueable;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
use Illuminate\Queue\InteractsWithQueue;
11
use Illuminate\Queue\SerializesModels;
12
use Mpociot\CaptainHook\CaptainHookLog;
13
use Psr\Http\Message\RequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
class TriggerWebhooksJob implements ShouldQueue
17
{
18
    use Queueable, InteractsWithQueue, SerializesModels;
19
20
21
    /**
22
     * All the webhooks that should be executed.
23
     *
24
     * @var array|\Illuminate\Support\Collection
25
     */
26
    protected $webhooks;
27
28
    /**
29
     * The event data to be posted to our hooks.
30
     *
31
     * @var mixed
32
     */
33
    protected $eventData;
34
35
    /**
36
     * Create a new job instance.
37
     *
38
     * @param array|\Illuminate\Support\Collection $wekbhooks
0 ignored issues
show
Documentation introduced by
There is no parameter named $wekbhooks. Did you maybe mean $webhooks?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
39
     * @param $eventData
40
     */
41
    public function __construct($webhooks, $eventData)
42
    {
43
        $this->eventData = $eventData;
44
        $this->webhooks = $webhooks;
45
    }
46
47
    /**
48
     * Execute the job.
49
     *
50
     * @return void
51
     */
52
    public function handle()
53
    {
54
        $config = app('Illuminate\Contracts\Config\Repository');
55
        $client = app(Client::class);
56
57
        if (($logging = $config->get('captain_hook.log.active') && $config->get('queue.driver') != 'sync') &&
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: $logging = ($config->get...eue.driver') != 'sync'), Probably Intended Meaning: ($logging = $config->get...ueue.driver') != 'sync'
Loading history...
58
            $config->get('captain_hook.log.storage_time') != -1) {
59
            CaptainHookLog::where('updated_at', '<', Carbon::now()->subHours($config->get('captain_hook.log.storage_time')))->delete();
60
        }
61
62
        foreach ($this->webhooks as $webhook) {
63
            if ($logging) {
64
                $log = new CaptainHookLog([
65
                    'webhook_id' => $webhook[ 'id' ],
66
                    'url' => $webhook[ 'url' ],
67
                ]);
68
                $middleware = Middleware::tap(function (RequestInterface $request, $options) use ($log) {
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
                    $log->payload_format = isset($request->getHeader('Content-Type')[0]) ? $request->getHeader('Content-Type')[0] : null;
0 ignored issues
show
Documentation introduced by
The property payload_format does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
70
                    $log->payload = $request->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property payload does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
71
                }, function ($request, $options, Promise $response) use ($log) {
72
                    $response->then(function (ResponseInterface $response) use ($log) {
73
                        $log->status = $response->getStatusCode();
0 ignored issues
show
Documentation introduced by
The property status does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
74
                        $log->response = $response->getBody()->getContents();
0 ignored issues
show
Documentation introduced by
The property response does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
75
                        $log->response_format = $log->payload_format = isset($response->getHeader('Content-Type')[0]) ? $response->getHeader('Content-Type')[0] : null;
0 ignored issues
show
Documentation introduced by
The property response_format does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property payload_format does not exist on object<Mpociot\CaptainHook\CaptainHookLog>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
76
77
                        $log->save();
78
                    });
79
                });
80
81
                $client->post($webhook[ 'url' ], [
82
                    'body' => $this->eventData,
83
                    'handler' => $middleware($client->getConfig('handler')),
84
                ]);
85
            } else {
86
                $client->postAsync($webhook[ 'url' ], [
87
                    'body' => $this->eventData,
88
                    'verify' => false,
89
                    'future' => true,
90
                ]);
91
            }
92
        }
93
    }
94
}
95