Passed
Push — master ( fbdfbb...422ed8 )
by Morten Poul
01:38
created

TaskLoader   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 36
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadFor() 0 27 6
1
<?php
2
3
namespace Signifly\SchedulingTasks;
4
5
use ReflectionClass;
6
use Illuminate\Support\Str;
7
use Symfony\Component\Finder\Finder;
8
use Illuminate\Console\Scheduling\Schedule;
9
use Illuminate\Contracts\Foundation\Application;
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
            $taskClass = $namespace.str_replace(
32
                ['/', '.php'],
33
                ['\\', ''],
34
                Str::after($taskFile->getPathname(), app_path().DIRECTORY_SEPARATOR)
35
            );
36
37
            if (in_array($taskClass, $exclude)) {
38
                continue;
39
            }
40
41
            if (is_subclass_of($taskClass, TaskContract::class) &&
42
                ! (new ReflectionClass($taskClass))->isAbstract()) {
43
                $task = new $taskClass;
44
45
                // Invoke task
46
                $task($schedule);
47
            }
48
        }
49
    }
50
}
51