ClockworkController::sendTextMessage()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use App\Http\Requests;
7
use MJErwin\Clockwork\ClockworkClient;
8
use MJErwin\Clockwork\Message;
9
10
class ClockworkController extends Controller
11
{
12
    /**
13
     * @var mixed
14
     */
15
    protected $apiKey;
16
17
    /**
18
     * @var ClockworkClient
19
     */
20
    protected $client;
21
22
    const MSG_NUMBER = '07700900123';
23
24
    /**
25
     * Initialize ClockworkController
26
     */
27
    public function __construct()
28
    {
29
        $this->apiKey = env('CLOCKWORK_API_KEY');
30
        $this->client = new ClockworkClient($this->apiKey);
31
        $this->message = new Message();
0 ignored issues
show
Bug introduced by
The property message does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        $this->message->setNumber(self::MSG_NUMBER);
33
        $this->message->setContent(trans('texts.message.sample_body'));
0 ignored issues
show
Bug introduced by
It seems like trans('texts.message.sample_body') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, MJErwin\Clockwork\Message::setContent() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
34
    }
35
36
    /**
37
     * Return all data to the Clockwork API dashboard
38
     */
39
    public function getPage()
40
    {
41
        return view('api.clockwork');
42
    }
43
44
    /**
45
     * Send a Text Message
46
     *
47
     * @param  Request $request
48
     *
49
     * @return \Illuminate\Http\RedirectResponse
50
     */
51
    public function sendTextMessage(Request $request)
52
    {
53
        $this->validate($request, [
54
            'telephone'  => 'required'
55
        ]);
56
57
        $response = $this->client->sendMessage($this->message);
58
59
        if ($response->getMessageId()) {
60
            return redirect()->back()->with('info', trans('texts.message.sent_success'));
61
        }
62
63
        return redirect()->back()->with('errors', trans('texts.message.sent_failed'));
64
    }
65
}
66