Completed
Push — master ( 1d1ab7...0b8e79 )
by Mahmoud
03:26
created

ProcessMarkdownTemplatesAction   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 15 1
A replace() 0 4 1
A minutesToTimeDisplay() 0 9 1
1
<?php
2
3
namespace App\Containers\Documentation\Actions;
4
5
use App\Port\Action\Abstracts\Action;
6
use DateTime;
7
use Illuminate\Support\Facades\Config;
8
9
/**
10
 * Class ProcessMarkdownTemplatesAction.
11
 *
12
 * @author Mahmoud Zalt <[email protected]>
13
 */
14
class ProcessMarkdownTemplatesAction extends Action
15
{
16
17
    protected $headerMarkdownContent;
18
19
    const PATH = 'Containers/Documentation/ApiDocJs/';
20
21
    /**
22
     * Read the markdown header template and fill it with some real data from the .env file.
23
     */
24
    public function run()
25
    {
26
        // read the template file
27
        $this->headerMarkdownContent = file_get_contents(app_path(self::PATH . 'private/header.template.md'));
28
29
        $this->replace('api.domain.dev', Config::get('api.domain'));
30
        $this->replace('{{rate-limit-expires}}', Config::get('hello.api.limit_expires'));
31
        $this->replace('{{rate-limit}}', Config::get('hello.api.limit'));
32
        $this->replace('{{token-expires}}', $this->minutesToTimeDisplay(Config::get('jwt.ttl')));
33
        $this->replace('{{token-expires-minutes}}', Config::get('jwt.ttl'));
34
        $this->replace('{{pagination-limit}}', Config::get('repository.pagination.limit'));
35
36
        // write the actual file
37
        file_put_contents(app_path(self::PATH . '/private/header.md'), $this->headerMarkdownContent);
38
    }
39
40
    /**
41
     * @param $templateKey
42
     * @param $value
43
     */
44
    private function replace($templateKey, $value)
45
    {
46
        $this->headerMarkdownContent = str_replace($templateKey, $value, $this->headerMarkdownContent);
47
    }
48
49
    /**
50
     * @param $minutes
51
     *
52
     * @return  string
53
     */
54
    private function minutesToTimeDisplay($minutes)
55
    {
56
        $seconds = $minutes * 60;
57
58
        $dtF = new DateTime('@0');
59
        $dtT = new DateTime("@$seconds");
60
61
        return $dtF->diff($dtT)->format('%a days, %h hours, %i minutes and %s seconds');
62
    }
63
}
64