Test Failed
Push — develop ( f4fb9d...b64f53 )
by Nikita
06:04 queued 11s
created

ServersTasksRepository::convertModelToArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 15
rs 9.8333
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Gameap\Repositories;
4
5
use Carbon\CarbonInterval;
6
use Gameap\Exceptions\Repositories\RepositoryValidationException;
7
use Gameap\Models\ServerTask;
8
9
class ServersTasksRepository
10
{
11
    const MIN_PERIOD_IN_SECONDS = 600;
12
13
    /** @var ServerTask */
14
    protected $model;
15
16
    public function __construct(ServerTask $serverTask)
17
    {
18
        $this->model = $serverTask;
19
    }
20
21
    /**
22
     * @param int $serverId
23
     * @return array
24
     */
25
    public function getTasks(int $serverId): array
26
    {
27
        $tasks = [];
28
29
        /** @var ServerTask $task */
30
        foreach (ServerTask::where('server_id', $serverId)->get() as $task) {
31
            $tasks[] = $this->convertModelToArray($task);
32
        }
33
34
        return $tasks;
35
    }
36
37
    /**
38
     * @param int $taskId
39
     * @return array
40
     */
41
    public function get(int $taskId): array
42
    {
43
        return $this->convertModelToArray(ServerTask::find($taskId));
44
    }
45
46
    /**
47
     * @param array $task
48
     * @return int
49
     * @throws RepositoryValidationException
50
     */
51
    public function store(array $task): int
52
    {
53
        $this->validate($task);
54
        $convertedTask = $this->convert($task);
55
56
        $serverTask = ServerTask::create($convertedTask);
57
58
        return $serverTask->id;
59
    }
60
61
    /**
62
     * @param int $taskId
63
     * @param array $task
64
     * @throws RepositoryValidationException
65
     */
66
    public function update(int $taskId, array $task)
67
    {
68
        $this->validate($task);
69
        $convertedTask = $this->convert($task);
70
71
        ServerTask::where('id', $taskId)->update($convertedTask);
72
    }
73
74
    /**
75
     * @param array $task
76
     * @return array
77
     * @throws RepositoryValidationException
78
     */
79
    private function convert(array $task) {
80
81
        if ($task['repeat'] != 1) {
82
            if (empty($task['repeat_period'])) {
83
                throw new RepositoryValidationException(__('servers_tasks.errors.empty_period'));
0 ignored issues
show
Bug introduced by
It seems like __('servers_tasks.errors.empty_period') can also be of type array and array; however, parameter $message of Gameap\Exceptions\Reposi...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
                throw new RepositoryValidationException(/** @scrutinizer ignore-type */ __('servers_tasks.errors.empty_period'));
Loading history...
84
            }
85
86
            $task['repeat_period'] = CarbonInterval::fromString($task['repeat_period'])
87
                ->locale('en')
88
                ->cascade()
89
                ->totalSeconds;
90
91
            if ($task['repeat_period'] < self::MIN_PERIOD_IN_SECONDS) {
92
                throw new RepositoryValidationException(__('servers_tasks.errors.minimum_period'));
93
            }
94
        } else {
95
            $task['repeat_period'] = 0;
96
        }
97
98
        return $task;
99
    }
100
101
    /**
102
     * @param array $task
103
     * @throws RepositoryValidationException
104
     */
105
    private function validate(array $task) {
106
        if (empty($task) || empty($task['command'])) {
107
            throw new RepositoryValidationException(__('servers_tasks.errors.empty_command'));
0 ignored issues
show
Bug introduced by
It seems like __('servers_tasks.errors.empty_command') can also be of type array and array; however, parameter $message of Gameap\Exceptions\Reposi...xception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
            throw new RepositoryValidationException(/** @scrutinizer ignore-type */ __('servers_tasks.errors.empty_command'));
Loading history...
108
        }
109
    }
110
111
    /**
112
     * @param ServerTask $task
113
     * @return array
114
     */
115
    private function convertModelToArray(ServerTask $task)
116
    {
117
        return [
118
            'id'            => $task->id,
119
            'command'       => $task->command,
120
            'server_id'     => $task->server_id,
121
            'repeat'        => $task->repeat,
122
            'repeat_period' => CarbonInterval::seconds($task->repeat_period)
0 ignored issues
show
Bug Best Practice introduced by
The method Carbon\CarbonInterval::seconds() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
            'repeat_period' => CarbonInterval::/** @scrutinizer ignore-call */ seconds($task->repeat_period)
Loading history...
123
                ->locale('en')
124
                ->cascade()
125
                ->forHumans(),
126
            'execute_date'  => $task->execute_date,
127
            'payload'       => $task->payload,
128
            'created_at'    => $task->created_at,
129
            'updated_at'    => $task->updated_at
130
        ];
131
    }
132
}