TaskLoader   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
eloc 17
c 9
b 1
f 1
dl 0
loc 35
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadFor() 0 26 6
1
<?php
2
3
namespace Signifly\SchedulingTasks;
4
5
use Illuminate\Console\Scheduling\Schedule;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Support\Str;
8
use ReflectionClass;
9
use Symfony\Component\Finder\Finder;
10
11
class TaskLoader
12
{
13
    protected $app;
14
15
    public function __construct(Application $app)
16
    {
17
        $this->app = $app;
18
    }
19
20
    public function loadFor(Schedule $schedule, array $exclude = [])
21
    {
22
        $namespace = $this->app->getNamespace();
23
24
        $path = $this->app->path('Console/Tasks');
25
26
        if (! is_dir($path)) {
27
            return;
28
        }
29
30
        foreach ((new Finder)->in($path)->files() as $taskFile) {
31
            $task = $namespace.str_replace(
32
                ['/', '.php'],
33
                ['\\', ''],
34
                Str::after($taskFile->getPathname(), $this->app->path().DIRECTORY_SEPARATOR)
35
            );
36
37
            if (in_array($task, $exclude)) {
38
                continue;
39
            }
40
41
            if (is_subclass_of($task, TaskContract::class) &&
42
                ! (new ReflectionClass($task))->isAbstract()) {
43
44
                // Invoke task
45
                app($task)($schedule);
46
            }
47
        }
48
    }
49
}
50