MessageService   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 21
lcom 1
cbo 7
dl 0
loc 118
rs 10
c 1
b 0
f 1
ccs 0
cts 84
cp 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
C handle() 0 46 9
C jobDispatcher() 0 32 7
A eventRulesParser() 0 18 4
A deleteJobByModel() 0 4 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: alive
5
 * Date: 4/28/18
6
 * Time: 7:17 AM
7
 */
8
9
namespace Alive2212\LaravelMessageService;
10
11
12
use App\Jobs\MessageServiceEmailJob;
13
use App\Jobs\MessageServiceNotificationJob;
14
use App\Jobs\MessageServiceScopeJob;
15
use App\Jobs\MessageServiceSmsJob;
16
use App\Jobs\MessageServiceSocialJob;
17
use Carbon\Carbon;
18
19
class MessageService
20
{
21
    public function handle($model, $eventType)
22
    {
23
        // get model class name
24
        $modelClassName = get_class($model);
25
        $event = new AliveMessageEvent();
26
        $event = $event->where([
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Alive2212\Laravel...vice\AliveMessageEvent>? 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...
27
            ['model', '=', $modelClassName],
28
        ])->with('messageProcesses');
29
        $eventParams = $event->get()->toArray();
30
        if (count($eventParams)) {
31
            foreach ($eventParams as $eventParam) {
32
                // handle available_at update and delete record
33
                $dirty = $model->getDirty();
34
                if (isset($dirty['available_at'])) {
35
                    switch ($eventType) {
36
                        // TODO more performance need
37
38
                        case 'Updating':
39
                            $this->deleteJobByModel($model);
40
                            break;
41
                        case 'Deleting':
42
                            $this->deleteJobByModel($model);
43
                            break;
44
                    }
45
                }
46
47
48
                if ($eventParam['type'] == $eventType) {
49
                    $eventRules = json_decode($eventParam['rules'], true);
50
                    $whereParams = $this->eventRulesParser($eventRules, $model->id);
51
                    if (count($whereParams)) {
52
                        $currentModel = (new $modelClassName())
53
                            ->where($whereParams);
54
                        $currentModelParams = $currentModel->get()->toArray()[0];
55
                    } else {
56
                        $currentModelParams = $model->toArray();
57
                    }
58
                    if (count($currentModelParams)) {
59
                        // this place is where process must be done
60
                        $processes = $eventParam['message_processes'];
61
                        $this->jobDispatcher($processes, $currentModelParams);
62
                    }
63
                }
64
            }
65
        }
66
    }
67
68
    /**
69
     * @param $processes
70
     * @param $currentModelParams
71
     */
72
    public function jobDispatcher($processes, $currentModelParams)
73
    {
74
        foreach ($processes as $process) {
75
            switch ($process['type']) {
76
                case "Sms":
77
                    dispatch(new MessageServiceSmsJob($process, $currentModelParams));
78
                    break;
79
80
                case "Notification":
81
                    $launchTimeKey = $process['launch_time'];
82
                    if (!is_null($launchTimeKey)) {
83
                        $launchDateTimeTimeStamp = $currentModelParams[$launchTimeKey];
84
                        $launchDateTime = Carbon::createFromTimestamp($launchDateTimeTimeStamp);
85
                        dispatch((new MessageServiceNotificationJob($process, $currentModelParams))->delay($launchDateTime));
86
                    } else {
87
                        dispatch(new MessageServiceNotificationJob($process, $currentModelParams));
88
                    }
89
                    break;
90
91
                case "Social":
92
                    dispatch(new MessageServiceSocialJob($process, $currentModelParams));
93
                    break;
94
95
                case "Email":
96
                    dispatch(new MessageServiceEmailJob($process, $currentModelParams));
97
                    break;
98
99
                default:
100
                    dispatch(new MessageServiceScopeJob($process, $currentModelParams));
101
            }
102
        }
103
    }
104
105
    /**
106
     * @param $eventRules
107
     * @param $modelId
108
     * @return mixed
109
     */
110
    public function eventRulesParser($eventRules, $modelId)
111
    {
112
        $whereParams = [];
113
114
        // add id after created variable
115
        if (!is_null($modelId)) {
116
            array_push($whereParams, ['id', '=', $modelId]);
117
        }
118
119
120
        if (is_array($eventRules)) {
121
            foreach ($eventRules as $eventRule) {
122
                // TODO create multiple deep where
123
                array_push($whereParams, [$eventRule[0], $eventRule[1], $eventRule[2]]);
124
            }
125
        }
126
        return $whereParams;
127
    }
128
129
    /**
130
     * @param $model
131
     */
132
    public function deleteJobByModel($model)
133
    {
134
        $job = \DB::table('jobs')->where('available_at', $model->getOriginal('available_at'))->delete();
0 ignored issues
show
Unused Code introduced by
$job is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135
    }
136
}