Completed
Push — master ( 75c543...b51150 )
by max
02:14
created

Config::__construct()   C

Complexity

Conditions 13
Paths 64

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 31
rs 5.1234
cc 13
eloc 16
nc 64
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace T4web\Cron;
4
5
use Zend\Stdlib;
6
use T4web\Cron\Exception\RuntimeException;
7
use T4web\Cron\Log\FileSystem;
8
9
class Config
10
{
11
    /**
12
     * Array of shell jobs.
13
     *
14
     * @var array
15
     */
16
    protected $jobs = [];
17
18
    /**
19
     * PHP executable path for using shell jobs. Defaults 'php'.
20
     *
21
     * @var string
22
     */
23
    protected $phpPath = 'php';
24
25
    /**
26
     * Base path for script. Defaults 'getcwd()/public/'.
27
     *
28
     * @var string
29
     */
30
    protected $scriptPath = null;
31
32
    /**
33
     * Timeout in seconds for the process. null for no timeout.
34
     *
35
     * @var int
36
     */
37
    protected $timeout = null;
38
39
    /**
40
     * @var string
41
     */
42
    protected $logDirectory;
43
44
    /**
45
     * @var FileSystem
46
     */
47
    protected $fileSystem;
48
49
    /**
50
     * Config constructor.
51
     *
52
     * @param array           $config
53
     * @param FileSystem|null $filesystem
54
     */
55
    public function __construct(array $config = [], FileSystem $filesystem = null)
56
    {
57
        if (!$filesystem) {
58
            $filesystem = new FileSystem();
59
        }
60
61
        $this->fileSystem = $filesystem;
62
63
        if (isset($config['phpPath']) && !empty($config['phpPath'])) {
64
            $this->phpPath = $config['phpPath'];
65
        }
66
67
        if (isset($config['scriptPath']) && !empty($config['scriptPath'])) {
68
            $this->scriptPath = $config['scriptPath'];
69
        }
70
71
        if (isset($config['jobs']) && !empty($config['jobs']) && is_array($config['jobs'])) {
72
            $this->jobs = $config['jobs'];
73
        }
74
75
        if (isset($config['timeout']) && !empty($config['timeout'])) {
76
            $this->timeout = $config['timeout'];
77
        }
78
79
        $logDirectory = null;
80
        if (isset($config['log-directory']) && !empty($config['log-directory'])) {
81
            $logDirectory = $config['log-directory'];
82
        }
83
84
        $this->logDirectory = $this->prepareLogDirectory($logDirectory);
85
    }
86
87
    private function prepareLogDirectory($logDirectory)
88
    {
89
        if (empty($logDirectory)) {
90
            $logDirectory = getcwd() . '/data';
91
        }
92
93
        $logDirectory = rtrim($logDirectory, '/');
94
95
        if (!$this->fileSystem->isWritable($logDirectory)) {
96
            throw new RuntimeException("Directory $logDirectory must be writable");
97
        }
98
99
        return $logDirectory;
100
    }
101
102
    /**
103
     * @return string
104
     */
105
    public function getLogDirectory()
106
    {
107
        return $this->logDirectory;
108
    }
109
110
    /**
111
     * @return array
112
     */
113
    public function getJobs()
114
    {
115
        return $this->jobs;
116
    }
117
118
    /**
119
     * @param $key
120
     * @return array
121
     */
122
    public function getJob($key)
123
    {
124
        return $this->jobs[$key];
125
    }
126
127
    /**
128
     * @return boolean
129
     */
130
    public function hasJobs()
131
    {
132
        return (bool) (count($this->jobs) > 0);
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getPhpPath()
139
    {
140
        return $this->phpPath;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getScriptPath()
147
    {
148
        if (!$this->scriptPath) {
149
            $this->scriptPath = getcwd() . DIRECTORY_SEPARATOR  . 'public' . DIRECTORY_SEPARATOR;
150
        }
151
        return $this->scriptPath;
152
    }
153
154
    /**
155
     * @return int
156
     */
157
    public function getTimeout()
158
    {
159
        return $this->timeout;
160
    }
161
}
162