Test Setup Failed
Push — master ( eddc54...b7ae74 )
by Php Easy Api
04:00
created

Schedule::cronjob_exists()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 19
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Resta\Console\Source\Schedule;
4
5
use Resta\Support\Utils;
6
use Resta\Console\ConsoleOutputter;
7
use Resta\Console\ConsoleListAccessor;
8
9
class Schedule extends ConsoleOutputter {
10
11
    use ConsoleListAccessor;
12
13
    /**
14
     * @var $type
0 ignored issues
show
Documentation Bug introduced by
The doc comment $type at position 0 could not be parsed: Unknown type name '$type' at position 0 in $type.
Loading history...
15
     */
16
    public $type = 'schedule';
17
18
    /**
19
     * @var $define
0 ignored issues
show
Documentation Bug introduced by
The doc comment $define at position 0 could not be parsed: Unknown type name '$define' at position 0 in $define.
Loading history...
20
     */
21
    public $define = 'creates schedule for application';
22
23
    /**
24
     * @var $commandRule
0 ignored issues
show
Documentation Bug introduced by
The doc comment $commandRule at position 0 could not be parsed: Unknown type name '$commandRule' at position 0 in $commandRule.
Loading history...
25
     */
26
    public $commandRule = ['schedule'];
27
28
    /**
29
     * @return void
30
     */
31
    public function create(){
32
33
        $schedulePath = app()->path()->schedule();
34
35
        if(!file_exists($schedulePath)){
36
            $this->directory['schedule'] = $schedulePath;
37
            $this->file->makeDirectory($this);
38
        }
39
40
        $this->argument['scheduleNamespace'] = app()->namespace()->schedule();
41
        $this->argument['scheduleClass'] = ucfirst($this->argument['schedule']).'';
42
        $this->argument['projectName'] = strtolower($this->projectName());
43
44
        $this->touch['schedule/schedule']= $schedulePath.'/'.$this->argument['schedule'].'.php';
45
46
47
        $this->file->touch($this);
48
49
        echo $this->classical(' > Schedule file called as "'.$this->argument['schedule'].'" has been successfully created in the '.$schedulePath.'');
50
    }
51
52
    /**
53
     * @return void
54
     */
55
    public function register()
56
    {
57
        $schedules = Utils::glob(app()->path()->schedule());
58
59
        if(isset($schedules[$this->argument['schedule']])){
60
61
            $scheduleNamespace = Utils::getNamespace($schedules[$this->argument['schedule']]);
62
            $scheduleInstance = app()->resolve($scheduleNamespace);
0 ignored issues
show
Unused Code introduced by
The assignment to $scheduleInstance is dead and can be removed.
Loading history...
63
64
            $command = '*/1 * * * * cd '.root.' && php api schedule run munch schedule:'.lcfirst($this->argument['schedule']).' >> /dev/null 2>&1';
65
66
            if($this->cronjob_exists($command)===false){
67
68
                $output = shell_exec('crontab -l');
69
                file_put_contents('/tmp/crontab.txt', $output.''.$command.''.PHP_EOL);
70
                exec('crontab /tmp/crontab.txt');
71
72
                echo $this->info('Cron has been added');
73
            }
74
75
        }
76
77
78
    }
79
80
    public function run()
81
    {
82
        $schedules = Utils::glob(app()->path()->schedule());
83
84
        if(isset($schedules[$this->argument['schedule']])){
85
            $scheduleNamespace = Utils::getNamespace($schedules[$this->argument['schedule']]);
86
            $scheduleInstance = app()->resolve($scheduleNamespace);
87
88
            $scheduleInstance->command();
89
        }
90
    }
91
92
    private function cronjob_exists($command){
93
94
        $cronjob_exists=false;
95
96
        exec('crontab -l', $crontab);
97
98
99
        if(isset($crontab)&&is_array($crontab)){
100
101
            $crontab = array_flip($crontab);
102
103
            if(isset($crontab[$command])){
104
105
                $cronjob_exists=true;
106
107
            }
108
109
        }
110
        return $cronjob_exists;
111
    }
112
113
}
114