ServersTasksRepository::getTasks()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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
    public const MIN_PERIOD_IN_SECONDS = 600;
12
13
    /** @var ServerTask */
14
    protected $model;
15
16 33
    public function __construct(ServerTask $serverTask)
17
    {
18 33
        $this->model = $serverTask;
19 33
    }
20
21
    /**
22
     * @param int $serverId
23
     * @return array
24
     */
25 3
    public function getTasks(int $serverId): array
26
    {
27 3
        $tasks = [];
28
29
        /** @var ServerTask $task */
30 3
        foreach (ServerTask::where('server_id', $serverId)->get() as $task) {
31 3
            $tasks[] = $this->convertModelToArray($task);
32
        }
33
34 3
        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));
0 ignored issues
show
Bug introduced by
It seems like Gameap\Models\ServerTask::find($taskId) can also be of type null; however, parameter $task of Gameap\Repositories\Serv...::convertModelToArray() does only seem to accept Gameap\Models\ServerTask, 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

43
        return $this->convertModelToArray(/** @scrutinizer ignore-type */ ServerTask::find($taskId));
Loading history...
44
    }
45
46
    /**
47
     * @param array $task
48
     * @return int
49
     * @throws RepositoryValidationException
50
     */
51 15
    public function store(array $task): int
52
    {
53 15
        $this->validate($task);
54 12
        $convertedTask = $this->convert($task);
55
56 6
        $serverTask = ServerTask::create($convertedTask);
57
58 6
        return $serverTask->id;
59
    }
60
61
    /**
62
     * @param int $taskId
63
     * @param array $task
64
     * @throws RepositoryValidationException
65
     */
66 15
    public function update(int $taskId, array $task): void
67
    {
68 15
        $this->validate($task);
69 12
        $convertedTask = $this->convert($task);
70
71 6
        ServerTask::where('id', $taskId)->update($convertedTask);
72 6
    }
73
74
    /**
75
     * @param array $task
76
     * @return array
77
     * @throws RepositoryValidationException
78
     */
79 24
    private function convert(array $task)
80
    {
81 24
        if ($task['repeat'] != 1) {
82 18
            if (empty($task['repeat_period'])) {
83 6
                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 12
            $task['repeat_period'] = CarbonInterval::fromString($task['repeat_period'])
87 12
                ->locale('en')
88 12
                ->cascade()
89 12
                ->totalSeconds;
90
91 12
            if ($task['repeat_period'] < self::MIN_PERIOD_IN_SECONDS) {
92 6
                throw new RepositoryValidationException(__('servers_tasks.errors.minimum_period'));
93
            }
94
        } else {
95 6
            $task['repeat_period'] = 0;
96
        }
97
98 12
        return $task;
99
    }
100
101
    /**
102
     * @param array $task
103
     * @throws RepositoryValidationException
104
     */
105 30
    private function validate(array $task): void
106 30
    {
107 6
        if (empty($task) || empty($task['command'])) {
108
            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

108
            throw new RepositoryValidationException(/** @scrutinizer ignore-type */ __('servers_tasks.errors.empty_command'));
Loading history...
109 24
        }
110
    }
111
112
    /**
113
     * @param ServerTask $task
114
     * @return array
115 3
     */
116
    private function convertModelToArray(ServerTask $task)
117
    {
118 3
        return [
119 3
            'id'            => $task->id,
120 3
            'command'       => $task->command,
121 3
            'server_id'     => $task->server_id,
122 3
            'repeat'        => $task->repeat,
123 3
            'repeat_period' => CarbonInterval::seconds($task->repeat_period)
124 3
                ->locale('en')
125 3
                ->cascade()
126 3
                ->forHumans(),
127 3
            'execute_date' => $task->execute_date,
128 3
            'payload'      => $task->payload,
129 3
            'created_at'   => $task->created_at,
130
            'updated_at'   => $task->updated_at,
131
        ];
132
    }
133
}
134