Passed
Push — master ( ef93dc...4f7931 )
by
unknown
13:58
created

SmsController::postSendCode()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18

Duplication

Lines 6
Ratio 33.33 %

Code Coverage

Tests 8
CRAP Score 5.0342

Importance

Changes 0
Metric Value
dl 6
loc 18
rs 9.3554
c 0
b 0
f 0
ccs 8
cts 9
cp 0.8889
cc 5
nc 4
nop 0
crap 5.0342
1
<?php
2
3
/*
4
 * This file is part of ibrand/laravel-sms.
5
 *
6
 * (c) iBrand <https://www.ibrand.cc>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace iBrand\Sms;
13
14
use Illuminate\Routing\Controller;
15
use iBrand\Sms\Facade as Sms;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, iBrand\Sms\Sms.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
16
17
/**
18
 * Class SmsController
19
 * @package iBrand\Sms
20
 */
21
class SmsController extends Controller
22
{
23
    /**
24
     * @return \Illuminate\Http\JsonResponse
25
     */
26 2
    public function postSendCode()
27
    {
28 2
        $mobile = request('mobile');
29
30 2
        if(!config('app.debug') && !Sms::verifyMobile($mobile)){
31 2
            return response()->json(['success' => false, 'message' => '无效手机号码']);
32
        }
33
34 2 View Code Duplication
        if (!Sms::canSend($mobile)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
35 2
            return response()->json(['success' => false, 'message' => '每60秒发送一次']);
36
        }
37
38 2 View Code Duplication
        if (!Sms::send($mobile)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
            return response()->json(['success' => false, 'message' => '短信发送失败']);
40
        }
41
42 2
        return response()->json(['success' => true, 'message' => '短信发送成功']);
43
    }
44
45
    /**
46
     * laravel sms code info.
47
     */
48 2
    public function info()
49
    {
50 2
        $html = '<meta charset="UTF-8"/><h2 align="center" style="margin-top: 30px;margin-bottom: 0;">iBrand Laravel Sms</h2>';
51 2
        $html .= '<p style="margin-bottom: 30px;font-size: 13px;color: #888;" align="center">' . 1.0 . '</p>';
52 2
        $html .= '<p><a href="https://github.com/ibrandcc/laravel-sms" target="_blank">ibrand laravel-sms源码</a>托管在GitHub,欢迎你的使用。如有问题和建议,欢迎提供issue。</p>';
53 2
        $html .= '<hr>';
54 2
        $html .= '<p>你可以在调试模式(设置config/app.php中的debug为true)下查看到存储在存储器中的验证码短信/语音相关数据:</p>';
55 2
        echo $html;
56 2
        if (config('app.debug')) {
57
58 1
            $key = md5('ibrand.sms.' . request('mobile'));
59
60 1
            dump(Sms::getStorage()->get($key, ''));
61
        } else {
62 1
            echo '<p align="center" style="color: red;">现在是非调试模式,无法查看调试数据</p>';
63
        }
64
    }
65
}