Completed
Push — master ( fa6016...95ebcc )
by Elf
04:01
created

SendBearyChatJob::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 12
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 20
ccs 0
cts 18
cp 0
crap 42
rs 8.8571
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Services\BearyChat;
4
5
use Illuminate\Bus\Queueable;
6
use ElfSundae\BearyChat\Message;
7
use Illuminate\Queue\SerializesModels;
8
use Illuminate\Queue\InteractsWithQueue;
9
use Illuminate\Contracts\Queue\ShouldQueue;
10
11
class SendBearyChatJob implements ShouldQueue
12
{
13
    use InteractsWithQueue, Queueable, SerializesModels;
14
15
    /**
16
     * The BearyChat client.
17
     *
18
     * @var \ElfSundae\BearyChat\Client
19
     */
20
    protected $client;
21
22
    /**
23
     * The Message instance to be sent.
24
     *
25
     * @var \ElfSundae\BearyChat\Message
26
     */
27
    protected $message;
28
29
    /**
30
     * Create a new job instance.
31
     *
32
     * @param  mixed  $message  A Message instance, or parameters which can be handled
33
     *                          by the `send` method of a Message instance.
34
     */
35
    public function __construct($message = null)
36
    {
37
        if ($message instanceof Message) {
38
            $this->message = $message;
39
        } elseif (is_string($message)) {
40
            $this->text($message);
1 ignored issue
show
Documentation Bug introduced by
The method text does not exist on object<ElfSundae\Laravel...yChat\SendBearyChatJob>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
41
42
            if (func_num_args() > 1) {
43
                if (is_bool(func_get_arg(1))) {
44
                    $this->markdown(func_get_arg(1));
1 ignored issue
show
Documentation Bug introduced by
The method markdown does not exist on object<ElfSundae\Laravel...yChat\SendBearyChatJob>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
45
46
                    if (func_num_args() > 2) {
47
                        $this->notification(func_get_arg(2));
1 ignored issue
show
Documentation Bug introduced by
The method notification does not exist on object<ElfSundae\Laravel...yChat\SendBearyChatJob>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
48
                    }
49
                } else {
50
                    call_user_func_array([$this, 'add'], array_slice(func_get_args(), 1));
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * Execute the job.
58
     *
59
     * @return void
60
     */
61
    public function handle()
62
    {
63
        if ($this->client) {
64
            $this->client->sendMessage($this->message);
65
        } elseif ($this->message) {
66
            $this->message->send();
67
        }
68
    }
69
70
    /**
71
     * Set the client with client name.
72
     *
73
     * @param  string  $name
74
     * @return $this
75
     */
76
    public function client($name)
77
    {
78
        $this->client = bearychat($name);
79
80
        return $this;
81
    }
82
83
    /**
84
     * Any unhandled methods will be sent to the Message instance.
85
     *
86
     * @param  string  $method
87
     * @param  array  $parameters
88
     * @return $this
89
     */
90
    public function __call($method, $parameters)
91
    {
92
        if (! $this->message) {
93
            $this->message = new Message($this->client ?: bearychat());
94
        }
95
96
        call_user_func_array([$this->message, $method], $parameters);
97
98
        return $this;
99
    }
100
}
101